java,Stream,filter,sorted,map用法

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Double[] arr = new Double[]{78.0, -0.56, 1.23, 0.0, 12.2, 123.6};//正确
        List<Double> list = Arrays.asList(arr);

        list.stream().filter(element -> element > 0).forEach(System.out::println);//输出大于0的元素
        System.out.println("1-----------");
        list.stream().filter(element -> element > 0).map(elemnt -> elemnt * 2)
                .forEach(System.out::println);//大于0的元素*2,再输出
        System.out.println("2-----------");
        list.stream().filter(element -> element > 0).sorted().map(element -> element * 2).limit(3)
                .forEach(System.out::println);//大于0的元素,排序后, *2,取前三个,再输出
        System.out.println("3-----------");
        list.stream().sorted().filter(element -> element > 0).map(element -> element * 2).limit(3)
                .forEach(System.out::println);//元素排序,大于0的元素, *2,取前三个,再输出
        System.out.println("4-----------");
        List<Double> result = list.stream().filter(element -> element > 0).sorted().map(element -> element * 2)
                .limit(2).collect(Collectors.toList());//大于0的元素,排序后, *2,取前2个,封装成list
        System.out.println(result);
        System.out.println("5-----------");
        System.out.println(list);
    }
}

/*78.0
1.23
12.2
123.6
1-----------
156.0
2.46
24.4
247.2
2-----------
2.46
24.4
156.0
3-----------
2.46
24.4
156.0
4-----------
[2.46, 24.4]
5-----------
[78.0, -0.56, 1.23, 0.0, 12.2, 123.6]*/

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

class User {
    private Integer age;
    private String name;

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

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class Main {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        List<User> userList = new ArrayList<>();
        userList.add(new User(3, "Tom"));
        userList.add(new User(2, "Ana"));
        userList.add(new User(99, "Bob"));
        userList.add(new User(1, "Lyly"));
        userList.add(new User(4, "Lyly"));

//        正确,对象映射成名字
//        List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList());//[Tom, Ana, Bob, Lyly, Lyly]

//      正确,对象映射成名字
        List<String> nameList = userList.stream().map(user -> user.getName()).collect(Collectors.toList());//[Tom, Ana, Bob, Lyly, Lyly]

        //错误
        List<String> nameList = userList.stream().forEach(user -> user.getName()).collect(Collectors.toList());

        System.out.println(nameList);
    }
}

 

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

    原文作者:二十六画生的博客
    原文地址: https://blog.csdn.net/u010002184/article/details/81330654
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞