最近研究了一下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);
} }