Java 8 Stream介绍及使用2
(原)
stream中另一些比较常用的方法.
1、
public static<T> Stream<T> generate(Supplier<T> s)
通过generate返回一个Stream对象,接收一个 Supplier函数
例如:
Stream stream8 = Stream.generate(UUID.randomUUID()::toString);
2、
Optional<T> findFirst();
This is a short-circuiting terminal operation
这是一个终止操作,返回一个Optional,为什么是Optional而不是一个对象T呢?因为Stream中可能会有空的情况,就像这样Stream stream8 = Stream.empty();
如果是这样,那么findFirst()可能会返回null值,所以才用了Optional来规避空值的情况。
例如:
Stream stream8 = Stream.generate(UUID.randomUUID()::toString);
stream8.findFirst().ifPresent(System.out::println);
3、
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
Returns an infinite sequential ordered {@code Stream} produced by iterative
application of a function {@code f} to an initial element {@code seed}
producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
*{@code f(f(seed))}, etc.
用这个种子元素seed,通过迭代f,返回一个无限的串行的有序的流元素,
最后的stream的结果是一个seed ,f(seed),f(f(seed)) … 的结果。
由于这个方法是无限循环的,所以往往会跟limit终止操作配合使用。
它的第2个参数UnaryOperator是这样定义的:
public interface UnaryOperator<T> extends Function<T, T>
如果它的apply方法接收参数和返回参数是同一类型,是一个一元的Function函数
例如:
Stream.iterate(1,i -> i + 2).limit(3).forEach(System.out::println);
返回的结果将是1、3、5。
4、
Stream<T> limit(long maxSize);
Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length
返回一个长度不超过给定参数maxSize大小的stream
Stream<T> skip(long n);
Returns a stream consisting of the remaining elements of this stream
after discarding the first {@code n} elements of the stream
返回一个丢弃了前n个元素之后的stream。
例如:用iterate生成一个从1开始往后加1的stream,先限定这个stream长度为5,再忽略前2个,取后3个元素输出。
Stream.iterate(1, i -> i + 1).limit(5).skip(2).forEach(System.out::println);
5、
Stream<T> distinct()
没啥说的,去重用的。
例如:
IntStream.of(1,1,3,4,5,6,4).distinct().forEach(System.out::println);
返回1,3,4,5,6
6 、
Optional<T> max(Comparator<? super T> comparator)
Optional<T> min(Comparator<? super T> comparator)
求最大最小值,一般而言,如果是数字可用IntStream,IntStream中的求最大值,最小值是不带参数的,max(),min(),另外还提供了一个sum()求和方法。
如果IntStream是一个空对象,那么sum可以返回0,而min,max是一个null , 所以这里sum的返回值用的是int,而min和max返回值是OptionalInt,用于规避值为null的情况,所以一般可以这么写.
IntStream.of(1,2,3,4,5).max().ifPresent(System.out::println);
针对于IntStream的统计方法,它还额外提供了一个方法:
IntSummaryStatistics summaryStatistics()
IntSummaryStatistics 这个类,它也提供了数值统计的常用方法,如count,sum,min,max ,average等
例如:
IntSummaryStatistics intSummaryStatistics = IntStream.of(1, 2, 3, 4, 5).summaryStatistics();
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getAverage());
使用场景:
1、将stream元素去重
Stream<String> stream1 = Stream.of("happy new", "new year", "happy year","new happy");
stream1.map(item -> item.split(" ")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()).forEach(System.out::println);
2、如何将二个集合中的元素做笛卡尔集,交叉拼接
List<String> list1 = Arrays.asList("hello","world","today"); List<String> list2 = Arrays.asList("a","b","c"); list1.stream().flatMap(item -> list2.stream().map(item2 -> item + " " + item2)).collect(Collectors.toList()).forEach(System.out::println);
3、如果有一个对象Student,包含二个属性,name和age,在一个student集合中如何分组。
class Student{ private String name; private Integer age; public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
Student s1 = new Student("zhangsan", 30); Student s2 = new Student("lisi", 26); Student s3 = new Student("wangwu",30); Student s4 = new Student("lisi",22);
//根据姓名分组
List<Student> list3 = Arrays.asList(s1, s2, s3, s4); Map<String,List<Student>> map3 = list3.stream().collect(Collectors.groupingBy(Student::getName)); map3.forEach((key,value) -> System.out.println(key+":"+value));
//查看根据姓名分组后每组多少数量
List<Student> list4 = Arrays.asList(s1, s2, s3, s4); Map<String,Long> map4 = list4.stream().collect(Collectors.groupingBy(Student::getName,Collectors.counting())); map4.forEach((key,value) -> System.out.println(key+":"+value));
//查看根据姓名分组后,每个组平均年龄
List<Student> list5 = Arrays.asList(s1, s2, s3, s4); Map<String,Double> map5 = list5.stream().collect(Collectors.groupingBy(Student::getName,Collectors.averagingDouble(Student::getAge))); map5.forEach((key,value) -> System.out.println(key+":"+value));
//查看根据年龄分区,25岁以上为一个区,分区类似于分组,但是只有二个组,一个true,一个false,
List<Student> list6 = Arrays.asList(s1, s2, s3, s4); Map<Boolean,List<Student>> map6 = list6.stream().collect(Collectors.partitioningBy(s -> s.getAge() > 25)); map6.forEach((key,value) -> System.out.println(key+":"+value));
例子请看这里:https://github.com/LeeScofield/java8