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;
}