zoukankan      html  css  js  c++  java
  • java8特性

    List<Apple> appleList = new ArrayList<>();//存放apple对象集合

    1. List转Map

    id为key,apple对象为value,可以这么做:

    复制代码
    /**
     * List -> Map
     * 需要注意的是:
     * toMap 如果集合对象有重复的key,会报错Duplicate key ....
    * apple1,apple12的id都为1。 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2 */ Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
    复制代码

    打印appleMap:

    {1=Apple{id=1, name='苹果1', money=3.25, num=10}, 2=Apple{id=2, name='苹果2', money=2.89, num=30}, 3=Apple{id=3, name='苹果3', money=9.99, num=40}}

     2. 分组

    List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起:

    Map<Integer, List<User>> groupBy = appleList.stream().collect(Collectors.groupingBy(User::getId));

     3. 过滤filter

    从集合中过滤出来符合条件的元素:

    //过滤出符合条件的数据
    getSource()==5的数据
    List<AppointOrderDTO> filter3= filter.stream().filter(a -> a.getSource()==5).collect(Collectors.toList());
    
    

    4.去重 

    <--单条件-->
    List<ClassMember> list1 = list.stream().collect( Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>( Comparator.comparing(ClassMember::getStartTime))),ArrayList::new));

    <--多条件-->
    List<AppointOrderDTO> list2 = list1.stream().collect(
    Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(
    Comparator.comparing(a->a.getPayOrderDate()+";"+a.getAppointUserId()))), ArrayList::new));

    5.排序

    正序:
    sysLogDTOS.sort(Comparator.comparing(SysLogDTO::getDate));
    倒序
    sysLogDTOS = sysLogDTOS.stream().sorted(Comparator.comparing(SysLogDTO::getDate).reversed())
    .collect(Collectors.toList());

    6. 求和

    将集合中的数据按照某个属性求和:

    通过 sum() 方法实现(两种方式):

      

    public static void answer() {
        List<Student> students = initData();
        Double result = students.stream()
                .filter(one -> one.getScore() >= 60).map(o -> o.getScore())
                .reduce(0d, (a,b) -> (a + b));
        System.out.println(result);
    }

      

    public static void answerSecondImpl() {
            List<Student> students = initData();
            Double result = students.stream()
                    .filter(one -> one.getScore() >= 60).mapToDouble(o -> o.getScore()).sum();
            System.out.println(result);
    }

    BigDecimal:

    //计算 总金额
    BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
    System.err.println("totalMoney:"+totalMoney);  //totalMoney:17.48

    Integer:

    //计算 数量
    int sum = appleList.stream().mapToInt(Apple::getNum).sum();
    System.err.println("sum:"+sum);  //sum:100  
  • 相关阅读:
    Oracle 11g SQL Fundamentals Training Introduction02
    Chapter 05Reporting Aggregated data Using the Group Functions 01
    Chapter 01Restriicting Data Using The SQL SELECT Statemnt01
    Oracle 11g SQL Fundamentals Training Introduction01
    Chapter 04Using Conversion Functions and Conditional ExpressionsConditional Expressions
    Unix时代的开创者Ken Thompson (zz.is2120.bg57iv3)
    我心目中计算机软件科学最小必读书目 (zz.is2120)
    北京将评估分时分区单双号限行 推进错时上下班 (zz)
    佳能G系列领军相机G1X
    选购单反相机的新建议——心民谈宾得K5(转)
  • 原文地址:https://www.cnblogs.com/sun27/p/11126444.html
Copyright © 2011-2022 走看看