java8学习的一点总结

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

}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值