流式编程和函数式编程创建一个数组

public class client {
    public static void main(String[] args) {

        Random random = new Random();
        Integer[] arr =  IntStream.rangeClosed(0,4)
                .boxed()
                .map(index->random.nextInt(10))
                .toArray(Integer[]::new);

        for (Integer num: arr) {
            System.out.print(num);
        }
}

很多场合,我们需要创建一个指定长度的指定范围的数组。

输出结果:98778

关于函数式编程,在优先级队列里,也可以这么用

PriorityQueue<Integer> maxHeap = new PriorityQueue<>((s1,s2)->s1-s2);
PriorityQueue<Integer> minHeap = new PriorityQueue<>((s1,s2)->s2-s1);

传统方式是实现比较器,这里提供了一种函数式编程的方式。在spark reduceByKey/groupByKey的操作中,也可以用到哟

原文地址:https://www.cnblogs.com/xiaoyingying/p/10655484.html