- list.stream()对某一属性计算总和
计算 Person 年龄总和的三种方式:
->summintInt():
int sum = list.stream().collect(summingInt(Person::getAge));
->mapToInt().sum()
int sum = list.stream().mapToInt(Person::getAge).sum();
->map().reduce(sum())
int sum = list.stream().map(Person::getAge).reduce(Interger::sum).get();
注:Long,Double同理
- list.stream()对list汇总操作
->counting()
long l = list.stream().collect(counting());
->count()
long l = list.stream().count();
- list.stream()对list求平均值
->averagingInt()
Double average = list.stream().collect(averagingInt(Person::getAge));
->mapToInt().average()
OptionalDouble average = list.stream().mapToInt(Person::getAge).average();
- list.stream()对list取最大(小)值
->maxBy(comparing()),minBy(comparing())
Optional<Person> optional = list.stream().collect(maxBy(comparing(Person::getAge)));
->max(comparing()),min(comparing())
Optional<Person> optional = list.stream().max(comparing(Person::getAge));
- list.strem()对流里面的字符串元素进行连接,其底层实现用的是StringBuilder
String s = list.stream().map(Person::getName).collect(joining());
String s = list.stream().map(Person::getName).collect(joining(","));
->joining()重载方法
String s = list.stream().map(Person::getName).collect(joining("A","B","C"));
//以A开头,以C结尾,B用于连接各个字符串
- list.stream()对流数据进行分组,最终返回一个Map类型
Map<String, List<Person>> map = list.stream().collect(groupingBy(Person::getSex));
//按照性别分组
->多级分组
Map<Integer, Integer> map = list.stream().collect(groupingBy(Person::getAge, summingInt(Person::getAge)));
->按组求和
Map<Integer, Integer> map = list.stream().collect(groupingBy(Person::getAge, summingInt(Person::getAge)));
//计算每一组的年龄之和
- list.stream()对BigDecimal求和
BigDecimal sum = list.stream().map(Person::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add);
- list.stream()转map
注意:用Collectors的toMap方法转换List,一般会遇到两个问题。一个是转换map,key重复问题;另一个是空指针异常,即转为map的value是null。
Map<String, List<String>> map = list.stream().collect(Collectors.toMap(Person::getId,
p -> {
List<String> getNameList = new ArrayList<>();
getNameList.add(p.getName()); return getNameList;
},
(List<String> value1, List<String> value2) -> {
value1.addAll(value2); return value1;
}
))
System.out.println(map);
- 根据Map的key排序
public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {
Map<K, V> result = Maps.newLinkedHashMap();
if (isDesc) {
map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
} else {
map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
}
return result;
}
- 根据Map的value排序
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean isDesc) {
Map<K, V> result = Maps.newLinkedHashMap();
if (isDesc) {
map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed())
.forEach(e -> result.put(e.getKey(), e.getValue()));
} else {
map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
}
return result;
}