Apache Commons Collections MultiValuedMap通过多个字段过滤数据

时间:2019-06-14 07:37:52

标签: java collections filtering guava

下面的数据是一个内存集中的Java ArrayList集合,需要从该集合中根据两个字段(VRNT_CD和ITM_NB)的组合来过滤数据

VRNT_CD ITM_NB COMMT_TEXT
 10      A0A     SampleText1
 10      A0A     SampleText2
 10      A0A     SampleText3
 10      A0B     SampleText4
 10      A0C     SampleText5
 20      A0A     SampleText6
 20      A0A     SampleText7
 20      A0B     SampleText8
 20      A0C     SampleText9
 30      A0A     SampleText10
 30      A0A     SampleText11
 30      AOB     SampleText12
 30      A0C     SampleText13
 30      A0C     SampleText14

如上所述,上表中的每一行都映射到下面的java对象

public class SummaryDataOracle {

    private String funcCode;
    private String commentText;
    private String variantCd;
    private String itemNB;
  //setters //getters
}

上表的集合表示为List<SummaryDataOracle>,需要使用基于以下键的集合对象来生成地图

public class VssKey {

    private String funCode;
    private String varntCode;
    private String itemNb;

    //setters //getters // equals // hashcode

}

因此,结果集合应具有以下数据结构

AOA   10    SampleText1
            SampleText2
            SampleText3

      20    SampleText6
            SampleText7

      30    SampleText10
            SampleText11

AOB   10    SampleText4

      20    SampleText8

      30    SampleText12

AOC   10    SampleText5

      20    SampleText9

      30    SampleText13
            SampleText14

1 个答案:

答案 0 :(得分:0)

通过实现以下对象并使用apache commons MultiValuedMap解决了该问题。

public class VssKey {

     // getterrs
     // setters
     // equals
     // hashcode
}

    MultiValuedMap<VssKey, String> partNumberVarientMap = new ArrayListValuedHashMap<>();

            for (SummaryDataOracle summaryDataOracle : summeryDataOracleList) {

                VssKey key = new VssKey(summaryDataOracle);

                String varntText = null;
                if (!StringUtils.isEmpty(summaryDataOracle.getVariantSmText())) {
                    varntText = summaryDataOracle.getVariantSmText().trim();
                }

                partNumberVarientMap.put(key, varntText);    
}