Java 检查HashMap值是否相等

Java 检查HashMap值是否相等,java,hash,hashmap,hashset,Java,Hash,Hashmap,Hashset,我需要写一个循环,看看HashMap中的值是否相等,如果相等,看看它们出现了多少次。数字集将通过扫描仪输入(下面是输入示例)。下面的代码将把count键和HashSet的值放入hashMap中 public static void main(String[] args) { System.out.println("Type in your numbers followed by spaces and press enter"); System.out.prin

我需要写一个循环,看看HashMap中的值是否相等,如果相等,看看它们出现了多少次。数字集将通过扫描仪输入(下面是输入示例)。下面的代码将把count键和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
publicstaticvoidmain(字符串[]args){
System.out.println(“输入数字,后跟空格,然后按enter键”);
System.out.println(“每输入一组后,键入任何字母以输入更多组”);
System.out.println(“或输入*完成”);
HashMap hset=新的HashMap();
扫描仪sc=新的扫描仪(System.in);
整数计数=1;
HashSet list=新的HashSet();
while(sc.hasNextLine()){
while(sc.hasnetint()){
添加(sc.nextInt());
}
hset.put(计数,新哈希集(列表));
计数++;
list.clear();
sc.nextLine();
if(sc.nextLine()等于(“*”){
系统输出打印(“工作”);
打破
}
}

对于(int i=0;i在列表形式的列表中给定上述输入,您可以按如下方式执行:

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));
印刷品

[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(“输入数字,后跟空格,然后按enter键”);
System.out.println(“每输入一组后,键入任何字母以输入更多组”);
System.out.println(“或输入*完成”);
扫描仪sc=新的扫描仪(System.in);
Map setCounts=new HashMap();
做{
if(sc.hasnetint()){
Set=新树集();
做{
set.add(sc.nextInt());
}而(sc.hasnetint());
计算(set,(key,count)->count!=null?count+1:1);
}
}而(sc.hasNext()&&!sc.next()等于(“*”);
setCounts.entrySet().forEach(System.out::println);
问题中的数字,更改为符合打印说明

12345x
10987X
5 4 3 2 1 x
1 x
1235X
1236 x
6 4 2 x
2 4 6 x
4 2 6 x
4 6 2 x
6 2 4 x
123445X
15 14 13 x
5 3 2 1 x
79 x
79x
*
输出

[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

您是否检查了您的代码在for循环之前是否工作?因为它不应该工作。您当前正在将整个输入读取到一个HasSet(即,只有一个HashMap条目)中,该HasSet的副本为:
[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