java—通过哈希Map中的值进行循环并查看特定哈希集出现多少次的算法

5vf7fwbs  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(335)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

两天前关门了。
改进这个问题
我需要写一个循环,看看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
   }
}

//Example 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 the output should 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
u4dcyp6a

u4dcyp6a1#

从您的特定示例抽象,您有一个项目列表,并且您想计算每个项目在列表中出现的次数。
实现结果的一个经典方法是使用一个以项为键、计数器为值的Map;然后逐项处理列表,检查该项是否已经在Map中,如果没有,则创建条目并将计数器设置为1,否则增加现有计数器。
回到您的问题,您的项是一组整数(每行一个),因此Map将是一个 Map<Set<Integer>,Integer>不是a Map<Integer,Set<Integer>> 就像你的代码一样。

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

    //Map of items and counters 
    Map<Set<Integer>, Integer> hset = new HashMap<>();
    Scanner sc = new Scanner(System.in);

    //Read line by line with the scanner, then process each line, since when you call
    //nextInt() ad end of line the scanner will eat up EOL and silently proceed with
    //the next line
    while(sc.hasNextLine()) {
        String line=sc.nextLine();
        if(line.equals("*")) {
            break;
        }
        //I kept the variable name, but please don't name 'list' a set!
        Set<Integer> list = new HashSet<>();
        for (String num: line.split(" ")) {
            num=num.trim();
            if (num.length()>0) {
                list.add(Integer.parseInt(num));
            }
        }

        //Just in case of blank lines
        if (list.isEmpty()) continue;

        //Check if the set is already in the map and get the counter
        Integer count=hset.get(list);
        if (count==null) {
            //Not found, new counter
            count=0;
        }
        //Increase the counter (both new and old ones)
        hset.put(list, count+1);
    }

    //Print
    hset.entrySet().forEach((e)-> System.out.println(e.getKey()+"="+e.getValue()));
}

相关问题