第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從地圖內(nèi)的列表中刪除重復(fù)的元素

從地圖內(nèi)的列表中刪除重復(fù)的元素

慕村225694 2023-06-28 15:37:21
假設(shè)我有以下地圖:"A": [1, 2, 3, 4]"B": [5, 6, 1, 7]"C": [8, 1, 5, 9]如何從數(shù)組中刪除重復(fù)的元素,以便返回僅包含從未重復(fù)的元素的映射?"A": [2, 3, 4]"B": [6, 7]"C": [8, 9]
查看完整描述

2 回答

?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可能想這樣做:


// Initializing the map

Map<String, List<Integer>> map = new LinkedHashMap<String, List<Integer>>() {

    {

        put("A", new ArrayList<>(Arrays.asList(1, 2, 3, 4)));

        put("B", new ArrayList<>(Arrays.asList(5, 6, 1, 7)));

        put("C", new ArrayList<>(Arrays.asList(8, 1, 5, 9)));

    }

};


// finding the common elements

List<Integer> allElements = map.values().stream().flatMap(List::stream).collect(Collectors.toList());

Set<Integer> allDistinctElements = new HashSet<>();

Set<Integer> commonElements = new HashSet<>();

allElements.forEach(element -> {

    if(!allDistinctElements.add(element)) {

        commonElements.add(element);

    }

});


// removing the common elements

map.forEach((key, list) -> list.removeAll(commonElements));


// printing the map

map.forEach((key, list) -> System.out.println(key + " = " + list));

輸出:


A = [2, 3, 4]

B = [6, 7]

C = [8, 9]


查看完整回答
反對(duì) 回復(fù) 2023-06-28
?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊

首先你必須計(jì)算每個(gè)列表中的數(shù)字

Map<Integer, Long> countMap = map.values().stream()
            .flatMap(List::stream)
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

然后過(guò)濾其中 count == 1

Map<String, List<Integer>> result = map.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().stream()
                    .filter(i -> countMap.get(i) == 1).collect(Collectors.toList())));


查看完整回答
反對(duì) 回復(fù) 2023-06-28
  • 2 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)