简体   繁体   English

检查 HashMap 值是否相等

[英]Check if HashMap values are equal

I need to write a loop that will see if the values in a HashMap are equal and if they are see how many times they occur.我需要编写一个循环来查看 HashMap 中的值是否相等,以及它们是否出现了多少次。 Sets of numbers will be entered in through the Scanner (example input will be below) The following code will put the key of count and the value of the HashSet into the 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

Given the above input in a List of List form, you can do it as follows:鉴于 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));
  • since the count will be changing I recommend using the set as the key and the count as the value.因为计数会发生变化,我建议使用集合作为键,计数作为值。 You can always reverse them later.您以后可以随时反转它们。 Also note that sets with the same values compare equally without regard to order.另请注意,具有相同值的sets比较相同,而不考虑顺序。
  • just stream the lists and convert to a set.只需 stream 列表并转换为集合。 The dups will be removed automatically. dups 将被自动删除。
  • put them in a Map<Set<Integer>, Long>把它们放在Map<Set<Integer>, Long>
  • input may still be taken from the console.仍然可以从控制台获取输入。
Map<Set<Integer>, Long> map = list.stream().map(lst->new HashSet<>(lst))
        .collect(Collectors.groupingBy(a->a,
            Collectors.counting()));

Prints印刷

[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
 

I would do it like this:我会这样做:

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

Numbers from question, changed to fit printed instructions问题中的数字,更改为适合印刷说明

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