Java8的Stream流(一) --- 基础用法

Stream的特性及优点:

  • 无存储. Stream不是一种数据结构,它只是某种数据源的一个视图,数据源可以是一个数组,Java容器或I/O channel等.
  • 为函数式编程而生. 对于Stream的任何修改都不会修改背后的数据源,比如对Stream执行过滤操作并不会删除被过滤的元素,而是会产生一个不包含被过滤掉元素的新Stream
  • 惰式执行: Stream上的操作不回立即执行,只有等到用户真正需要结果的时候才会执行.
  • 可消费性: Stream只能被消费一次,一旦遍历过后就会失效,就像容器的迭代器一样,想要再次遍历必须重新生成.

Stream的创建

  1. 通过已有的集合来创建流:

通过集合的stream方法来创建流

// 数组
String[] s = "I Love You But I miss You so I miss you".split(" ");
Stream<String> stream = Arrays.stream(s);
// 集合
List<String> list = Arrays.asList(s);
Stream<String> stream1 = list.stream();
  1. 通过Stream创建流

Stream类提供了of静态方法来创建Stream流

Stream<String> stringStream = Stream.of("1", "2", "3");

Stream流常用的中间操作

filter

filter方法用于通过设置的条件过滤出元素,下面的例子是过滤出长度大于3的字符串

String[] s = "I Love You But I miss You so I miss you".split(" ");
List<String> list = Arrays.asList(s);
// 流转List
list.stream().filter(str -> str.length() > 3);

map

map元素用于映射每隔元素到对应的结果,下面的例子用map输出元素对应的平方数

Stream.of(1,2,3,4,5).map(i -> i * i).forEach(System.out::println);

limit / skip

limit是返回Stream的前面n个元素;skip是跳过前面n个元素.下面的例子是保留前四个元素:

Stream.of(1,2,3,4,5).limit(4).forEach(System.out::println);

sorted

sorted方法是用于对流进行排序.下面例子是进行排序

// 默认是从小到大
Stream.of(5,4,3,2,1).sorted().forEach(System.out::println);
// 重写比较器方法从大到小
Stream.of(1,2,3,4,5).sorted((x,y)-> {return y - x;}).forEach(System.out::println);

distinct

distinct主要用来去重,下面的例子是用来去重的

// 去重
Stream.of(5,4,3,5,4).distinct().forEach(System.out::println);

注意上面操作的返回结果还是Stream流,这个意思也就是可以进行流式操作,直到遇到最终操作,下面就说一下最终操作.

最终操作

最终操作就是一些输出,统计,转换等操作,返回结果不再是Stream流. 但是注意的是当进行最终操作之后,不可再次使用流,否则会抛出异常.

java.lang.IllegalStateException: stream has already been operated upon or closed

forEach

Stream利用forEach来迭代流中的数据,下面的代码片段使用forEach输出10个随机数

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

count

count方法是用来统计流中元素的个数

Stream.of(5,4,3,5,4).count();

collect

collect方法是一个规约操作,就是将结果进行转换为集合或者其他. 下面的例子是将流转化为集合.

Stream.of(5,4,3,5,4).collect(Collectors.toList());
private static List<User> list = new ArrayList<User>();
    
    public static void main(String[] args) {
        list =  Arrays.asList(
                new User(1, "a", 10),
                new User(4, "d", 19),
                new User(5, "e", 13),
                new User(2, "b", 14),
                new User(3, "a", 10),
                new User(6, "f", 16)
                );
        
        long start = System.currentTimeMillis();
        
        order();
        
        println(String.format("耗时[%s]毫秒", (System.currentTimeMillis() - start)));
        
    }
    
    /**
     * 多条件去重
     * @param list
     */
    public static void order() {
        list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(u -> println(u));
    }
    
    public static void group() {
        Map<Integer, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getAge));
        System.out.println(collect);
    }
    
    /**
     * filter过滤
     * @param list
     */
    public static void filterAge() {
        list.stream().filter(u -> u.getAge() == 10).forEach(u -> println(u));
    }
    
    /**
     * sorted排序
     */
    public static void stord() {
        list.stream().sorted(Comparator.comparing(u-> u.getAge())).forEach(u -> println(u));
        
    }
    /**
     * limit方法限制最多返回多少元素
     */
    public static void limit() {
        list.stream().limit(2).forEach(u -> println(u));
    }
    /**
     * 不要前多n个元素,n大于满足条件的元素个数就返回空的流
     */
    public static void skip() {
        list.stream().skip(2).forEach(u -> println(u));
    }
    // 最大值 最小值
    public static void statistics() {
        Optional<User> min = list.stream().min(Comparator.comparing(User::getUserId));
        println(min);
        Optional<User> max = list.stream().max(Comparator.comparing(User::getUserId));
        println(max);
    }
    
    // 统计
    public static void summarizingInt(){
        IntSummaryStatistics statistics = list.stream().collect(Collectors.summarizingInt(User::getAge));
        double average = statistics.getAverage();
        long count = statistics.getCount();
        int max = statistics.getMax();
        int min = statistics.getMin();
        long sum = statistics.getSum();
        println(average);
        println(count);
        println(min);
        println(sum);
        println(max);
        
    }
    /**
     * 转set
     */
    public static void toSet() {
        Set<User> collect = list.stream().collect(Collectors.toSet());
        Iterator<User> iterator = collect.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next().getUserId());
        }
    }

    /**
     * 转map
     */
    public static void toMap() {
        Map<Integer, User> collect = list.stream().collect(Collectors.toMap(User::getUserId, u -> u));
        for (Integer in : collect.keySet()) {
          User u = collect.get(in);//得到每个key多对用value的值
          println(u);
        }
    }
    /**
     *map
     */
    public static void map() {
        list.stream().map(User::getUserId).forEach(userId -> println(userId));
        list.stream().mapToInt(User::getAge).forEach(userId -> println(userId));
        list.stream().mapToDouble(User::getUserId).forEach(userId -> println(userId));
        list.stream().mapToLong(User::getUserId).forEach(userId -> println(userId));
    }
    
    /**
     * 查找与匹配
     * allMatch方法与anyMatch差不多,表示所有的元素都满足才返回true。noneMatch方法表示没有元素满足
     */
    public static void anyMatch() {
        boolean anyMatch = list.stream().anyMatch(u -> u.getAge() == 100);
        boolean allMatch = list.stream().allMatch(u -> u.getUserId() == 10);
        boolean noneMatch = list.stream().noneMatch(u -> u.getUserId() == 10);
        println(anyMatch);
        println(allMatch);
        println(noneMatch);
    }
    
    /**
     * reduce操作
     */
    public static void reduce() {
        Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);
        Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);
        Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);
        println(sum);
        println(max);
        println(min);
    }
    /**
     * 公共输出
     * @param c
     */
    public static void println(Object c) {
        System.out.println(c.toString());
    }
//forEach:
list.stream().forEach(userProduct->{
    BigDecimal juniorIntegral = new BigDecimal(userProduct.getJuniorIntegral()).multiply(new BigDecimal(tempProductReqDto.getJuniorIntegral())).divide(new BigDecimal(coreTeamTemplateProduct.getJuniorIntegral()),0,BigDecimal.ROUND_DOWN);
    userProduct.setId(null);
    userProduct.setProportion(tempProductReqDto.getJuniorIntegral()+"");
    userProduct.setJuniorIntegral(juniorIntegral.intValue());
    userProduct.setEffectiveTime(nowTime);
    userProduct.setExpireTime(DateUtils.parseDate(RequestConstants.FOREVER_DATE));
    userProduct.setGmtCreate(nowTime);
    userProduct.setGmtModified(nowTime);
});
Set<Integer> userProductIds= list.stream().map(c->c.getId()).collect(Collectors.toSet());

Stream流之求和

BigDecimal:
BigDecimal bb =list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);

int、double、long:
double max = list.stream().mapToDouble(User::getHeight).sum();

collect

// 转为list
List<String> nameList = prodList.stream().map(item -> item.getName()).collect(Collectors.toList());

// 转为Set
Set<String> nameSet = prodList.stream().map(item -> item.getName()).collect(Collectors.toSet());

// 转为Map
Map<Long, Product> idMap = prodList.stream().collect(Collectors.toMap(Product::getId,Functions.identity()));

// 转为LinkedList
prodList.stream().map(item -> item.getName()).collect(Collectors.toCollection(LinkedList::new));

//统计总数
Long count = prodList.stream().collect(Collectors.counting());

// 分组
Map<Long, List<Product>> listMap = prodList.stream().collect(Collectors.groupingBy(Product::getId));

// 拼接
String nameJoin = prodList.stream().map(Product::getName).collect(Collectors.joining(","));
-- 假设我们用以下方式收集元素A、B、C、D
-- joining() 返回用于连接不带分隔符的元素的收集器。对于给定的输入元素,输出字符串将是ABCD

-- joining(CharSequence delimiter) 返回用于将元素与给定分隔符连接起来的收集器。对于给定的输入元素和分隔符(-),输出字符串将是A-B-C-D

-- joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 返回收集器,该收集器用于连接具有给定分隔符、前缀和后缀的元素。对于给定的输入元素,分隔符( - )、前缀( [ )和后缀( ] ),输出字符串将是[A-B-C-D]

过滤:

List<OrderInfo> headerInfos = list.stream().filter(e -> e.getPlatformDelivery() == PLATFORM_SHIPPED).collect(Collectors.toList());

从list集合中取出某一属性的值的集合

String mailNos = headerInfos.stream().map(DeliveryOrderHeaderInfo::getMailNo).collect(Collectors.joining(","));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值