Stream 不能被重用,一旦它被使用或使用,流将被关闭
一、创建流的方式
1、通过Collection接口方法:default Stream<E> stream()
Stream<String> stream = list.stream();
2、通过Stream接口
2.1 public static<T> Stream<T> of(T... values) 产生一个元素为给定值的流
String[] split = dbsetting.split("\PL+"); Stream<String> split1 = Stream.of(split);
2.2 public static<T> Stream<T> generate(Supplier<T> s) 产生一个无限流,它的值是通过反复调用函数s而构建的。
Stream.generate(Math::random).forEach(x-> System.out.println(x));
2.3 public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) 产生一个无限流,它的元素包含种子,在种子上调用f产生的值,在前一个元素上调用f产生的值
Stream.iterate(0, n -> n + 1).limit(10).forEach(x -> System.out.println(x));
3、通过Arrays类的方法:public static <T> Stream<T> stream(T[] array)
String[] split = dbsetting.split("\PL+"); Arrays.stream(split).forEach(x-> System.out.println(x));
4、通过Files类的方法 : public static Stream<String> lines(Path path, Charset cs)
Stream<String> lines1 = Files.lines(Paths.get("D://dbsetting.txt"),Charset.forName("GBK")); ines1.forEach(x-> System.out.println(x));
二、流转换产生新的流
1、Stream<T> filter(Predicate<? super T> predicate); 原元素与某种条件匹配 产生新元素
String[] split = dbsetting.split("\PL+"); Stream<String> stringStream = Arrays.stream(split).filter(x -> x.length() > 0);
2、public static <T> Stream<T> stream(T[] array) 按某种方式转换原元素的值来产生新元素
String[] split = dbsetting.split("\PL+"); Arrays.stream(split).map(String::toUpperCase).forEach(x-> System.out.println(x));
3、Stream<T> limit(long maxSize); limit方法返回一个新流,第n个元素结束
Stream<T> skip(long n); 丢弃前n个元素
Stream.iterate(0, n -> n + 1).limit(10).forEach(x -> System.out.println(x));
4、public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) 流拼接
String[] array = {"a", "b", "c"}; String[] array2 = {"d", "e"}; Stream.concat(Arrays.stream(array), Stream.of(array2)).forEach(x-> System.out.println(x));
5、Stream<T> distinct(); 剔除重复元素
Stream.of("aa","nn","cc","dd","aa","bb").distinct().forEach(x-> System.out.println(x));
6、Stream<T> sorted(); 排序
Stream.of("aa","nnn","c","dd","aaaa","bb").sorted().forEach(x-> System.out.println(x)); Stream.of("aa","nnn","c","dd","aaaa","bb").sorted(Comparator.comparing(String::length)).forEach(x-> System.out.println(x));
7、Stream<T> peek(Consumer<? super T> action); 每次获取元素时都会调用一个函数
Object[] names = Stream.of("aa", "bb", "cc").peek(x -> System.out.println("获取元素:" + x)).limit(2).toArray();
三、简单简约
boolean anyMatch = Stream.of("aa", "bb", "cc", "a").anyMatch(x -> x.length() < 1); boolean allMatch = Stream.of("aa", "bb", "cc", "a").allMatch(x -> x.length() > 1); boolean noneMatch = Stream.of("aa", "bb", "cc", "a").noneMatch(x -> x.length() > 2); System.out.println(anyMatch); System.out.println(allMatch); System.out.println(noneMatch); Optional<String> max = Stream.of("aa", "bb", "cc", "a","aad").max(Comparator.comparing(String::length)); System.out.println("max:"+max.get()); Optional<String> min = Stream.of("aa", "bb", "cc", "a","aad").min(Comparator.comparing(String::length)); System.out.println("min:"+min.get()); Optional<String> any = Stream.of("aa", "bb", "cc", "a", "aad").findAny(); Optional<String> first = Stream.of("aa", "bb", "cc", "a", "aad").findFirst(); System.out.println("any:" + any.get()); System.out.println("first:" + first.get());
四、收集结果
Object[] objects = Stream.of("aa", "bb", "cc", "a").toArray(); List<String> collect = Stream.of("aa", "bb", "cc", "a").collect(Collectors.toList()); Set<String> collect1 = Stream.of("aa", "bb", "cc", "a").collect(Collectors.toSet()); TreeSet<String> collect2 = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toCollection(TreeSet::new)); String collect3 = Stream.of("aa", "bb", "cc", "a").collect(Collectors.joining("---")); Employee wxh = new Employee("wxh", 5345.2); Employee www = new Employee("www", 1345.2); Employee aaa = new Employee("aaa2", 2345.2); Employee aaa2 = new Employee("aaa1", 2345.2); List<Employee> employees = Stream.of(wxh, www, aaa, aaa2).collect(Collectors.toList()); Map<String, Double> employeesSalary = employees.stream().collect(Collectors.toMap(Employee::getName, Employee::getSalary)); Map<String, Employee> collect4 = employees.stream().collect(Collectors.toMap(Employee::getName, e -> e, (o, n) -> n)); TreeMap<String, Employee> collect5 = employees.stream().collect(Collectors.toMap(x->x.getName(), e -> e, (o, n) -> n, TreeMap::new));
五、分组
Locale[] availableLocales = Locale.getAvailableLocales(); //groupingBy 根据国家分组 Map<String, List<Locale>> collect1 = Arrays.stream(availableLocales).collect(groupingBy(Locale::getCountry)); //groupingBy toset 根据国家分组,分组用set类型 Map<String, Set<Locale>> collect = Arrays.stream(availableLocales).collect(Collectors.groupingBy(Locale::getCountry, toSet())); //counting() 对分组后的元素计数 Map<String, Long> collect2 = Arrays.stream(availableLocales).collect(groupingBy(Locale::getCountry, counting())); Employee wxh = new Employee("wxh", 1111,"salary"); Employee www = new Employee("www", 2222,"salary"); Employee aaa = new Employee("aaa", 3333,"salary"); Employee aaa2 = new Employee("aaa", 2222,"bonus"); Employee www2 = new Employee("www", 5555,"bonus"); //summingDouble 对分组后的元素进行计算然后返回计算结果 Map<String, Double> collect3 = Stream.of(wxh, www, aaa, aaa2, www2).collect(groupingBy(Employee::getName, summingDouble(Employee::getSalary))); //maxBy 对分组后的元素计算最大值,返回最大值对应的元素 Map<String, Optional<Employee>> collect4 = Stream.of(wxh, www, aaa, aaa2, www2).collect( groupingBy(Employee::getName, maxBy(Comparator.comparing(Employee::getSalary)))); //mapping Map<String, Optional<String>> collect5 = Stream.of(wxh, www, aaa, aaa2, www2).collect(groupingBy(Employee::getName, mapping(Employee::getSalaryclass, maxBy(Comparator.comparing(String::length,Comparator.reverseOrder()))))); Map<String, Set<Double>> collect6 = Stream.of(wxh, www, aaa, aaa2, www2).collect(groupingBy(Employee::getName, mapping(Employee::getSalary, toSet()))); Map<String, String> collect7 = Stream.of(wxh, www, aaa, aaa2, www2).collect(groupingBy(Employee::getName, mapping(Employee::getSalaryclass, joining(","))));