Reputation: 3
I have a list of objects, let's call it Model
public class Model {
String modelId;
List<String> PropertyA;
List<String> PropertyB;
List<String> PropertyC;
String modelCol1;
String modelCol2;
String modelCol3;
String modelCol4;
}
Sample data :
1, ["A","C"], ["7", "8"], ["00111", "00112"], "colA1", "colA2", "colA3", "colA4"
2, ["A", "B"], ["8", "9"], ["00112", "00113"], "colB1", "colB2", "colB3", "colB4"
Expected :
Get elements from lists within list of Models and then group by multiple fields
"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"}]
. ..
In this case, "A", "8", "00112" is captured just once. For each group, selecting first or selecting any of the Model objects is acceptable. Is this possible using LambdaJ?
I'm currently only able to group by lists directly without extracting individual elements from the lists.
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]}]
Any advice is much appreciated.
Upvotes: 0
Views: 292
Reputation: 1356
first create
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() {...}
}
then first convert every Model to list of RefModel for each propertyA, propertyB and propertyC, then add them to one list, then select distinct of them,
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;
})));
Upvotes: 0