java8-lambda操作数组、集合

简介

         “Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

基本语句

Runnable noArguments = () -> System.out.println("Hello World"); 
ActionListener oneArgument = event -> System.out.println("button clicked"); 
Runnable multiStatement = () -> { 
    System.out.print("Hello");
    System.out.println(" World");
};
BinaryOperator<Long> add = (x, y) -> x + y; q
BinaryOperator<Long> addExplicit = (Long x, Long y) -> x + y; 

编写线程

Runnable runnable = () -> {
    System.out.println("Lambda 创建线程");
};
Thread thread = new Thread(runnable);
thread.start();

利用Lambda运算

BinaryOperator<Integer> add = (x, y) -> x + y;

Integer x = add.apply(20, 30);

统计数组中某元素出现的个数

String name[] = {"张三", "李四", "王五", "孙刘", "赵强", "李明", "赵强", "汪汪"};
long num = Arrays.stream(name).filter(x -> x.equals("赵强")).count();

数组去重并转化成集合存储

String name[] = {"张三", "李四", "王五", "孙刘", "赵强", "李明", "赵强", "汪汪"};
List<String> stringList = Arrays.stream(name).filter(x -> !x.equals("赵强")).collect(toList());

数组去重,并对元素加后缀

String name[] = {"张三", "李四", "王五", "孙刘", "赵强", "李明", "赵强", "汪汪"};
List<String> stringList2 = Arrays.stream(name).filter(x -> !x.equals("赵强")).map(y -> y + "加后缀:").collect(toList());

数组求和

        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 9, 10 };
        int count = 0;
        // for循环
        for (int i = 0; i < a.length; i++) {
            count += a[i];
        }
        // foreach
        count = 0;
        for (int i = 0; i < a.length; i++) {
            count += a[i];
        }
        // Lambda
        int num = Arrays.stream(a).reduce(0, (b, c) -> b + c);

Lambda  的提取对象字段,并拼接

class User{

    private String username;
    private int age;

    public User(){}

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

List<User> userList = asList(new User("张三" , 10) , new User("李四" , 10) , new User("王五" , 13));
String result = userList.stream().map(User::getUsername).collect(Collectors.joining("," , "[" , "]"));

输出结果:[张三,李四,王五]

Lambda  groupBy

        List<User> userList = asList(new User("张三" , 10) , new User("李四" , 10) , new User("王五" , 13));
        Stream<User> userStream = userList.stream();
        Map<Integer , List<String>> integerStringMap = userStream.collect(groupingBy(user->user.getAge() , mapping(User::getUsername , toList())));
        System.out.println(integerStringMap.toString());

输出结果:{10=[张三, 李四], 13=[王五]}

String result2 =
                userList.stream().map(User::getUsername)
                        .reduce(new StringJoiner(",1 ", "[", "]"),
                                StringJoiner::add,
                                StringJoiner::merge)
                        .toString();
System.out.println(result2);

输出结果:[张三,1 李四,1 王五]

    原文作者:java_小斌
    原文地址: https://blog.csdn.net/love3765/article/details/69385765
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞