jdk8新特性-stream

摘要

以一种声明的方式处理数据。让程序员写出高效率、干净、简洁的代码。将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。


示例

1.生成流

1.1 生成串行流

list.stream() 

2.1 生成并行流

list.parallelStream()

2.forEach

迭代流中的每个数据

static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < 5; i++) {
        tempList.add(i);
    }
}
System.out.println("========forEach=======");
tempList.stream().limit(10).forEach(System.out::println);

输出

========forEach=======
0
1
2
3
4

3.map

映射每个元素到对应的结果

static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < 5; i++) {
        tempList.add(i);
    }
}
System.out.println("========map=======");
tempList.stream().map(x->"第"+x+"个元素").forEach(System.out::println);

输出

========map=======0个元素
第1个元素
第2个元素
第3个元素
第4个元素

4.filter

static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < 5; i++) {
        tempList.add(i);
    }
}
System.out.println("========filter=======");
tempList.stream().filter(integer -> integer<3).forEach(System.out::println);

输出

========filter=======
0
1
2

5.limit

获取指定数量的流

static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < 5; i++) {
        tempList.add(i);
    }
}
System.out.println("========limit=======");
tempList.stream().limit(3).forEach(System.out::println);

输出

========limit=======
0
1
2

6.sorted

static Random random=new Random();
static int max=5;
static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < max; i++) {
        tempList.add(random.nextInt(10));
    }
}
System.out.println("========sorted=======");
tempList.stream().sorted((x,y)->x-y).forEach(System.out::println);

输出

========sorted=======
1
1
3
5
8

7.Collectors

归约操作,将流转换成集合和聚合元素。Collectors 可用于返回列表、字符串、map等等

static Random random=new Random();
static int max=5;
static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < max; i++) {
        tempList.add(random.nextInt(10));
    }
}
System.out.println("========Collectors.toList=======");
List<Integer> filtered = tempList.stream().filter(integer -> integer<5).collect(Collectors.toList());
System.out.println(filtered);
System.out.println("========Collectors.joining=======");
String mergedString = tempList.stream().map(integer -> String.valueOf(integer)).collect(Collectors.joining(","));
System.out.println(mergedString);
System.out.println("========Collectors.toMap=======");
Map<String,String> map = tempList.stream().collect(Collectors.toMap(integer->String.valueOf(integer),integer->"第"+integer+"个元素",(oldVal, currVal) -> oldVal));
System.out.println(JSON.toJSONString(map));

输出

========Collectors.toList=======
[3, 1]
========Collectors.joining=======
3,5,6,1,7
========Collectors.toMap=======
{"1":"第1个元素","3":"第3个元素","5":"第5个元素","6":"第6个元素","7":"第7个元素"}

8.统计

产生统计结果的收集器。它们主要用于int、double、long等基本类型上,它们可以用来产生类似如下的统计结果

static Random random=new Random();
static int max=5;
static List<Integer> tempList =new ArrayList<>();
static {
    for (int i = 0; i < max; i++) {
        tempList.add(random.nextInt(10));
    }
}
System.out.println("========statistics=======");
IntSummaryStatistics stats = tempList.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("max=" + stats.getMax());
System.out.println("min=" + stats.getMin());
System.out.println("sum=" + stats.getSum());
System.out.println("avg=" + stats.getAverage());

输出

========statistics=======
max=9
min=1
sum=26
avg=5.2
发布了26 篇原创文章 · 获赞 28 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u014395955/article/details/104030992
今日推荐