List<Apple> appleList = new ArrayList<>();//存放apple对象集合
|
1、在List<Apple>中,查找name为苹果1的对象Apple。
Optional<Apple> firstA = appleList .stream().filter(a -> "苹果1".equals(a.getName())).findFirst();
|
2、在List<Apple>中,查找name为苹果1的集合对象。
List<Apple> tmpList = appleList .stream().filter(a -> "苹果1".equals(a.getName())).collect(Collectors.toList());
|
3、在List<Apple>中,筛选出所有对象的name属性的集合对象。
List<
String> tmpList = appleList .stream().map(Apple::getName).collect(toList());
|
commons-lang3-3.3.2.jar org.apache.commons.lang3.StringUtils.join(applyNameList, ",");
|
String str = "a, b, c";
List<String> result = Arrays.asList(StringUtils.split(str,","));
|
/**
* 需要注意的是:
|
//List 以ID分组 Map<Integer,List<Apple>>
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
|
BigDecimal:
|
示例,domain
public class Plan {
private int id;
private String planNo;
private BigDecimal price;
private long total;
private State state;
private Calendar createTime;
private JSONObject features = new JSONObject();
Sate :noPay(1,"未支付"), pay(2,"支付"), settle(3,"结算"),
List<Plan> planList = initList();
1. 把方案编号planNo转换大写 返回列表
List<String> noList = planList.stream().map(p->p.getPlanNo().toUpperCase()).collect(Collectors.toList());
2,价格由高到低排序
List<Plan> list = planList.stream().sorted((a,b) -> b.getPrice().compareTo(a.getPrice())).collect(Collectors.toList());
3,状态为支付的价格由高到低排序
planList.stream().filter(s -> State.pay == s.getState()).sorted((a,b) -> b.getPrice().compareTo(a.getPrice())).collect(Collectors.toList());
4,求最高价/最低价/总价, total数量平均,总和
BigDecimal max = planList.stream().max((a,b)->a.getPrice().compareTo(b.getPrice())).get().getPrice();
BigDecimal min = planList.stream().min((a,b)->a.getPrice().compareTo(b.getPrice())).get().getPrice();
BigDecimal total = planList.stream().map(p->p.getPrice()).reduce(BigDecimal.ZERO,(a,b)->a.add(b));
平均 : planList.stream().mapToLong(Plan::getTotal).average().getAsDouble();
总和:planList.stream().mapToLong(Plan::getTotal).sum()
5,总共有多少种状态值
long count = planList.stream().map(p->p.getState()).distinct().count();
long c2 = planList.stream().map(p->p.getState()).collect(Collectors.toSet()).size();
6,方案编号中包含某些字符
List<Plan> list = planList.stream().filter(p->p.getPlanNo().contains("gt")).collect(Collectors.toList());
7,价格前三的方案
List<Plan> topList = planList.stream().sorted((a,b)->b.getPrice().compareTo(a.getPrice())).limit(3).collect(Collectors.toList());
8,按方案状态分组列表
Map<State, List<Plan>> map = planList.stream().collect(Collectors.groupingBy(p->p.getState()));
9,方案分成是否支付二种,查询列表
Map<Boolean, List<Plan>> map = planList.stream().collect(Collectors.partitioningBy(s -> s.getState()==State.noPay));
map.get(true) 是全部 未支付
map.get(false) 是支付 和 结算
10,转换成Map结构 <方案编号 , 价格>
Map<String, BigDecimal> map = planList.stream().collect(Collectors.toMap(p->p.getPlanNo(), Plan::getPrice));
11,转换数据结构 , list转成数组
Plan[] ps = planList.stream().toArray(Plan[]::new);
12,按状态算数量的总和/平均数
平均:Map<State, Double> map = planList.stream().collect(Collectors.groupingBy(Plan::getState, Collectors.averagingLong(Plan::getTotal)));
求和:Map<State, Long> sum = planList.stream().collect(Collectors.groupingBy(Plan::getState, Collectors.summingLong(Plan::getTotal)));
13、按状态统计BigDecimal
MAp<State,BigDecimal> map = planList.stream().collect(Collectors.groupingBy(Plan::getState, Collectors.maping(Plan::getPrice,Collectors.reducing(BigDecimal.ZERO,BigDecimal::add))));
Map<State, LongSummaryStatistics> sumMap = planList.stream().collect(Collectors.groupingBy(Plan::getState, Collectors.summarizingLong(Plan::getTotal)));
LongSummaryStatistics描述流中元素的各种摘要数据,求 count, min, max, sum, and average.
14、分组后计数
Map<String, Long> groupByType = intentionList.stream().filter(a -> Func.isNotEmpty(a.getMerchantType())).collect(Collectors.groupingBy(IntentionVO::getMerchantType, Collectors.counting()));