java8學習的一點總結
阿新 • • 發佈:2018-11-25
最近研究了一下java8 弄了幾個例子學習了一下用法:
建立了一個實體類:
@Data public class Apple { private Integer id; private String name; private BigDecimal money; private Integer num; public Apple(Integer id, String name, BigDecimal money, Integer num) { this.id = id; this.name = name; this.money = money; this.num = num; } }
以下是具體的例子:
import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * java 8 流工作模式 * * @author bzs on 2018/11/1. */ public class Test { public static void main(String[] args) { List<Apple> appleList = new ArrayList<>();//存放apple物件集合 Apple apple1 = new Apple(1, "蘋果1", new BigDecimal("3.25"), 10); Apple apple12 = new Apple(1, "蘋果2", new BigDecimal("1.35"), 20); Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30); Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40); appleList.add(apple1); appleList.add(apple12); appleList.add(apple2); appleList.add(apple3); //1、list轉map,value 值是一個物件但是可以取其中一個物件 Map<Integer, Apple> appleMapOnly = appleList.stream() .collect(Collectors.toMap(Apple::getId, apple -> apple, (value1, value2) -> value1)); System.out.println(appleMapOnly.toString()); System.out.println("===================================="); //1、list轉map,value 值是一個物件 Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getNum, apple -> apple)); System.out.println(appleMap.toString()); System.out.println("-------------------------------------"); //1、list轉map,value 值是一個集合 Map<Integer, List<Apple>> appleMapList = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); System.out.println(appleMapList.toString()); System.out.println("也可以這樣,第二種寫法適合集合物件轉換的情況*****************"); //2、分組,value 值是一個集合 Map<Integer, List<Apple>> appleMapListMoney = appleList.stream() .collect(Collectors.groupingBy(Apple::getId, Collectors.mapping(apple -> apple, Collectors.toList()))); System.out.println(appleMapListMoney.toString()); System.out.println("-------------------------------------"); //3、filter 過濾器 List<Apple> filterList = appleList.stream().filter(apple -> apple.getName().equals("香蕉")) .collect(Collectors.toList()); System.out.println(filterList.toString()); System.out.println("-------------------------------------"); //4、求和 //計算 總金額 BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); System.err.println("totalMoney:" + totalMoney); //totalMoney:17.48 //計算 數量 int sum = appleList.stream().mapToInt(Apple::getNum).sum(); System.err.println("sum:" + sum); //sum:100 System.out.println("-------------------------------------"); //5.獲取所有的id集合(去重以後的) Set<Integer> idSet = appleList.stream().map(a -> a.getId()).collect(Collectors.toSet()); System.out.println(new ArrayList<>(idSet)); System.out.println("-------------------------------------"); //6.排序 List<Apple> sortList = appleList.stream().sorted(Comparator.comparing(Apple::getId).reversed()) .collect(Collectors.toList()); System.out.println(sortList.toString()); List<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3); List<Integer> list2 = new ArrayList<>(); list2.add(3); list2.add(4); list2.add(5); System.out.println("====求交集==="); List<Integer> list = list1.stream().filter(t -> list2.contains(t)).collect(Collectors.toList()); list.stream().forEach(System.out::println); System.out.println("====求差集==="); list = list1.stream().filter(t -> !list2.contains(t)).collect(Collectors.toList()); list.stream().forEach(System.out::println); System.out.println("====求並集==="); list.addAll(list1); list.addAll(list2); list = list.stream().distinct().collect(Collectors.toList()); list.stream().forEach(System.out::println); } }