2017-05-03 73 views
0

我有字符串數組的ArrayList,已經低於聚集串

{"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"} 
{"test1","abc","20","50","t1","SD1","0","1"} 
{"test2","znc","21","23","t1","SF5","3","4"} 
{"test1","abc","20","50","t1","SD3","0","1"} 
{"test1","HJc","53","50","t1","SD3","0","1"} 
{"test2","znc","21","23","t1","SF1","1","6"} 
{"test1","abc","20","50","t1","SD5","2","19"} 
{"test3","ldb","19","54","t1","SR51","6","1"} 
{"test2","znc","21","23","t1","SF12","17","36"} 
{"test3","ldb","19","54","t1","SR44","19","31"} 
{"test4","lYI","76","56","t1","TB77","54","87"} 

我想有通過排序此電流ArrayList的一個新的ArrayList(列和行)的值填充並獲得線使用相同的鍵(順序:名稱,名稱,Id1,Id2,類型),將它們的值在一行中連接(用;分隔)在一行中。

預期輸出:

{"name","sname","Id1","Id2","type","Comment"} 
{"test1","abc","20","50","t1","SD1,0,1; SD3,0,1; SD5,2,19"} 
{"test1","HJc","53","50","t1","SD3,0,1"} 
{"test2","znc","21","23","t1","SF5,3,4; SF1,1,6; SF12,17,36"} 
{"test3","ldb","19","54","t1","SR44,19,31;SR51,6,1 } 
{"test4","lYI","76","56","t1","TB77,54,87"} 

我的ArrayList是從結果查詢生成:

// header 
    String[] myString0 = {"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"}; 
    //lines 
    while (rset.next()) { 

           String name = rset.getString("name"); 
           String sname = rset.getString("sname"); 
           String Id1 = rset.getString("Id1"); 
           String Id2 = rset.getString("Id2"); 
           String type = rset.getString("type"); 
           String LDP = rset.getString("LDP"); 
           String oldvalue = rset.getString("oldvalue"); 
           String newvalue = rset.getString("newvalue"); 

           String[] myString1 = {name, sname, Id1, Id2, "type", LDP, oldvalue, newvalue}; 

           outerArr.add(myString1);// my Arraylist 
          } 
         } 

感謝,

+0

你舉的例子是模糊的(以我?)。你是怎麼定的。按什麼排序?如何連接值?請描述這些事情。 –

+0

感謝您的回覆,按名稱排序,名稱,Id1,Id2,類型(作爲鍵) –

+0

一種方法是創建一個代表您的密鑰的類。說那是MyKey。現在您可以創建一個HashMap >。將每一行添加到地圖中的列表中,如有必要,創建一個條目。現在只需遍歷地圖中的條目。對於每個條目,從該條目列表中的記錄創建摘要行。 –

回答

1

下面是使用流的解決方案,從番石榴的Ordering效用有點幫助:

public static List<String[]> aggregate(List<String[]> data) { 
    List<String[]> aggregated = data.stream() 
      .skip(1) 
      .map(Arrays::asList) 
      .collect(Collectors.groupingBy(
        a -> a.subList(0, 5), 
        () -> new TreeMap<>(
          Ordering.from(String.CASE_INSENSITIVE_ORDER) 
            .lexicographical()), 
        Collectors.mapping(
          a -> String.join(",", a.subList(5, 8)), 
          Collectors.joining("; ")))) 
      .entrySet() 
      .stream() 
      .map(e -> Stream.concat(
        e.getKey().stream(), 
        Stream.of(e.getValue()))) 
      .map(s -> s.toArray(String[]::new)) 
      .collect(Collectors.toCollection(ArrayList::new)); 

    aggregated.add(0, new String[] {"name","sname","Id1","Id2","type","Comment"}); 

    return aggregated; 
} 

測試:

public static void main(String[] args) { 
    List<String[]> data = Arrays.asList(new String[][] { 
      {"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"}, 
      {"test1","abc","20","50","t1","SD1","0","1"}, 
      {"test2","znc","21","23","t1","SF5","3","4"}, 
      {"test1","abc","20","50","t1","SD3","0","1"}, 
      {"test1","HJc","53","50","t1","SD3","0","1"}, 
      {"test2","znc","21","23","t1","SF1","1","6"}, 
      {"test1","abc","20","50","t1","SD5","2","19"}, 
      {"test3","ldb","19","54","t1","SR51","6","1"}, 
      {"test2","znc","21","23","t1","SF12","17","36"}, 
      {"test3","ldb","19","54","t1","SR44","19","31"}, 
      {"test4","lYI","76","56","t1","TB77","54","87"} 
    }); 

    aggregate(data) 
      .stream() 
      .map(Arrays::toString) 
      .forEach(System.out::println); 
} 

輸出:

 
[name, sname, Id1, Id2, type, Comment] 
[test1, abc, 20, 50, t1, SD1,0,1; SD3,0,1; SD5,2,19] 
[test1, HJc, 53, 50, t1, SD3,0,1] 
[test2, znc, 21, 23, t1, SF5,3,4; SF1,1,6; SF12,17,36] 
[test3, ldb, 19, 54, t1, SR51,6,1; SR44,19,31] 
[test4, lYI, 76, 56, t1, TB77,54,87] 
+0

謝謝你的幫助!預計的解決方案! :-) –

1
  1. 創建自己的密鑰類第5列,或將它們連接成一個字符串(可能會或可能不足以滿足您的用例)。
  2. 將所有內容添加到由您的關鍵類鍵入的地圖,併爲您的值/連續字段使用String,StringBuilder或List
  3. 將所有數據解析到地圖後,迭代地圖以將所有內容到列表中,循環以連接每個鍵的值
  4. 根據排序偏好對新列表排序(或者,最初使用LinkedHashMap維護排序的映射)。

上面使用番石榴ArrayListMultiMap可能更容易做到。

E.g.像

// for each row... 
myArrayListMultiMap.put(new MyKey(name, sname, id1, id2, type), LDP + "," + oldValue + "," + newValue); 

// then 
for (MyKey key : myArrayListMultiMap.keySet()) { 
    List<String> values = myArrayListMultiMap.get(key); 
    String concatenated = StringUtils.join(values, ";"); 
    myList.add(new Row(entry.getKey(), concatenated)); 
} 

Collections.sort(myList, myComparator); 

重要提示:請確保您的myKey類實現hashCodeequals

+0

謝謝你的幫助! –