Java 中的过滤器 java.util.Collection

发布于 2024-09-25 17:29:47 字数 474 浏览 2 评论 0原文

我编写了一个util类来过滤java.util.Collection中的元素,如下所示:

public class Util{
  public static <T> void filter(Collection<T> l, Filter<T> filter) {
    Iterator<T> it= l.iterator();
    while(it.hasNext()) {
      if(!filter.match(it.next())) {
        it.remove();
      }
    }
  }
}

public interface Filter<T> {
  public boolean match(T o);
}

问题:

  1. 您认为有必要编写该方法吗?
  2. 方法上有什么改进吗?

I wrote a util class to filter elements in java.util.Collection as follows:

public class Util{
  public static <T> void filter(Collection<T> l, Filter<T> filter) {
    Iterator<T> it= l.iterator();
    while(it.hasNext()) {
      if(!filter.match(it.next())) {
        it.remove();
      }
    }
  }
}

public interface Filter<T> {
  public boolean match(T o);
}

Questions:

  1. Do you think it's necessary to write the method?
  2. Any improvement about the method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

海之角 2024-10-02 17:29:47

您应该允许任何 Filter 不仅仅是 Filter

客户端可能还希望有一个返回新 Collection 的方法:

public static <T> Collection<T> filter(Collection<T> unfiltered, 
    Filter<? super T> filter)

You should allow any Filter<? super T> not just Filter<T>.

Clients might also want to have a method that returns a new Collection instead:

public static <T> Collection<T> filter(Collection<T> unfiltered, 
    Filter<? super T> filter)
落叶缤纷 2024-10-02 17:29:47
  1. 不会。guava-libraries 已经具备此功能。请参阅 Iterables.filter(iterableCollection, predicate) 其中 Predicate 实现过滤
  1. No. The guava-libraries already have this functionality. See Iterables.filter(iterableCollection, predicate) where the Predicate implements the filtering
木森分化 2024-10-02 17:29:47

是否有必要取决于您想要实现的目标。如果您可以使用其他第三方库(例如 Google Collections),则不需要。如果计划是一次性的,那么可能不会。如果您计划创建不同的过滤器,那么是的,这看起来是保持事物模块化和内聚性的好方法。

一个建议 - 您可能想要返回一个集合 - 这样,您可以选择返回一个新的过滤集合,而不是改变原始集合。如果您需要在并发上下文中使用它,这可能会很方便。

您还可以查看对这个类似问题的回复< /a>.

Whether it's necessary depends on what you want to achieve. If you can use other third party libs like Google Collections, then no. If it's planned to be a one-off, then probably not. If you plan on creating different filters, then yep, looks like a good approach to keep things modular and cohesive.

One suggestion - you might want to return a Collection - that way, you have the option of returning a new filtered Collection rather than mutating the original Collection. That could be handy if you need to use it in a concurrent context.

You might also look at the responses to this similar question.

梦魇绽荼蘼 2024-10-02 17:29:47

关于问题1,已经有很多馆藏库了。过滤是由 apache common-collections CollectionUtils 和 google 集合 可迭代

Regarding question 1 there are already a lot of collection libraries. Filtering is offered by instance by apache common-collections CollectionUtils and google collections Iterables .

魂牵梦绕锁你心扉 2024-10-02 17:29:47

看起来不错 - 但我们无法决定是否“有必要”编写它(好吧,你确实写了它;))

remove() 方法并不总是实现,它被标记为 (可选)。有些迭代器只是抛出UnsupportedOperationException。您应该捕获它或将其转换为自定义异常,表示无法过滤此集合。

然后您可以将方法签名更改为,

public static <T> void filter(Iterable<T> i, Filter<T> filter)

因为迭代器不限于集合。使用此实用方法,您可以过滤每个提供允许删除操作的迭代器的“容器”。

Looks nice - but we can't decide, if it's 'necessary' to write it (OK, you actually wrote it ;) )

The remove() method is not always implemented, it is labelled (optional). Some Iterators just throw an UnsupportedOperationException. You should catch it or convert it to a custom exception saying, that this collection can't be filtered.

And then you could change the method signature to

public static <T> void filter(Iterable<T> i, Filter<T> filter)

because iterators are not limited to Collections. With this utility method you could filter every 'container' that provides an iterator which allows remove operations.

拥抱影子 2024-10-02 17:29:47

<块引用>

你觉得有必要写这个方法吗?

如果您不介意使用第三方库,那么不。

对提供此功能的第三方库的一些建议:

您可能需要查看提供 filter< 的 Functional Java /code> 以及在真正的蓝色函数语言中发现的许多其他高阶函数。

示例:

List<Person> adults = filter(people, new F1<Person, Boolean>() {
  public Boolean f(Person p) {
    return p.getAge() > 18;
  }
});

另一种选择是使用 lambdaj - 一个具有类似目标但比功能性更简洁的库爪哇。不过 lambdaj 的覆盖范围不如函数式 Java 那么多。

例子:

List<Person> adults = filter(having(on(Person.class).getAge(), greaterThan(18)), people);

Do you think it's necessary to write the method?

If you don't mind using a third party library then no.

Some suggestions for third party libraries that provide this functionality:

You might want to look at Functional Java which provides filter plus many other higher order functions found in true-blue functional languages.

Example:

List<Person> adults = filter(people, new F1<Person, Boolean>() {
  public Boolean f(Person p) {
    return p.getAge() > 18;
  }
});

Another alternative is using lambdaj - a library with similar goals but is much more concise than Functional Java. lambdaj doesn't cover as much ground as Functional Java though.

Example:

List<Person> adults = filter(having(on(Person.class).getAge(), greaterThan(18)), people);
仙气飘飘 2024-10-02 17:29:47

我认为在 Filter 接口中定义一个 visit(T o) 方法会很酷。这样,过滤器实现可以决定当存在匹配时对访问的对象采取什么操作。

I think it would be cool to have a visit(T o) method defined in your Filter<T> interface. That way the filter implementation can decide what action to take on the visited object when there is a match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文