Stream操作

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

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

public class test {
    public static void main(String[] args) {
        //(1)Collection对象中的stream()和parallelStream()方法
        ArrayList<String> list = new ArrayList<>();
        list.add("zhangsan");
        list.add("zhangwuji");
        list.add("lisi");
        list.add("zhaoliu");
        Stream<String> stream = list.stream();
        //遍历
        stream.forEach(System.out::println);

        //(2)Arrays工具类的stream方法
        String[] strings = {"a","A","aaa"};
        Stream<String> stream2 = Arrays.stream(strings);
        stream2.forEach(System.out::println);

        //(3)Stream接口中的of iterate generate 方法
        Stream<Integer> stream3 = Stream.of(10, 20, 30, 40, 50);
        stream3.forEach(System.out::println);

        //迭代流
        Stream<Integer> iterate = Stream.iterate(0, x -> x + 2);
        iterate.limit(10).forEach(System.out::println);

        //生成流
        Stream<Integer> generate = Stream.generate(() -> new Random().nextInt(100));
        generate.limit(5).forEach(System.out::println);

        //(4)IntStream,LongStream,DoubleStream 的of、range、rangeClosed
        IntStream intStream = IntStream.of(10, 20, 30);
        intStream.forEach(System.out::println);

        IntStream range = IntStream.range(10, 20);
        range.forEach(System.out::println);

        ArrayList<Employee> list5 = new ArrayList<>();
        list5.add(new Employee("zhangsan",20000));
        list5.add(new Employee("zhangwuji",19000));
        list5.add(new Employee("lisi",15000));
        list5.add(new Employee("zhaoliu",13211));
        list5.add(new Employee("zhaoliu",13211));
        //filter过滤
        list5.stream()
                .filter(e->e.getName().startsWith("zhang"))
                .forEach(System.out::println);
        //limit限制
        list5.stream()
                .limit(2)
                .forEach(System.out::println);
        //skip跳过
        list5.stream()
                .skip(2)
                .forEach(System.out::println);
        //distinct去重
        list5.stream()
                .distinct()
                .forEach(System.out::println);
        //sorted排序
        list5.stream()
                .sorted(((o1, o2) -> o1.getMoney()-o2.getMoney()))
                .forEach(System.out::println);
        //map
        list5.stream()
                .map(Employee::getName)
                .forEach(System.out::println);
        //parallel 采用多线程
        list5.parallelStream()
                .forEach(System.out::println);

        //终止操作 foreach max min collect
        System.out.println("---------max-------------");
        Optional<Employee> max = list5.stream()
                .max((o1, o2) -> o1.getMoney() - o2.getMoney());
        System.out.println(max.get());
        System.out.println("---------min-------------");
        Optional<Employee> min = list5.stream()
                .min((o1, o2) -> o1.getMoney() - o2.getMoney());
        System.out.println(min.get());
        System.out.println("---------collect-------------");
        List<String> collect = list5.stream()
                .map(Employee::getName)
                .collect(Collectors.toList());
        System.out.println(collect);

    }
}

你可能感兴趣的:(java,stream,java,lambda)