一、List集合轉(zhuǎn)換成Set集合
Set<@NotNull Long> ids =entityList.stream().filter(e -> e != null).map(UserCopyPointEntity::getPointId).collect(Collectors.toSet());
二、集合map的循環(huán)
map.forEach((k, v) -> {
System.out.println(k + "----" + v);
});
三、集合排序,名稱排序,順序排序
if (result.size() > 0) {
// 樓層排序, 從小到大排序, null排最后
Collections.sort(result,
Comparator.nullsLast(Comparator.comparing(FloorLatestPointMeterVO::getFloorLevel,
Comparator.nullsLast(Integer::compareTo))));
// 名稱排序, 順序排序, null排最后
for (int i = 0; i < result.size(); i++) {
Collections.sort(result.get(i).getPoints(),
Comparator.nullsLast(Comparator.comparing(LatestPointMeterVO::getName,
Comparator.nullsLast(SystemConstant.COLLATOR::compare))));
}文章來源:http://www.zghlxwxcb.cn/news/detail-587873.html
}

Comparator.nullsLast使用含義
你應(yīng)該使用Comparator.nullsLast兩次:
list.sort(nullsLast(comparing(Bean::getVal, nullsLast(naturalOrder()))));
首先
nullsLast將處理Bean對象為null 時的情況.
第二個
nullsLast將處理返回值為Bean::getValnull時的情況.
如果你確定null列表中沒有任何值,那么你可以省略第一個nullsLast(如@Holger所述):
list.sort(comparing(Bean::getVal, nullsLast(naturalOrder())));
或者,如果 bean 永遠不是 `null`,則省略外部的 `nullsLast`,即只有屬性值可能是 `null`。
多屬性排序
if (dtos.isEmpty()) {
return dtos;
}
// 樓幢樓層名稱進行排序優(yōu)先級一,在設(shè)備名稱排序優(yōu)先級二, 順序排序, null排最后
if (dtos.size() > 1) {
dtos.sort(Comparator.nullsLast(Comparator.comparing(PointMonthEnergyIncrementValue::getBuildingName, Comparator.nullsLast(SystemConstant.COLLATOR::compare))
.thenComparing(PointMonthEnergyIncrementValue::getFloorName, Comparator.nullsLast(SystemConstant.COLLATOR::compare))
.thenComparing(PointMonthEnergyIncrementValue::getPointName, Comparator.nullsLast(SystemConstant.COLLATOR::compare))));
}
四、兩個List集合取交集、并集、差集、去重并集
public class Test {
public static void main(String[] args) {
List list1 = new ArrayList();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List list2 = new ArrayList();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集 取相等的
List intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
//應(yīng)用場景:批量添加權(quán)限,表中userId和roleId相同則認為數(shù)據(jù)相同不插入,需要從數(shù)據(jù)中過濾出需要插入的數(shù)據(jù)
// 差集 (list1 - list2) 取集合對象,
List<PointEntity> reduce = resultPointEntities.stream().filter(e -> {
for (PointEntity pointEntity : pointEntities) {
if (e.getId().equals(pointEntity.getId())) {
return false;//false排除不要
}
}
return true;
}).collect(Collectors.toList());
//第二種 先把集合二轉(zhuǎn)換成 單個屬性值,在調(diào)用 contains查找 集合中的值 ,有返回true,沒有返回false
List<SysUser1> collect = user1List.stream().filter(sysUser1
-> !user2List.stream().map(SysUser2::getAge).collect(Collectors.toList()).contains(sysUser1.getAge()))
.collect(Collectors.toList());
//java中contains方法是判斷是否存在包含關(guān)系,比如說a =[1,2,3,4], b=1那么a就包含b
//contains返回的是布爾類型true 和false,包含的話就返回true,不包含的話就返回false
// 差集 (list2 - list1)
List reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List listAll = list1.parallelStream().collect(Collectors.toList());
List listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原來的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原來的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-587873.html
到了這里,關(guān)于java8集合操作(排序、取交集、并集、差集、去重并集)分組的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!