Stream流操作

Stream特性:

  1. Stream 是不会存储元素的。
  2. Stream 不会改变原对象,相反,他们会返回一个持有结果的新Stream。
  3. Stream 操作是延迟执行的。意味着它们会等到需要结果的时候才执行。

Stream生成方式

/** collection.stream && collection.parallelStream */
Collection<String> collection = new ArrayList<>();
Stream<String> collectionStream = collection.stream();
collectionStream = collection.parallelStream();
/** Arrays.stream */
Stream<String> arrStream = Arrays.stream(new String[10]);
/** Stream.of */
Stream<Integer> ofStream = Stream.of(1, 2, 3);
/** Stream.iterate生成无限流 */
Stream<Integer> iterateStream = Stream.iterate(0, (x) -> x + 2);
/** Stream.generate */
Stream<Double> generateStream = Stream.generate(() -> Math.random());

 Stream中间操作

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
stream
        /** 映射 */
        .map(i -> i + 1)
        .flatMap(i -> Stream.of(i))
        /** 过滤 */
        .filter(i -> i <= 6)
        /** 截取 */
        .limit(5)
        /** 跳过 */
        .skip(1)
        /** 去重 */
        .distinct()
        /** 自然排序 */
        .sorted()
        /** 自定义排序 */
        .sorted(Integer::compareTo);

Stream终止操作(每个stream数据源只能有一次终止操作

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
/** 终止API */
// 是否匹配所有元素
Boolean allMatch = stream.allMatch((i) -> i > 5);
System.out.println("allMatch ------> " + allMatch);
// 至少匹配一个元素
Boolean anyMatch = stream.anyMatch((i) -> i == 5);
System.out.println("anyMatch ------> " + anyMatch);
// 是否没有匹配所有元素
Boolean noneMatch = stream.noneMatch((i) -> i < 5);
System.out.println("noneMatch ------> " + noneMatch);
// 返回第一个元素
Integer first = stream.findFirst().get();
System.out.println("first ------> " + first);
// 返回当前流中任意一个元素
Integer any = stream.findAny().get();
System.out.println("any ------> " + any);
// 返回当前流中元素个数
Long count = stream.count();
System.out.println("count ------> " + count);
// 获取当前流中最小值
Integer min = stream.min(Integer::compareTo).get();
System.out.println("min ------> " + min);
// 获取当前流中最大值
Integer max = stream.max(Integer::compareTo).get();
System.out.println("max ------> " + max);

/** reduce(规约):将流中元素反复结合起来得到一个值 */
// 规约:0为第一个参数的默认值,x是计算后的返回值,y是每一项的值
Integer reduce = stream.map(i -> i + 1).reduce(0, (x, y) -> x + y);
System.out.println(reduce);
// 规约:x是计算后的返回值,默认为第一项的值,y为其后的每一项值
Integer reduceOpt = stream.map(i -> i + 1).reduce((x, y) -> x + y).get();
System.out.println(reduceOpt);
// 求和
BigDecimal sum = stream.map(i -> new BigDecimal(i)).reduce(BigDecimal::add).get();
System.out.println(sum);

/** collect(收集):将流转换成其他形式 */
Set<Integer> set = stream.collect(Collectors.toSet());
List<Integer> list = stream.collect(Collectors.toList());
HashSet<Integer> hashSet = stream.collect(Collectors.toCollection(HashSet::new));
// 分组 将返回值相同的分为一组
Map<String, List<Integer>> group = stream.collect(Collectors.groupingBy((x) -> x % 2 == 0 ? "偶数" : "奇数"));
// 分区
Map<Boolean, List<Integer>> partition = stream.collect(Collectors.partitioningBy((x) -> x > 5));
// 汇总
DoubleSummaryStatistics summary = stream.collect(Collectors.summarizingDouble((x) -> x));
System.out.println("summary max ------> "+summary.getMax());
System.out.println("summary avg ------> "+summary.getAverage());
System.out.println("summary sum ------> "+summary.getSum());

 Stream常用操作

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
class User {
    private String name;

    private String sex;

    private Integer age;

    private String email;
}

/** 获取指定属性列表 */
List<String> nameList = userList.stream()
        .filter(Objects::nonNull)
        .map(User::getName)
        .collect(Collectors.toList());
System.out.println(nameList);

/** list转map */
Map<String, User> userMap = userList.stream()
        .filter(Objects::nonNull)
        .collect(Collectors.toMap(User::getName, User -> User, (key1, key2) -> key2));
System.out.println(JSONObject.toJSONString(userMap));

/** 按指定字段排序 */
List<User> sorted = userList.stream()
        .filter(Objects::nonNull)
        .sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()))
        .collect(Collectors.toList());
System.out.println(JSONObject.toJSONString(sorted));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值