Lambdaj按展平列表分组



我有一个对象列表,我们称之为模型

public class Model {
String modelId;
List<String> PropertyA;
List<String> PropertyB;
List<String> PropertyC;
String modelCol1;
String modelCol2;
String modelCol3;
String modelCol4;
}

样本数据:

1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"
2, ["A", "B"], ["8", "9"], ["00112", "00113"], "colB1", "colB2", "colB3", "colB4"

预期:

从模型列表中的列表中获取元素,然后按多个字段分组

"A", "7", "00111", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"A", "7", "00112", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"A", "8", "00111", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"A", "8", "00112", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"C", "7", "00111", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"C", "7", "00112", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"C", "8", "00111", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"C", "8", "00112", [{1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"}]
"A", "8", "00113", [{2, ["A", "B"], ["8", "9"], ["00112", "00113"], "colB1", "colB2", "colB3", "colB4"}]
"A", "9", "00112", [{2, ["A", "B"], ["8", "9"], ["00112", "00113"], "colB1", "colB2", "colB3", "colB4"}]
"A", "9", "00113", [{2, ["A", "B"], ["8", "9"], ["00112", "00113"], "colB1", "colB2", "colB3", "colB4"}]

..

在这种情况下,"A"、"8"、"00112"只捕获一次。对于每个组,可以先选择或选择任何模型对象。这可能使用LambdaJ吗?

我目前只能直接按列表进行分组,而无需从列表中提取单个元素。

    Group<Model> grpResult = group(models, by(on(Model.class).getPropertyA()), by(on(Model.class).getPropertyB()), by(on(Model.class).getPropertyC()));
returns
[{children=[{children=[{children=[Model [modelId=null, PropertyA=[A, B], PropertyB=[7, 8], PropertyC=[00111, 00112], modelCol1=ACol1, modelCol2=ACol2, modelCol3=null, modelCol4=null]], propertyC=[00111, 00112]}], propertyB=[7, 8]}], propertyA=[A, B]}, 
 {children=[{children=[{children=[Model [modelId=null, PropertyA=[A, C], PropertyB=[8, 9], PropertyC=[00112, 00113], modelCol1=BCol1, modelCol2=BCol2, modelCol3=null, modelCol4=null]], propertyC=[00112, 00113]}], propertyB=[8, 9]}], propertyA=[A, C]}]

如有任何建议,不胜感激。

首先创建

class RefModel {
String PropertyA;
String PropertyB;
String PropertyC;
Model model;
public RefModel(String propertyA, String propertyB, String propertyC, Model model) {...}
@Override
public int hashCode() {
    // only calculate propertyA, propertyB, propertyC
    ...
}
@Override
public boolean equals(Object obj) {
   // only compare propertyA, propertyB, propertyC
   ...
}
@Override
public String toString() {...}
}

然后首先将每个Model转换为每个属性A、属性B和属性C的RefModel列表,然后将它们添加到一个列表中,然后选择不同的

    Collection<RefModel> refModels = selectDistinct(flatten(convert(models, (Converter<Model, List<RefModel>>)(m)->{
            List<RefModel> refs = new ArrayList<RefModel>();
            for(String a:m.getPropertyA()){
                for(String b:m.getPropertyB()){
                    for(String c:m.getPropertyC()){
                        refs.add(new RefModel(a, b, c, m));
                    }
                }
            }
            return refs;
        })));

相关内容

  • 没有找到相关文章

最新更新