java(1.8新特性)流式计算

一、Stream API

1.所有继承Collection的接口都可以直接转换成流

List list = Arrays.asList(1,2,3,4,5);
Stream stream = list.stream();
        
Map map = new HashMap();
Stream stream1 = map.entrySet().stream();

2.利用Arrays类中的stream()方法

Integer[] integers = new Integer[]{1,2,3,4,5};
Stream<Integer> stream2 = Arrays.stream(integers);

3.使用stream类中的静态方法

 Stream stream3 = Stream.of(1,2,3,4,5);

4.利用BufferedReader读取文件中的内容转化为流

try {
    BufferedReader reader = new BufferedReader(new FileReader("D:\\stream.txt"));
    Stream stream4 = reader.lines();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

二、操作流

流的操作主要有筛选、切片、匹配、映射、规约

1.筛选,筛选出特定的数据

2.切片,忽略流中的头几个元素或截取流中的前几个元素

3.匹配,查询是否有匹配的数据

4.映射,就是将一个对象转换成另一个对象,把老对象映射到新对象上。

5.规约,将流中的元素反复结合起来(求和,最大值,最小值)

stream()方法

1、filter()

用于对数据进行过滤,筛选出符合条件的数据;

//过滤大于2的数据

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> collect = list.stream().filter(s -> s > 2).collect(Collectors.toList());
System.out.println(collect);

2、map()

对流中的数据进行同一项操作

//将集合中的元素转换成小写

List<String> list = Arrays.asList("A","B");
List<String> collect = list.stream().map(s -> s.toLowerCase()).collect(Collectors.toList());
System.out.println(collect);

3、flatMap()

将流中的每一个元素转化为一个流,然后将转化的一个个流组合成一个新的流;

List<List<Integer>> list = Arrays.asList(Arrays.asList(1,2,3,4),Arrays.asList(5,6,7,8));
List<Integer> collect = list.stream().flatMap(s -> s.stream()).collect(Collectors.toList());
System.out.println(collect);

4、sorted

将流中的数据进行排序,然后排序后的数据组合成一个新的流;

List<Integer> list = Arrays.asList(4,5,1,2);
//升序
List<Integer> collect = list.stream().sorted().collect(Collectors.toList());
//降序
List<Integer> collect1 = list.stream().sorted((t1, t2) -> {return t1 < t2 ? 1 : t1.equals(t2) ? 0 : -1; }).collect(Collectors.toList());
System.out.println(collect);
System.out.println(collect

5、reduce()

将流中的数据进行规约,返回规约后的数据;

List<Integer> list = Arrays.asList(4,5,1,2);
Optional<Integer> reduce = list.stream().reduce((t1, t2) -> t1 + t2);
Integer reduce1 = list.stream().reduce(0, (t1, t2) -> t1 + t2);
//每个单独的并行流进行求和操作,并行流输出的结果进行相乘操作输出最终结果
Integer reduce2 = list.parallelStream().reduce(0, (t1, t2) -> t1 + t2, (s1, s2) -> s1 * s2);
System.out.println(reduce);
System.out.println(reduce1);
System.out.println(reduce2);

6、collect()

将流中的数据转化为一个新的数据结构;

List collect = Stream.of(1, 2, 3).collect(Collectors.toList());
System.out.println(collect);

三、中间操作

1、distinct

//去重

List<Integer> list = Arrays.asList(1,2,3,3,3);
List<Integer> collect = list.stream().distinct().collect(Collectors.toList());
System.out.println(collect);

2、limit

//返回前n个元素

List<Integer> list = Arrays.asList(1,2,3,3,3);
List<Integer> collect = list.stream().limit(2).collect(Collectors.toList());
System.out.println(collect);
//输出结果 [1, 2]

3、skip

//跳过n个元素

List<Integer> list = Arrays.asList(1,2,3,3,3);
List<Integer> collect = list.stream().skip(2).collect(Collectors.toList());
System.out.println(collect);
//输出结果 [3, 3, 3]

4、allMatch

//查找是否全部满足指定参数行为

List<Integer> list = Arrays.asList(1,2,3,3,3);
boolean b = list.stream().allMatch(s -> s > 2);
System.out.println(b);

5、anyMatch

//查找是否存在一个或多个满足指定的参数行为

List<Integer> list = Arrays.asList(1,2,3,3,3);
boolean b = list.stream().anyMatch(s -> s > 2);
System.out.println(b);

6、noneMatch

//查找是否不存在满足指定行为的元素

List<Integer> list = Arrays.asList(1,2,3,3,3);
boolean b = list.stream().noneMatch(s -> s == 4);
System.out.println(b);

7、findFirst

//用于返回满足条件的第一个元素

List<Integer> list = Arrays.asList(1,2,3,3,3);
Integer integer = list.stream().filter(s -> s < 2).findFirst().orElse(0);
System.out.println(integer);

8、findAny

//用于返回满足条件的任意一个元素

9、count

//统计总个数

List<Integer> list = Arrays.asList(5,3,2,6,1);
//统计总数
long count = list.stream().count();
System.out.println(count);

10、最大值最小值

List<Integer> list = Arrays.asList(5,3,2,6,1);
Integer integer = list.stream().max((s1, s2) -> s1 - s2).orElse(0);
Integer integer1 = list.stream().collect(Collectors.minBy((s1, s2) -> s1 - s2)).orElse(0);
System.out.println(integer);
System.out.println(integer1);

11、求和 summingInt、summingLong、summingDouble

List<Integer> list = Arrays.asList(5,3,2,6,1);
Integer collect = list.stream().collect(Collectors.summingInt(Integer::intValue));
Integer collect1 = list.stream().mapToInt(Integer::intValue).sum();
System.out.println(collect);
System.out.println(collect1);

12、平均值 averageInt、averageLong、averageDouble

List<Integer> list = Arrays.asList(5,3,2,6,1);
Double collect = list.stream().collect(Collectors.averagingInt(Integer::intValue));
System.out.println(collect);

13、summarizingInt、summarizingLong、summarizingDouble

//一次性查询元素个数、总和、最大值、最小值和平均值

List<Integer> list = Arrays.asList(5,3,2,6,1);
IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(Integer::intValue));
System.out.println(collect);
//输出IntSummaryStatistics{count=5, sum=17, min=1, average=3.400000, max=6}

14、joining

//字符串拼接

List<String> list = Arrays.asList("a","b","c","d");
String collect = list.stream().collect(Collectors.joining("_"));
System.out.println(collect);

15、groupingBy

//分组

List<Integer> list = Arrays.asList(4,2,3,1,3);
Map<Integer, List<Integer>> collect = list.stream().collect(Collectors.groupingBy(Integer::intValue));
System.out.println(collect);

16、partitioningBy

List<Integer> list = Arrays.asList(4,2,3,1,3);
Map<Boolean, List<Integer>> collect = list.stream().collect(Collectors.partitioningBy(s -> s > 2));
System.out.println(collect);
//结果{false=[2, 1], true=[4, 3, 3]}

猜你喜欢

转载自blog.csdn.net/qq_60547244/article/details/123416542