zoukankan      html  css  js  c++  java
  • lambda使用

     public static void main(String[] args) {
            Map<String, Object> map = new HashMap<>();
            map.put("1", "111");
            map.put("2", "2222");
            //1.需求:打印输出已经排好序的字符串数组中长度大于20 ,并且为其添加hello ;
            //数据源
            String[] players = {"Rafael Nadal", "Novak Djokovic",
                    "Stanislas Wawrinka", "David Ferrer",
                    "Roger Federer", "Andy Murray",
                    "Tomas Berdych", "Juan Martin Del Potro",
                    "Richard Gasquet", "John Isner"};
            //排序
            Arrays.sort(players);
            //转化为list操作
            List<String> list = Arrays.asList(players);
            //过滤长度大于20 ,并为其添加hello 输出
    
            list.stream().filter(a -> a.startsWith("J")).map(a -> a += " hello").forEach(player -> System.out.println(player));
            list.stream().filter(a -> a.length() > 20).map(a -> a += " hello").forEach(player -> System.out.println(player));
            //2.需求:对多个对象,按照年龄进行排序输出:
            List<Student> peopleList = new ArrayList<>();
            peopleList.add(new Student("a", 17));
            peopleList.add(new Student("b", 16));
            peopleList.add(new Student("c", 19));
            peopleList.add(new Student("d", 15));
            //按照年龄排序:
            Collections.sort(peopleList, (Student a, Student b) -> a.getAge().compareTo(b.getAge()));
            peopleList.stream().forEach(a -> System.out.println(a.getAge()));
        }
    

      

  • 相关阅读:
    day03 字符串
    day02 运算符和编码
    day01 初识Python
    windows 安装yaml支持和pytest支持等
    Python自动补全缩写意义
    关于python接口测试connect error
    关于Python的post请求报504错误
    python函数参数*args **kwargs
    利用Python语言Appium启动ios app
    shell 中| 用法
  • 原文地址:https://www.cnblogs.com/418836844qqcom/p/11672694.html
Copyright © 2011-2022 走看看