代码之家  ›  专栏  ›  技术社区  ›  Greg

使用apachecommons创建用于过滤数据的复合键

  •  0
  • Greg  · 技术社区  · 15 年前

    这是我的问题

        Collection uniqueExceptions = CollectionUtils.select(allSubareaExceptions, new Predicate(){
            private List<String> ids = new ArrayList<String>();
            public boolean evaluate(Object obj) {
                ....domain.Exception ex = (....domain.Exception)obj;
                String compositeKey = ex.getProcedure().getSubarea().getId() +":"+ex.getProcedure().getId();
                if(ids.contains(compositeKey) == false){
                    ids.add(compositeKey);
                    return true;
                }
                return false;
            }           
        });
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   ILMTitan    15 年前

    有几种选择。您可以创建一个列表:

     List<String> compositKey = Arrays.asList(getSubareaId(), getProcedureId());
    

    SimpleEntry<String, String> compositKey = new SimpleEntry(getSubareaId(), getProcedureId());
    

    使用串联的主要危险在于 : 是id字符串中的合法字符,则可能会得到错误的副本。

        2
  •  0
  •   Aravind Yarram    15 年前

    您可以重写exception类中的equals()来检查“compositeKey”。然后从列表中创建一个集合(Set.addAll()),就这样了。你有一组独特的例外