steam()的应用

Steam()是jdk1.8新增的一个对集合操作的类,这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同。
源码介绍
支持顺序和并行聚合的元素序列
操作。下面的示例演示了使用

int sum=widgets.stream()

.filter(w->w.getColor()=红色)

.mapToInt(w->w.getWeight())

.sum();

}

Collection.stream()创建顺序流,

Collection。parallelStream()创建并行流;
一:构造流的几种常见方法

//1.第一种,Steam.of(T... values)
   	Stream stream = Stream.of("a1", "b1", "c1")// 2.list.steam()使用较多
	List<User) list = new ArrayList()
	list.steam().常用方法,
	    //filter()-过滤,sorted()-排序,map()-执行函数,collect()-返回一个集合
        List<String> list2 = list.parallelStream().filter(t -> t.getValue() > 0)
			                .sorted(Comparator.comparing(Matter::getValue).reversed())
			                .map(Matter::getName)
			                .collect(Collectors.toList());
	3.List转map
	//查询自己签收的图片
	pubic void test(){
		List<Photos> photosList = photosDao.findByEnquiry_ReciverAndStateOrderByCreatimeDesc(Long.valueOf(userId), 0);
        /**
         * 按日期分组
         */
        Map<String,List<Photos>> map= photosList.parallelStream().collect(Collectors.groupingBy(this::fetchGroupKey));
	}
	
	
		 /**
         	* 日期是Date类型这里需要转化为String
         	*/
	  private String fetchGroupKey(Photos photos){
        return photos.getCreatime().toString();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值