java8 List 流操作

//传入pId作为条件,找出List对象中,productId等于pId的对象,组成一个新的List返回;
public List<Product2Select>getList(String pId){
		List<Product2Select> selectChildrenList = childrenList.stream().filter(o -> pId.equals(o.getProductId())).collect(Collectors.toList());
		return selectChildrenList;
}
//统计List项出现次数,转化成Map
List<Integer> list = new ArrayList() {
            {
                add(12);
                add(20);
                add(12);
                add(22);
                add(22);
                add(23);
                add(159);
                add(12);
            }
        };
 
Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
map.forEach((k, v) -> System.out.println(k + ":" + v));
 
 
输出结果
20:1
22:2
23:1
12:3
159:1

//List转Map,根据List中的对象,将对象一个属性作为key,在选个属性作为value
 List<DictionarySelect> dictionarySelects = dictionary2Mapper.selectDictionary();
 Map<String, String> dictMap == dictionarySelects.stream().collect(Collectors.toMap(
                    DictionarySelect::getValue, DictionarySelect::getId, (oldValue, newValue) -> oldValue, HashMap::new));
//根据一个List对象,将对象中的某个属性提取出来,组成新的List
 /**
  * Task类中有assignee属性,
  * taskList 是一个保存Task类的List
 */
 List<String> userIdList = taskList.stream().map(Task::getAssignee).collect(Collectors.toList());
//通过流进行列表循环操作
List list = new ArrayList();
for(int i=0; i< 5; i++){
list.add(i);

//通过流对列表循环输出
 list .stream().forEach(int-> {
 System.out.println(int);
 });
//List截取,从begin位置开始,截取size个元素,组成新的List
result.stream().skip(begin).limit(size).collect(Collectors.toList());
List list = new ArrayList();
for(int i=0; i< 5; i++){
list.add(i+ "");
//对list对象的每项处理后,组成新数组返回。
list.stream().filter(comment -> {
            return comment + ":" +  comment.length;
        })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值