@Data
@AllArgsConstructor
public class Kvs<T> {
private String key;
private T value;
}
public class AnswerApp {
public static void main(String[] args) {
List<Kvs<String>> kvsList = Lists.newArrayList(
new Kvs<>("A0001", "Answer"),new Kvs<>("A0001", "AnswerAIL"),new Kvs<>("A0001", "AI"),
new Kvs<>("A0002", "Iris"),new Kvs<>("A0002", "IrisA"),new Kvs<>("A0003", "Allen"),
new Kvs<>("A004", "James"),new Kvs<>("A005", "Kak"),new Kvs<>("A005", "KaKa")
);
// 对 key 进行统计个数
Map<String, Long> map1 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.counting()));
System.out.println("map1=" + JSON.toJSONString(map1));
System.out.println();
// 根据 key 对集合进行分组-1
Map<String, List<Kvs>> map2_1 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.toList()));
System.out.println("map2_1=" + JSON.toJSONString(map2_1));
System.out.println();
// 根据 key 对集合进行分组-2
Map<String, List<Kvs>> map2_2 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey));
System.out.println("map2_2=" + JSON.toJSONString(map2_2));
System.out.println();
// 根据 key 对集合进行分组, 并且 value 的集合为指定属性集合而不是对象
Map<String, List<String>> map3 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.mapping(Kvs::getValue, Collectors.toList())));
System.out.println("map3=" + JSON.toJSONString(map3));
System.out.println();
// 根据 key 进行分组并且将指定属性值进行拼接作为哈希表的 value 值
Map<String, String> map4 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.mapping(Kvs::getValue, Collectors.joining("#"))));
System.out.println("map4=" + JSON.toJSONString(map4));
System.out.println();
// 根据 key 进行分组并根据 指定属性进行比较取最大元素
Map<String, Optional<Kvs<String>>> map5 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.maxBy(Comparator.comparing(Kvs::getValue))));
System.out.println("map5=" + JSON.toJSONString(map5));
System.out.println();
// partitioningBy 会根据 传入的 表达式进行判断, 将集合分为 true 和 false 两个列表
Map<Boolean, List<Kvs>> map6 = kvsList.stream().collect(Collectors.partitioningBy(kvs -> "A0001".equals(kvs.getKey())));
System.out.println("map6=" + JSON.toJSONString(map6));
System.out.println();
// 遍历结果集
kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.toList())).forEach((k, v) -> {
System.out.println(MessageFormat.format("key={0}, value={1}", k, v));
});
List<Kvs<Integer>> numList = Lists.newArrayList(
new Kvs<>("A001", 1),new Kvs<>("A001", 2),new Kvs<>("A001", 3),
new Kvs<>("A002", 5),new Kvs<>("A002", 3),new Kvs<>("A003", 7)
);
System.out.println();
// 根据 key 进行分组并且根据 指定数据进行统计
Map<String, Integer> num1 = numList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.summingInt(Kvs::getValue)));
System.out.println("num1=" + JSON.toJSON(num1));
System.out.println();
// summarizingInt 用来同时计算 average、min、max、count、sum
Map<String, IntSummaryStatistics> num2 = numList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.summarizingInt(Kvs::getValue)));
System.out.println("num2=" + JSON.toJSON(num2));
System.out.println();
// 集合实体转 实体中某一字段集合
List<String> keys = kvsList.stream().map(Kvs::getKey).collect(Collectors.toList());
}
}
map1={"A0001":3,"A0002":2,"A005":2,"A004":1,"A0003":1}
map2_1={"A0001":[{"key":"A0001","value":"Answer"},{"key":"A0001","value":"AnswerAIL"},{"key":"A0001","value":"AI"}],"A0002":[{"key":"A0002","value":"Iris"},{"key":"A0002","value":"IrisA"}],"A005":[{"key":"A005","value":"Kak"},{"key":"A005","value":"KaKa"}],"A004":[{"key":"A004","value":"James"}],"A0003":[{"key":"A0003","value":"Allen"}]}
map2_2={"A0001":[{"key":"A0001","value":"Answer"},{"key":"A0001","value":"AnswerAIL"},{"key":"A0001","value":"AI"}],"A0002":[{"key":"A0002","value":"Iris"},{"key":"A0002","value":"IrisA"}],"A005":[{"key":"A005","value":"Kak"},{"key":"A005","value":"KaKa"}],"A004":[{"key":"A004","value":"James"}],"A0003":[{"key":"A0003","value":"Allen"}]}
map3={"A0001":["Answer","AnswerAIL","AI"],"A0002":["Iris","IrisA"],"A005":["Kak","KaKa"],"A004":["James"],"A0003":["Allen"]}
map4={"A0001":"Answer#AnswerAIL#AI","A0002":"Iris#IrisA","A005":"Kak#KaKa","A004":"James","A0003":"Allen"}
map5={"A0001":{"key":"A0001","value":"AnswerAIL"},"A0002":{"key":"A0002","value":"IrisA"},"A005":{"key":"A005","value":"Kak"},"A004":{"key":"A004","value":"James"},"A0003":{"key":"A0003","value":"Allen"}}
map6={false:[{"key":"A0002","value":"Iris"},{"key":"A0002","value":"IrisA"},{"key":"A0003","value":"Allen"},{"key":"A004","value":"James"},{"key":"A005","value":"Kak"},{"key":"A005","value":"KaKa"}],true:[{"key":"A0001","value":"Answer"},{"key":"A0001","value":"AnswerAIL"},{"key":"A0001","value":"AI"}]}
key=A0001, value=[Kvs(key=A0001, value=Answer), Kvs(key=A0001, value=AnswerAIL), Kvs(key=A0001, value=AI)]
key=A0002, value=[Kvs(key=A0002, value=Iris), Kvs(key=A0002, value=IrisA)]
key=A005, value=[Kvs(key=A005, value=Kak), Kvs(key=A005, value=KaKa)]
key=A004, value=[Kvs(key=A004, value=James)]
key=A0003, value=[Kvs(key=A0003, value=Allen)]
num1={"A003":7,"A001":6,"A002":8}
num2={"A003":{"average":7.0,"min":7,"max":7,"count":1,"sum":7},"A001":{"average":2.0,"min":1,"max":3,"count":3,"sum":6},"A002":{"average":4.0,"min":3,"max":5,"count":2,"sum":8}}
// List 转 Map>
List<Kvi> params = Lists.newArrayList();
// 加工数据... params.add(new Kvi());
/*
* -----------------------------------------------------------------------------------------------------------------------------------------------------
* 丨 params 丨 rleParams
* -----------------------------------------------------------------------------------------------------------------------------------------------------
* 丨 1, Name, zhangsan 丨
* 丨 1, Liandate, 20190809 丨
* 丨 1, Status, Open 丨 {1: {Name: zhangsan, Liandate: 20190809, Status: Open}, 2: {Name: lisi, Liandate: 20190809, Status: Close}}
* 丨 2, Name, lisi 丨
* 丨 2, Liandate, 20190809 丨
* 丨 2, Status, Close 丨
* -----------------------------------------------------------------------------------------------------------------------------------------------------
* */
Map<Integer, Map<String, String>> rleParams = params.stream().collect(Collectors.groupingBy(Kvi::getIndex, Collectors.toMap(Kvi::getKey, Kvi::getValue)));
@Data
public class Kvi {
private Integer index;
private String key;
private String value;
}
public class AnswerApp {
public static void main(String[] args) {
List<User> users = Lists.newArrayList(
new User("高三1班", "stu01", "男"),
new User("高三1班", "stu02", "女"),
new User("高三2班", "stu11", "男"),
new User("高三2班", "stu12", "女"),
new User("高三3班", "stu21", "女"),
new User("高三3班", "stu22", "男"),
new User("高三3班", "stu23", "女"));
Map<String, List<User>> collect = users.stream().collect(groupingBy(User::getClassName));
System.out.println(JSON.toJSONString(collect));
System.out.println();
Map<String, Long> collect1 = users.stream().collect(groupingBy(User::getClassName, Collectors.counting()));
System.out.println(JSON.toJSONString(collect1));
System.out.println();
Map<String, Map<String, User>> collect2 = users.stream().collect(groupingBy(User::getClassName, Collectors.toMap(User::getStudentName, o -> o)));
System.out.println(JSON.toJSONString(collect2));
System.out.println();
}
@Data
@AllArgsConstructor
private static class User {
private String className;
private String studentName;
private String sex;
}
}
程序运行结果
{"高三3班":[{"className":"高三3班","sex":"女","studentName":"stu21"},{"className":"高三3班","sex":"男","studentName":"stu22"},{"className":"高三3班","sex":"女","studentName":"stu23"}],"高三2班":[{"className":"高三2班","sex":"男","studentName":"stu11"},{"className":"高三2班","sex":"女","studentName":"stu12"}],"高三1班":[{"className":"高三1班","sex":"男","studentName":"stu01"},{"className":"高三1班","sex":"女","studentName":"stu02"}]}
{"高三3班":3,"高三2班":2,"高三1班":2}
{"高三3班":{"stu21":{"className":"高三3班","sex":"女","studentName":"stu21"},"stu23":{"className":"高三3班","sex":"女","studentName":"stu23"},"stu22":{"className":"高三3班","sex":"男","studentName":"stu22"}},"高三2班":{"stu12":{"className":"高三2班","sex":"女","studentName":"stu12"},"stu11":{"className":"高三2班","sex":"男","studentName":"stu11"}},"高三1班":{"stu02":{"className":"高三1班","sex":"女","studentName":"stu02"},"stu01":{"className":"高三1班","sex":"男","studentName":"stu01"}}}