遍历赋值
list.forEach(stage -> {stage.setCreateTime(new Date()); stage.setId(UUID.randomUUID().toString().replace("-", ""));});
遍历求和
long sum = list.stream().mapToLong(SubStage::getWeight).sum();
对List<Integer>进行求和
long sum = list.stream().reduce(Integer::sum).orElse(0);
按String类型的字段分组
Map<String, List<CompanyClue>> sortMap = list.stream().collect(Collectors.groupingBy(companyClue -> companyClue.getCompanyId()));
Map<String, List<MealLikeBase>> sortMap = orginList.stream().collect(Collectors.groupingBy(MealLikeBase::getDishName));
尽量写成下面的 要不然会报LAMBDAS WHICH CAN BE REPLACED WITH METHOD REFERENCES
把lambda表达式x->x.getFplrId()改成相应的“实体类::获取实体类的属性”,如Cfptz::getFplrId
按int类型的字段分组
List<Entry<Integer, List<RankCompanyVo>>> sortCountList = tempList.stream().collect(Collectors.groupingBy(RankCompanyVo::getCount)).entrySet()
.stream().sorted((s1, s2) -> -Integer.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
分组按照某个字段有顺序
Map<String, List<MealLikeBase>> sortMap = orginList.stream().collect(Collectors.groupingBy(MealLikeBase::getDishName, LinkedHashMap ::new,Collectors.toList()));
按int类型的字段排序(倒序叙从大到小)
List<RankCompanyVo> resultList = tempList.stream().sorted(Comparator.comparingInt(RankCompanyVo::getCount).reversed()).collect(Collectors.toList());
list对象集合的某个属性值是否存在 (string类型)
whiteList.stream().filter(tWhite->tWhite.getWhiteIp().equals(ip)).findAny().isPresent();
上面的太啰嗦 用下面这个
tempList.stream().anyMatch(t->t.getDay().endsWith(str.substring(5)));
ist对象集合存在某个对象的属性和某个值相等 str 找到该对象 (目前测试string和int都可以)
Optional<MealLikeBase> sexOptional = baseList.stream().filter(item -> item.getSex().equals(str)).findFirst();
if (sexOptional.isPresent()) {
//存在
MealLikeBase mealLikeBase =
sexOptional.get()
} else {
//不存在
}
list对象里存在符合某条件的对象的个数
orginList.stream().filter(s -> s.getCountVar() >= sectionBean.getValue1()).count();
list对象转为Set<String> (目前测试string和int都可以)
Set<String> normalSet = orginList.stream().map(MealLikeBase::getSex).collect(Collectors.toSet());
list按某个属性得到新的集合 注意去重
List<String> xnameList = baseList.stream().map(BarInfo::getXname).distinct().collect(Collectors.toList());//去重
Optional<WebJsonDataVo> maxS = tempList.stream().max(Comparator.comparing(BarInfo::getValue)) 最大值
求和 tempList.stream().collect(Collectors.summingInt(BarInfo::getValue)
取list前5个 subList = list.subList(0, 5);
list排序
list1.sort((o1,o2)->o1.getWeight().compareTo(o2.getWeight()));
Collections.sort(list, (a,b) -> b.getValue().compareTo(a.getValue()));
list某个属性值全部取出来
List<String> idList=list.stream().map(Order::getId()).collect(Collectors.toList());
list删除对象元素
baseList.removeIf(i -> i.getName().equals("dal"));
List<String> 修改所有元素
ynameList = ynameList.stream().map(string -> { return Integer.parseInt(string.substring(8)) +"";}).collect(Collectors.toList());//转换成数字