Guava - How to remove from a list, based on a predicate, keeping track of what was removed?

后端 未结 5 1618
清酒与你
清酒与你 2021-02-07 06:51

I have an ArrayList to be filtered, and various Guava Predicates to filter it with. This list will have only 50-100 elements.

I was planning on

5条回答
  •  独厮守ぢ
    2021-02-07 07:37

    This may be an occasion when using a loop is simplest.

    List list =
    Predicate[] predicates =
    Map> removed = 
          new LinkedHashMap>();
    for(Iterator iter=list.iterator();list.hasNext();) {
       MyType mt = iter.next();
       for(Predicate pred: predicates) 
           if(pred.apply(mt)) {
              List mts = removed.get(pred);
              if(mts == null)
                  removed.put(pred, mts = new ArrayList());
              mts.add(mt);
              iter.remove();
              break;
           }
     }
    

提交回复
热议问题