簡體   English   中英

檢查 HashMap 值是否相等

[英]Check if HashMap values are equal

我需要編寫一個循環來查看 HashMap 中的值是否相等,以及它們是否出現了多少次。 將通過掃描器輸入一組數字(示例輸入將在下面) 下面的代碼將計數鍵和 HashSet 的值放入 hashMap。

public static void main(String[] args) {
    System.out.println("Type in your numbers followed by spaces and press enter");
    System.out.println("After every set entered type in any letter to enter more sets");
    System.out.println("Or enter * to finish");

    HashMap<Integer, HashSet<Integer>> hset = new HashMap<>();
    Scanner sc = new Scanner(System.in);
    int count = 1;

    HashSet<Integer> list = new HashSet<>();
    while(sc.hasNextLine()) {
        while(sc.hasNextInt()) {
            list.add(sc.nextInt());
        }
        hset.put(count, new HashSet<>(list));
        count++;
        list.clear();
        sc.nextLine();
        if(sc.nextLine().equals("*")) {
            System.out.println("working");
            break;
        }
    }
    for(int i=0; i<count; i++){
        //some code goes here
        //if(hset.get(x) == hset.get(j)) or something along these lines
    }
}

//Sample Scanner input
1 2 3 4 5
10 9 8 7
5 4 3 2 1
1 1 1 1 1
1 2 3 5
1 2 3 6
6 4 2
2 4 6
4 2 6
4 6 2
6 2 4
1 3 2 4 5
15 14 13
5 3 2 1
79
7 9

//What I need the output to look like    
[7, 9]=1
[1]=1
[7, 8, 9, 10]=1
[13, 14, 15]=1
[1, 2, 3, 5]=2
[1, 2, 3, 6]=1
[2, 4, 6]=5
[1, 2, 3, 4, 5]=3
[79]=1

鑒於 List of List 表單中的上述輸入,您可以按如下方式進行:

List<List<Integer>> list = List.of(List.of(1, 2, 3, 4, 5),
        List.of(10, 9, 8, 7), List.of(5, 4, 3, 2, 1),
        List.of(1, 1, 1, 1, 1), List.of(1, 2, 3, 5),
        List.of(1, 2, 3, 6), List.of(6, 4, 2),
        List.of(2, 4, 6), List.of(4, 2, 6), List.of(4, 6, 2),
        List.of(6, 2, 4), List.of(1, 3, 2, 4, 5),
        List.of(15, 14, 13), List.of(5, 3, 2, 1), List.of(79),
        List.of(7, 9));
  • 因為計數會發生變化,我建議使用集合作為鍵,計數作為值。 您以后可以隨時反轉它們。 另請注意,具有相同值的sets比較相同,而不考慮順序。
  • 只需 stream 列表並轉換為集合。 dups 將被自動刪除。
  • 把它們放在Map<Set<Integer>, Long>
  • 仍然可以從控制台獲取輸入。
Map<Set<Integer>, Long> map = list.stream().map(lst->new HashSet<>(lst))
        .collect(Collectors.groupingBy(a->a,
            Collectors.counting()));

印刷

[7, 9]=1
[1]=1
[7, 8, 9, 10]=1
[13, 14, 15]=1
[1, 2, 3, 5]=2
[2, 4, 6]=5
[1, 2, 3, 6]=1
[79]=1
[1, 2, 3, 4, 5]=3
 

我會這樣做:

System.out.println("Type in your numbers followed by spaces and press enter");
System.out.println("After every set entered type in any letter to enter more sets");
System.out.println("Or enter * to finish");
Scanner sc = new Scanner(System.in);

Map<Set<Integer>, Integer> setCounts = new HashMap<>();
do {
    if (sc.hasNextInt()) {
        Set<Integer> set = new TreeSet<>();
        do {
            set.add(sc.nextInt());
        } while (sc.hasNextInt());
        setCounts.compute(set, (key, count) -> count != null ? count + 1 : 1);
    }
} while (sc.hasNext() && ! sc.next().equals("*"));
setCounts.entrySet().forEach(System.out::println);

問題中的數字,更改為適合印刷說明

1 2 3 4 5 x
10 9 8 7 x
5 4 3 2 1 x
1 1 1 1 1 x
1 2 3 5 x
1 2 3 6 x
6 4 2 x
2 4 6 x
4 2 6 x
4 6 2 x
6 2 4 x
1 3 2 4 5 x
15 14 13 x
5 3 2 1 x
79 x
7 9 x
*

Output

[7, 9]=1
[1]=1
[7, 8, 9, 10]=1
[13, 14, 15]=1
[1, 2, 3, 5]=2
[2, 4, 6]=5
[1, 2, 3, 6]=1
[79]=1
[1, 2, 3, 4, 5]=3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM