java1.8分组及其常用方法

filter 过滤条件 

Map<String, List<IndexQuestion>> questionMap = indexQuestions.stream().filter((e) -> e.getFromUnitId() != null)
        .collect(Collectors.groupingBy(x -> x.getIndexFlowId()));
questionMap.forEach((k, v) -> setExportQuestionExcelData(localPath, k,"", v,response));

一个集合塞入另一个集合

List<Object[]> list=(List<Object[]>) ;
indexQuestions= list.stream().map(x-> {
   IndexQuestion indexQuestion=new IndexQuestion();
   indexQuestion.setId(Integer.valueOf((x[0]).toString()));
   return indexQuestion;
  }).collect(Collectors.toList());

将集合两个值拿出 塞进map k v 

//key无重复值
List<Entity> list = new ArrayList<>();
Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType));

//key有重复值的情况下
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));

集合去重和求差集 

//集合去重   
List<LpBgglSettleInfo> objRelationList = lpBgglSettleInfos.stream().collect(Collectors.collectingAndThen
                (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(LpBgglSettleInfo::getSysId))), ArrayList::new));

        List<String> getSysIdList = objRelationList.stream().map(p -> p.getSysId()).collect(toList());

// 集合 taskNoList - 集合 getSysIdList 
        List<String>   reTaskNoList= taskNoList.stream().filter(item -> !getSysIdList.contains(item)).collect(toList());
 //从list集合中,取出字段name的列表
        List<String> names = list.stream().map(p -> p.getName()).collect(Collectors.toList());

求和:BigDecimal

List<BigDecimal> list=new ArrayList();
BigDecimal  sum = list.stream().reduce(BigDecimal.ZERO,BigDecimal::add);
List<Object> list = new ArrayList();
BigDecimal amounts = list.stream().map(item -> new BigDecimal(item.get属性())).reduce(BigDecimal.ZERO, BigDecimal::add);
package com.mo.tablet;

//import com.mo.tablet.entity.resources.OrderEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.stream.Collectors;


public class MainsTest {
    public static void main(String[] args) {
        long time = 1L;
        List<People> list = new ArrayList<>();
        list.add(new People("曹丕", "魏", time++));
        list.add(new People("关羽", "蜀", time++));
        list.add(new People("刘备", "蜀", time++));
        list.add(new People("小乔", "吴", time++));
        list.add(new People("周瑜", "吴", time++));
        list.add(new People("曹操", "魏", time++));

        Map<String, List<People>> collect = list.stream()
                .filter(d -> d != null && d.getAddress() != null)
                .sorted(Comparator.comparingLong(People::getCreateTime).reversed())
                .collect(Collectors.groupingBy(People::getAddress, LinkedHashMap::new, Collectors.toList()));

        for (Map.Entry<String, List<People>> entry : collect.entrySet()) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }
    }

}
@Data
@AllArgsConstructor
 class People {

    private String name;

    private String address;

    private Long createTime;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值