廖雪峰Java16函数式编程-2Stream-7其他操作

1. 排序

Stream<T> sorted(); //按元素默认大小排序(必须实现Comparable接口)
Stream<T> sorted(Comparator<? super T> cp); //按指定Comparator比较的结果排序
    public static void main(String[] args){
        Stream<String> s = Stream.of("hello","JAVA","python","RUBY","PHP");
        s.sorted().forEach(str-> System.out.print(str+"\t"));
        System.out.println();
        Stream<Integer> s1 = Stream.of(1,9,5,3,7);
        s1.sorted(Comparator.reverseOrder()).forEach(str-> System.out.print(str+"\t")); 
    }

1418970-20190908031800860-1290217591.png

2.去除重复元素

Stream<T> distinct(); //返回去除重复元素的Stream
        Stream<String> s = Stream.of("hello","JAVA","hello","RUBY","JAVA");
        s.distinct().forEach(str-> System.out.print(str+"\t"));

1418970-20190908032015305-1099646418.png

3. 截取

Stream<T> limit(long); //截取Stream前long个元素
Stream<T> skip(long); //跳过Stream前long个元素
        Stream<String> s = Stream.of("hello","JAVA","hello","RUBY","JAVA");
        s.limit(3).forEach(str-> System.out.print(str+"\t"));
        System.out.println();
        Stream<Integer> s1 = Stream.of(1,9,5,3,7);
        s1.skip(3).forEach(str-> System.out.print(str+"\t"));

1418970-20190908032449081-265220136.png

4. concat合并

        Stream<String> s1 = Stream.of("php","JAVA","python");
        Stream<String> s2 = Stream.of("android", "ios");
        Stream<String> s = Stream.concat(s1,s2);
        s.forEach(str-> System.out.print(str+"\t"));

1418970-20190908033010594-220678403.png

5. flatMap把列表的元素取出,构成一个新的Stream

把元素映射为Stream,然后合并成一个新的Stream
1418970-20190908045947834-1695316767.png

        List<String> s1 = new ArrayList<>();
        s1.add( "php");
        s1.add("JAVA");
        s1.add("python");
        List<String> s2 = Arrays.asList("android", "ios");
        Stream<List> l = Stream.of(s1,s2);
        Stream<String> s = l.flatMap(list -> list.stream());
        s.forEach(str-> System.out.print(str+"\t"));

1418970-20190908034750988-1314920209.png

6. parallel把一个Stream转换为可以并行处理的Stream

Stream的元素是串行处理的,也就是只能用单线程来处理,如果要提高效率,比如进行并行排序,只需使用parallel()就可以把一个Stream变成一个并行处理的Stream,通过这个方法调用,后续的操作会尽量的并行处理。

        Stream<String> s = Stream.of("php","JAVA","python","android", "ios");
        String[] result = s.parallel() //变成一个可以并行处理的Stream
            .sorted(). //可以进行并行排序
            toArray(String[]::new); 
        System.out.println(Arrays.toString(result));

1418970-20190908035634516-1621813176.png

7. Stream的聚合方法

Optional<T> reduce(BinaryOperater<T> bo)
long count()
T max(Comparator<? super T> cp)//查找最大元素
T min(Comparator<? super T> cp) //查找最下元素

针对IntStream, longStream, DoubleStream:

    sum() //求和
    average() //求平均值
        Integer[] nums = {1,2,3,4,5,6};
        long max = Stream.of(nums).max((i1,i2)->i1.compareTo(i2)).get();
        long min = Stream.of(nums).min((i1,i2)->i1.compareTo(i2)).get();
        System.out.println("最大值:"+max+"\t最小值:"+min);
        IntStream ins1 = IntStream.builder().add(1).add(2).add(3).add(4).add(5).add(6).build();
        IntStream ins2 = IntStream.builder().add(1).add(2).add(3).add(4).add(5).add(6).build();
        Double avg = ins1.average().getAsDouble();
        long sum = ins2.sum();
        System.out.println("求和:"+sum+"\t平均值:"+avg);

1418970-20190908043328385-777692289.png

8. 测试Stream的元素是否满足

boolean allMatch(Predicate<? super T>) //所有元素均满足测试条件
boolean anyMatch(Predicate<? super T>) //至少一个元素满足测试条件
        Integer[] nums = {1,2,3,4,5,6};
        boolean max = Stream.of(nums).allMatch(x->x>0);
        boolean min = Stream.of(nums).anyMatch(x->x>5);
        System.out.println("元素全部>0:"+max+"\t存在元素>5:"+min);

1418970-20190908044235351-1733543696.png

9. forEach循环处理Stream的元素

void forEach(Consumer<? super T> action)

10. 把Stream转换为其他类型

Object[] toArray() //转换为Object数组
A[] toArray(IntFunction<A[]>) //转换为A[]数组
<R, A> R collect(Collector<? super T, A, R> Collector) //转换为List/Set等集合类型
        Stream<String> s1 = Stream.of("php","JAVA","python","android", "ios");
        String[] arr = s1.toArray(String[]::new);
        Stream<String> s2 = Stream.of("php","JAVA","python","android", "ios");
        List<String> list = s2.collect(Collectors.toList());
        System.out.println(Arrays.toString(arr));
        System.out.println(list.toString());

1418970-20190908044944675-458569301.png

11. 总结

java.util.stream.Stream题哦那个了许多实用的功能

转载于:https://www.cnblogs.com/csj2018/p/11484203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值