@Data
@AllArgsConstructor
static class Achievement{
private String name;
private double score;
private String date;
}
static List<Achievement> getData(){
List<Achievement> list = new ArrayList<>();
list.add(new Achievement("wang",80,"2017-10-20"));
list.add(new Achievement("wang",90,"2017-5-20"));
list.add(new Achievement("li",90,"2018-10-20"));
list.add(new Achievement("zhao",50,"2017-10-20"));
list.add(new Achievement("qian",60,"2017-10-22"));
list.add(new Achievement("sun",20,"2017-10-23"));
list.add(new Achievement("sun",20,"2017-11-23"));
return list;
}
/**找出某个人的成绩*/
static void a1(){
Achievement achievement = getData().stream().filter(a-> a.getName().equals("wang")).findFirst().get();
System.out.println(achievement.toString());
}
/**是否含有某条数据*/
static void a2(){
boolean match = getData().stream().anyMatch(a -> a.getName().equals("liu"));
System.out.println(match);
}
/**按名字分组*/
static void a3(){
Map<String, List<Achievement>> collect = getData().stream().collect(Collectors.groupingBy(a -> a.getName(), Collectors.toList()));
System.out.println(collect);
}
/**按属性去重*/
static void a4(){
ArrayList<Achievement> achievements = getData().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Achievement::getName))), ArrayList::new));
System.out.println(achievements);
}
/**List转Map*/
static void a5(){
Map<String, Achievement> mappings = getData().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Achievement::getName))), ArrayList::new)).stream().collect(Collectors.toMap(a -> a.getName(), a -> a));
System.out.println(mappings);
}
/**List转Map*/
static void a6(){
/*策略1:忽略后面的值*/
Map<String, Achievement> map = getData().stream().collect(Collectors.toMap(a -> a.getName(), a -> a, (x, y) -> x));
System.out.println(map);
/*策略2:使用集合接受重复的属性*/
Map<String, List<Achievement>> collect = getData().stream().collect(Collectors.toMap(a -> a.getName(), (Achievement a) -> {
List<Achievement> list = new ArrayList<>();
list.add(a);
return list;
}, (x, y) -> {
x.addAll(y);
return x;
}));
System.out.println(collect);
}
/**按多属性升序排序*/
static void a7(){
List<Achievement> collect = getData().stream().sorted(Comparator.comparing(Achievement::getName).thenComparing(Achievement::getScore).thenComparing(Achievement::getDate)).collect(Collectors.toList());
System.out.println(collect);
}
/**求分数和*/
static void a8(){
double score = getData().stream().mapToDouble(Achievement::getScore).reduce(0d, (x, y) -> x + y);
System.out.println(score);
}
/**分组求和*/
static void a9(){
Map<String, Double> collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName, Collectors.summingDouble(Achievement::getScore)));
System.out.println(collect);
}
/**求所有的名字*/
static void a10(){
Set<String> collect = getData().stream().collect(Collectors.mapping(Achievement::getName, Collectors.toSet()));
System.out.println(collect);
}
/**求及格的数据*/
static void a11(){
Map<Boolean, List<Achievement>> collect = getData().stream().collect(Collectors.partitioningBy(a -> a.getScore() >= 60));
System.out.println(collect);
}
网友评论