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()));
        }
    

      

  • 相关阅读:
    数组
    循环(二)
    循环——笔记
    C#基础(四)
    C#基础(三)
    安卓帮助文档
    增加线程异步发送消息的方法二(Runnable)
    增加线程异步发送消息的方法一(Thread)
    获取下拉框的值
    获取表中字段最大值,并且保存在前台页面中
  • 原文地址:https://www.cnblogs.com/418836844qqcom/p/11672694.html
Copyright © 2011-2022 走看看