克隆集合成员匹配条件

时间:2015-12-24 09:31:42

标签: java arraylist collections filter

我尝试编写一个方法,当传递一个arraylist时,将返回一个新的arraylist,它只包含符合指定条件的成员(不使用迭代)。集合类有一个类似的removeIf,但我正在寻找一个" copyIf"如果存在任何此类功能。例如:

public class Foo {
  public int value;
  public Foo(int val){
    value = val;
  }
}

public class Bar {
  private ArrayList<Foo> refList;
  public ArrayList<Foo> overTwentyList;

  public Bar(){
    refList = new ArrayList<Foo>();
    refList.add(new Foo(12));
    refList.add(new Foo(23));
    refList.add(new Foo(6));
    refList.add(new Foo(44));
    refList.add(new Foo(2));
    refList.add(new Foo(19));
    refList.add(new Foo(99));
    refList.add(new Foo(74));

    overTwentyList = overTwenty(refList);
    // now overTwentyList would contain only Foo members
    // with values 23, 44, 99, 74 and refList would retain
    // all values
  }

  public ArrayList<Foo> overTwenty(ArrayList<Foo> bankList){
    // bankList filter code here
    // normally i would use iteration but i think
    // calling this method frequently would eat a lot
    // of system resources if it iterates every time
    return filteredList;
  }
}

2 个答案:

答案 0 :(得分:2)

当然,over twenty列表只是列表的副本,将删除所有under twenty个条目。您可以使用removeIf

执行此操作
    public ArrayList<Foo> overTwenty(ArrayList<Foo> bankList) {
        // Take a complete copy.
        ArrayList<Foo> filtered = new ArrayList<>(bankList);
        // Remove the under 20s.
        filtered.removeIf(a -> a.value <= 20);
        return filtered;
    }

然而,这仍然每次都复制整个列表。你需要一点摆动来避免这种情况。 Iterable<Foo>可以接受吗?

public class Foo {

    public int value;

    public Foo(int val) {
        value = val;
    }

    @Override
    public String toString() {
        return "Foo{" + "value=" + value + '}';
    }

}

public class Bar {

    private ArrayList<Foo> refList;
    public Iterable<Foo> overTwentyList;

    public Bar() {
        refList = new ArrayList<Foo>();
        refList.add(new Foo(12));
        refList.add(new Foo(23));
        refList.add(new Foo(6));
        refList.add(new Foo(44));
        refList.add(new Foo(2));
        refList.add(new Foo(19));
        refList.add(new Foo(99));
        refList.add(new Foo(74));

        overTwentyList = new OverTwenties(refList);
    }

    class OverTwenties implements Iterable<Foo> {

        private final Iterable<Foo> refList;

        private OverTwenties(ArrayList<Foo> refList) {
            this.refList = refList;
        }

        @Override
        public Iterator<Foo> iterator() {
            return new Iterator<Foo>() {
                Iterator<Foo> ref = refList.iterator();
                Foo next = null;

                @Override
                public boolean hasNext() {
                    while (next == null && ref.hasNext()) {
                        do {
                            next = ref.hasNext() ? ref.next() : null;
                            // Skip everything less than 20.
                        } while (next != null && next.value < 20);
                    }
                    return next != null;
                }

                @Override
                public Foo next() {
                    Foo n = next;
                    next = null;
                    return n;
                }

            };
        }

    }

}

答案 1 :(得分:0)

我建议查看google-guava库。对于使用集合有很大的支持。