• Lambda表达式使用demo


    项目的过程中经常会接触到 lambda 表达式,随后发现它基本上可以替代所有 for 循环,包括增强for循环。也就是我认为,绝大部分的for循环都可以用 lambda 表达式改写。

    lambda表达式有它自己的优点:(1)简洁,(2)易并行计算。尤其适用于遍历结果,循环计算数值或者赋值的时候非常方便。

    缺点: (1)若不用并行计算,很多时候计算速度没有比传统的 for 循环快。

       (2)不容易使用debug模式调试。

       (3)在 lambda 语句中直接强制类型转换不方便。

       (4)不可以在foreach中修改foreach外面的值。

    下面是一些我再开发过程中经常使用过的表达式去处理list集合。

    (1)先新建一个实体类

    public class Person {
    
        private String name;
        private int age;
        private int size;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getSize() {
            return size;
        }
        public void setSize(int size) {
            this.size = size;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", size=" + size + "]";
        }
        public Person(String name, int age, int size) {
            super();
            this.name = name;
            this.age = age;
            this.size = size;
        }
        public Person() {
            super();
            // TODO Auto-generated constructor stub
        }
        
    }

    (2)写主函数。剩下的说明会写在代码中

    public class Jdk8Main {
    
        public static void main(String[] args) {
            List<Person> list = new ArrayList<>();
            Person p1 = new Person("张1",1,1);
            Person p101 = new Person("张101",101,101);
            Person p2 = new Person("张2",2,2);
            Person p3 = new Person("张3",3,3);
            Person p4 = new Person("张4",4,4);
            Person p5 = new Person("张5",5,5);
            Person p6 = new Person("张6",6,6);
            list.add(p1);
            list.add(p2);
            list.add(p3);
            list.add(p4);
            list.add(p5);
            list.add(p6);
            list.add(p101);
            
            /**
             * 1.forEach()进行遍历集合
             *    item:可以是任意值。类似于for循环中的循环值
             */
            list.forEach(item->{
                //设置值
                item.setName(item.getName()+"测试");;
                //输出语句
                System.out.println(item.toString());
            });
            
            /**
             * 2.stream()流操作
             */
            //2.1. 去重 distinct() 去重;collect(Collectors.toList())。封装成集合
            List<Person> distinctList = list.stream().distinct().collect(Collectors.toList());
            //2.2 排序  sorted((第一个对象,第二个对象)->返回值)  (升降序看是第几个对象与第几个对象比较)
            List<Person> sortedList = list.stream().sorted((o1,o2)->o1.getAge()-o2.getAge()).collect(Collectors.toList());
            //2.3 过滤 , filter(item->{})   item为每一项。 按照自己的需求来筛选list中的数据
            List<Person> filterList = list.stream().filter(item->item.getAge()>3).collect(Collectors.toList());
            //2.4 map(), 提取对象中的某一元素.  用每一项来获得属性(也可以直接用  对象::get属性())
            List<String> mapList1 = list.stream().map(Person::getName).collect(Collectors.toList());
            List<String> mapList2 = list.stream().map(item->item.getName()).collect(Collectors.toList());
            //2.5 统计 sum() 。mapToDouble() 转换成double。还有其他类型转换。可以自己研究。
            //           max(),min(),average()
            double sum = list.stream().mapToDouble(Person::getAge).sum();
            //2.6 分组   Collectors.groupingBy(属性名)
            Map<Integer, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getAge));
            //2.7 多重分组 Collectors.groupingBy(属性,Collectors.groupingBy(属性))
            Map<String, Map<Integer, List<Person>>> map2 = list.stream().collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge())));
            //2.8 分组并计算综合        Collectors.summarizingLong()
            Map<String, Map<Integer, LongSummaryStatistics>> map3 = list.stream().collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge(),Collectors.summarizingLong(Person::getSize))));
            
            /**
             *  3.  集合比较的简写方式
             */
            list.sort((o1,o2)->{return o1.getAge()-o2.getAge();});
        }
    }


    原文链接:https://www.cnblogs.com/JiangLai/p/9954724.html

  • 相关阅读:
    array_diff()和array_diff_assoc()
    React出现错误:Uncaught TypeError: this.setState is not a function
    predis操作大全
    MacOS下出现-bash: 命令: command not found的解决方法
    OnCreateClient学习总结
    MFC之CSingleDocTemplate构造函数
    CString 的成员函数详解
    MFC 中Invalidate的使用
    CFileFind类的使用总结(转)
    MFC CSplitterWnd的用法
  • 原文地址:https://www.cnblogs.com/superming/p/12694090.html
Copyright © 2020-2023  润新知