1、Strem函式
List<People> list = new ArrayList<>();
//根據age對list分組,得到map
Map<String,List<People>> groupByAge=list.stream().collect(Collectors.groupingBy(People::getAge));
//根據age進行排序(reserve倒序)得到排序后的List
List<People> peopleListSorted = list.stream().sorted(Comparator.comparing(People::getAge).reversed()).collect(Collectors.toList());
//提取age,去重后排序
List<String> ageList = list.stream().map(People::getAge).distinct().sorted().collect(Collectors.toList());
//提取年齡大于20的people
List<People> olderThan20 = list.stream().filter(e->Integer.parseInt(e.getAge()) > 20).collect(Collectors.toList());
//累加List中的money
BigDecimal totalMoney = list.stream().map(People::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//查詢
People people = list.stream().filter(e->e.getAge().equals("20")).findFirst().orElse(null);
//List<People> -> Map<String,People> (name,people)
//物件集合轉化為map
Map<String,People> map = list.stream().collect(Collectors.toMap(People::getName,n->n));
//查找流中最大值和最小值
List<Person> personList = generatePersonList();
Person olderOne = personList.stream().max(Comparator.comparing(Person::getAge)).orElse(null);
Person youngerOne = personList.stream().min(Comparator.comparing(Person::getAge)).orElse(null);
//物件去重
ArrayList<Person> collect = personList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection
(() -> new TreeSet<>(Comparator.comparingInt(Person::getAge))),ArrayList::new)
);
2、optinal判斷空處理
對于深度嵌套的陳述句,可能需要多次判空,才能保證代碼的健壯性,但是用if來實作,會有一堆的if陳述句,java8通過optinal比較優雅的解決了這個問題,
舉個例子:
String isocode = user.getAddress().getCountry().getIsocode().toUpperCase();
通常if判斷的做法
if (user != null) {
Address address = user.getAddress();
if (address != null) {
Country country = address.getCountry();
if (country != null) {
String isocode = country.getIsocode();
if (isocode != null) {
isocode = isocode.toUpperCase();
}
}
}
}
Optional的做法
String isocode = Optional.ofNullable(user)
.map(User::getAddress)
.map(Address::getCountry)
.map(Country::getIsocode)
.orElse("default");
3、Stream的groupby
group by生成一個擁有分組功能的Collector,有三個多載方法
- 需要一個引數:按照該引數進行分組,結果回傳一個Map集合,每個Map的key默認是分組引數的型別,value是一個List集合,
Map <String,List < User >> collect =
users.stream().collect(Collectors.groupingBy(User: :getEdu));
- 需要兩個引數:第二引數是Collector型別,可以對value進行處理,
可以對結果進行映射
Map <String,List <Integer>> collect =
users.stream().collect(
Collectors.groupingBy(User: :getEdu,
//第二個引數對Map的value進行處理(映射)
Collectors.mapping(User: :getId, Collectors.toList()))
);
可以對結果進行求和
Map <String,Double> collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
//對引數進行累計求和
Collectors.summingDouble(User: :getPrice))
);
可以對結果進行統計
Map < String,Long > collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
//獲取count數量
Collectors.counting())
);
- 需要三個引數,第三個引數添加了對結果Map的生成方式,默認是HashMap
Map <String,Double > collect = users.stream().collect(
Collectors.groupingBy(User: :getEdu,
//決定map的生成方式,使用TreeMap
TreeMap: :new,
//對引數進行累計求和
Collectors.summingDouble(User: :getPrice))
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379526.html
標籤:其他
上一篇:性能測驗 理論初探,什么是性能測驗?性能測驗的目的是?
下一篇:selenium自動化測驗03
