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

  

原文地址:https://www.cnblogs.com/418836844qqcom/p/11672694.html