Java映射:如何打印每个值的出现次数以及共享该值的键

Java映射:如何打印每个值的出现次数以及共享该值的键,java,maps,Java,Maps,我正试图解决我遇到的一个开源项目上的问题,但我没有太多使用地图的经验。任何帮助都将不胜感激。发布了一个答案,看看是否有帮助。 2 occurrences of error1: item1, item2 3 occurrences of error3: item3, item4, ... ... <number of occurrences of an error> occurrences of <error name>: <the items that have

我正试图解决我遇到的一个开源项目上的问题,但我没有太多使用地图的经验。任何帮助都将不胜感激。

发布了一个答案,看看是否有帮助。
2 occurrences of error1: item1, item2 
3 occurrences of error3: item3, item4, ...
...
<number of occurrences of an error> occurrences of <error name>: <the items that have that error>
<number of occurrences of an error> occurrences of <error name>: <the items that have that error>
<"..." if there are more than 2 types of errors>
2 occurrences of error1: item1, item2 
3 occurrences of error3: item3, item4, ...
...
public static void main(String[] args) throws IOException  {
    Map<String, String> p = new HashMap<String, String>();
    p.put("item1","error1");
    p.put("item2","error1");
    p.put("item3","error3");
    p.put("item4","error3");
    p.put("item5","error3");
    p.put("item6","error4");
    
    Map<String, String> resultMap = new HashMap<String, String>();
    for(Map.Entry<String, String> entry: p.entrySet()) {
        if(resultMap.containsKey(entry.getValue())) {
            String result = resultMap.get(entry.getValue())+", "+entry.getKey();
            if(result.split(",").length <= 2) { //For less than 2 Occurrence
                resultMap.put(entry.getValue(), result);
            }else {
                resultMap.put(entry.getValue(), resultMap.get(entry.getValue())+", ...");
            }
        }else {
            resultMap.put(entry.getValue(), entry.getKey());
        }
    }
    //Printing the result Map
    for(Map.Entry<String, String> entry: resultMap.entrySet()) {
        System.out.println(entry.getKey()+" -> "+entry.getValue());
    }
}
error4 -> item6
error1 -> item2, item1
error3 -> item4, item3, ...