我有一个对象列表,我们称之为模型
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"}]
。 ..
在这种情况下,&#34; A&#34;,&#34; 8&#34;,&#34; 00112&#34;被捕获一次。对于每个组,首先选择或选择任何Model对象是可以接受的。这可能是使用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]}]
非常感谢任何建议。
答案 0 :(得分:0)
首先创建
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转换为每个propertyA,propertyB和propertyC的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;
})));