• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java,Lambda,Stream实例

java 来源:二十六画生的博客 2次浏览

package com.main;

import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        // write your code here
        List<String> list = Arrays.asList("a", "", "a", "cde", "fg", "h");
        System.out.println(list.stream().count());//6
        list.stream().distinct().forEach(System.out::print);//acdefgh
        System.out.println();
        list.stream().filter(string -> !string.isEmpty()).forEach(System.out::print);//aacdefgh
        System.out.println();
        List<String> filteredList = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());//非空的字符串元素,转换为List
        System.out.println(filteredList);//[a, a, cde, fg, h]
//        String filteredString = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(","));//非空的字符串元素,转换为字符串
//        System.out.println(filteredString);//a,a,cde,fg,h
        String filteredString = list.stream().filter(string -> string.indexOf('a') == -1).collect(Collectors.joining(","));//不包含a的字符串元素
        System.out.println(filteredString);//,cde,fg,h

        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 4, 13, 4, 15, 6, 17, 8, 19);
//        List<Integer> filter = integers.stream().filter(integer -> integer > 2).distinct().collect(Collectors.toList());//大于2并且不重复的元素
//        System.out.println(filter);//[3, 4, 13, 15, 6, 17, 8, 19]
        List<Integer> filter = integers.stream().filter(integer -> integer > 2).distinct().map(integer -> integer * integer).collect(Collectors.toList());//大于2并且不重复的元素再平方
        System.out.println(filter);//[9, 16, 169, 225, 36, 289, 64, 361]

//        filter = filter.stream().sorted().collect(Collectors.toList());//排序,升序
//        System.out.println(filter);//[9, 16, 36, 64, 169, 225, 289, 361]

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("jdk1.7 匿名内部类");
            }
        }).run();

        new Thread(() -> {
            System.out.println("jdk1.8 Lambda表达式");
        }).run();

//        Collections.sort(integers);//jdk1.8的方法排序
//        System.out.println(filter);//[9, 16, 169, 225, 36, 289, 64, 361]

        List<User> userList = new ArrayList<>();
        userList.add(new User(3, "Tom3"));
        userList.add(new User(2, "Tom2"));
        userList.add(new User(1, "Tom1"));
//        userList.stream().sorted((User o1, User o2) ->
//                o1.getId().compareTo(o2.getId())
//        );//这个排序无效,不用stream()
//        userList.forEach(user -> System.out.print(user.getId() + ","));//3,2,1,
//        System.out.println();
//
//        userList.sort((User o1, User o2) ->
//                o1.getId().compareTo(o2.getId())
//        );//jdk1.8的方法排序
//        userList.forEach(user -> System.out.print(user.getId() + ","));//1,2,3,
//        System.out.println();

        Collections.sort(userList, new Comparator<User>() {//jdk1.7的方法排序
            @Override
            public int compare(User o1, User o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });
        userList.forEach(user -> System.out.print(user.getId() + ","));//1,2,3,
        System.out.println();

        Collections.sort(filter, new Comparator<Integer>() {//jdk1.7的方法排序
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println(filter);//[9, 16, 36, 64, 169, 225, 289, 361]

//        filter.sort((Integer o1,Integer o2)->{
//           return o1.compareTo(o2);
//        });//jdk1.8的方法排序
//        System.out.println(filter);//[9, 16, 36, 64, 169, 225, 289, 361]

        Collections.sort(integers, new Comparator<Integer>() {//jdk1.7的方法排序
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println(integers);//[1, 2, 3, 4, 4, 4, 6, 8, 13, 15, 17, 19]

        integers.sort((Integer o1, Integer o2) -> {
            return o1.compareTo(o2);
        });//jdk1.8的方法排序.有{}return和;
        System.out.println(integers);//[1, 2, 3, 4, 4, 4, 6, 8, 13, 15, 17, 19]

        integers.sort((Integer o1, Integer o2) ->
                o1.compareTo(o2)
        );//jdk1.8的方法排序.没有{}return和;
        System.out.println(integers);//[1, 2, 3, 4, 4, 4, 6, 8, 13, 15, 17, 19]

    }
}

class User {
    Integer id;
    String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

注意以上这些提示:所有的匿名内部类都可以用Lambda表达式和方法引用代替。需要至少是jdk1.8才行。

参考:http://www.runoob.com/java/java8-new-features.html


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)