2016-05-15 94 views
0

我有對象的列表,我們稱之爲IT模式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」 被捕獲一次。對於每個組,首先選擇或選擇任何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

謝謝Reto糾正我的格式!這是我的第一篇文章,我只格式化代碼,但將來也會格式化數據。 – user777

回答

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() {...} 

} 

那麼首先每一個模型轉換爲RefModel的列表中爲每個propertyA,propertyB和propertyC,然後將它們添加到一個列表,然後選擇不同的人,

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; 
     }))); 
+0

謝謝你!有用! – user777