国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java8集合操作(排序、取交集、并集、差集、去重并集)分組

這篇具有很好參考價值的文章主要介紹了java8集合操作(排序、取交集、并集、差集、去重并集)分組。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、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))));

}

}

java8 取交集,java,數(shù)據(jù)結(jié)構(gòu),開發(fā)語言,Powered by 金山文檔

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包