【问题标题】:clone collection members matching condition克隆集合成员匹配条件
【发布时间】:2016-03-30 18:17:21
【问题描述】:

我正在尝试编写一个方法,当传递一个数组列表时,它将返回一个新的数组列表,其中仅包含与指定条件匹配的成员(不使用迭代)。集合类有一个类似的 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;
  }
}

【问题讨论】:

  • 而不是迭代?在后台几乎总是在迭代。

标签: java arraylist collections filter


【解决方案1】:

我建议查看 google-guava 库。非常支持使用集合。

【讨论】:

    【解决方案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&lt;Foo&gt; 可以接受吗?

    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;
                    }
    
                };
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多