java Collectors.groupingBy学习笔记
stream().collect Collectors.groupingBy使用方法
public static void main(String[] args) {
List<Data> list = new ArrayList<>();
Random random = new Random();
for(int i=0;i<50;i++){
Data data = new Data();
data.setName("phone"+i);
data.setType(String.valueOf(random.nextInt(10)));
data.setImg("img"+i);
list.add(data);
}
System.out.println(list);
HashMap<String, ArrayList<Data>> newData = list.stream().collect(Collectors.groupingBy(Data::getType, HashMap::new, Collectors.toCollection(ArrayList::new)));
Set<Map.Entry<String, ArrayList<Data>>> set = newData.entrySet();
Iterator<Map.Entry<String, ArrayList<Data>>> it = set.iterator();
JSONArray re = new JSONArray();
while (it.hasNext()){
JSONObject jsonObject = new JSONObject();
jsonObject.put("key",it.next().getKey());
List<Data> va = it.next().getValue();
jsonObject.put("value",va);
re.add(jsonObject);
}
System.out.println(re);
}
HashMap<String, ArrayList> newData = list.stream().collect(Collectors.groupingBy(Data::getType, HashMap::new, Collectors.toCollection(ArrayList::new)));
Collectors.groupingBy:分组
Collectors.groupingBy(以实体类哪个属性为依据分组,转换类型,value类型)
HashMap<String, ArrayList>,key分组后的值,value分组后type=key的数据
[Data(name=phone0, type=4, img=img0), Data(name=phone1, type=0, img=img1), Data(name=phone2, type=1, img=img2), Data(name=phone3, type=8, img=img3), Data(name=phone4, type=6, img=img4), Data(name=phone5, type=7, img=img5), Data(name=phone6, type=5, img=img6), Data(name=phone7, type=8, img=img7), Data(name=phone8, type=3, img=img8), Data(name=phone9, type=9, img=img9), Data(name=phone10, type=1, img=img10), Data(name=phone11, type=4, img=img11), Data(name=phone12, type=9, img=img12), Data(name=phone13, type=7, img=img13), Data(name=phone14, type=0, img=img14), Data(name=phone15, type=6, img=img15), Data(name=phone16, type=2, img=img16), Data(name=phone17, type=4, img=img17), Data(name=phone18, type=4, img=img18), Data(name=phone19, type=0, img=img19), Data(name=phone20, type=4, img=img20), Data(name=phone21, type=2, img=img21), Data(name=phone22, type=4, img=img22), Data(name=phone23, type=0, img=img23), Data(name=phone24, type=8, img=img24), Data(name=phone25, type=8, img=img25), Data(name=phone26, type=9, img=img26), Data(name=phone27, type=8, img=img27), Data(name=phone28, type=6, img=img28), Data(name=phone29, type=7, img=img29), Data(name=phone30, type=7, img=img30), Data(name=phone31, type=9, img=img31), Data(name=phone32, type=7, img=img32), Data(name=phone33, type=6, img=img33), Data(name=phone34, type=6, img=img34), Data(name=phone35, type=3, img=img35), Data(name=phone36, type=7, img=img36), Data(name=phone37, type=3, img=img37), Data(name=phone38, type=6, img=img38), Data(name=phone39, type=4, img=img39), Data(name=phone40, type=7, img=img40), Data(name=phone41, type=3, img=img41), Data(name=phone42, type=9, img=img42), Data(name=phone43, type=5, img=img43), Data(name=phone44, type=2, img=img44), Data(name=phone45, type=3, img=img45), Data(name=phone46, type=3, img=img46), Data(name=phone47, type=1, img=img47), Data(name=phone48, type=4, img=img48), Data(name=phone49, type=7, img=img49)]
转换后
[{"value":[{"img":"img2","name":"phone2","type":"1"},{"img":"img10","name":"phone10","type":"1"},{"img":"img47","name":"phone47","type":"1"}],"key":"0"},{"value":[{"img":"img8","name":"phone8","type":"3"},{"img":"img35","name":"phone35","type":"3"},{"img":"img37","name":"phone37","type":"3"},{"img":"img41","name":"phone41","type":"3"},{"img":"img45","name":"phone45","type":"3"},{"img":"img46","name":"phone46","type":"3"}],"key":"2"},{"value":[{"img":"img6","name":"phone6","type":"5"},{"img":"img43","name":"phone43","type":"5"}],"key":"4"},{"value":[{"img":"img5","name":"phone5","type":"7"},{"img":"img13","name":"phone13","type":"7"},{"img":"img29","name":"phone29","type":"7"},{"img":"img30","name":"phone30","type":"7"},{"img":"img32","name":"phone32","type":"7"},{"img":"img36","name":"phone36","type":"7"},{"img":"img40","name":"phone40","type":"7"},{"img":"img49","name":"phone49","type":"7"}],"key":"6"},{"value":[{"img":"img9","name":"phone9","type":"9"},{"img":"img12","name":"phone12","type":"9"},{"img":"img26","name":"phone26","type":"9"},{"img":"img31","name":"phone31","type":"9"},{"img":"img42","name":"phone42","type":"9"}],"key":"8"}]
本文来自博客园,作者:Sadness_sa,转载请注明原文链接:https://www.cnblogs.com/sadness-sa/p/16771042.html

浙公网安备 33010602011771号