Java 如何获取和比较地图的值结果<;整数,整数>;

Java 如何获取和比较地图的值结果<;整数,整数>;,java,hashmap,Java,Hashmap,通过调用一个方法,在我的例子countInNumbers中,是以数组的形式返回结果 System.out.println(countIntInNumbers(Array)); 结果: {1=17, 2=10, 3=16, 4=17, 5=13, 6=22, 7=10, 8=15, 9=16, 10=19, 11=11, 12=15, 13=16, 14=13, 15=19, 16=17, 17=13, 18=21, 19=19, 20=15,} 我试图根据它们的总值将不同表上的数字分开。 例

通过调用一个方法,在我的例子countInNumbers中,是以数组的形式返回结果

System.out.println(countIntInNumbers(Array));
结果:

{1=17, 2=10, 3=16, 4=17, 5=13, 6=22, 7=10, 8=15, 9=16, 10=19, 11=11, 12=15, 13=16, 14=13, 15=19, 16=17, 17=13, 18=21, 19=19, 20=15,}
我试图根据它们的总值将不同表上的数字分开。 例子。。。我想显示总数在3到4之间的数字,以便将表与其他数字分开

面对这个问题,您可能会注意到结果是映射的,因为我是Java新手,在这一点上我很困惑

有人能从一开始就提出建议吗

更新::: countIntInNumbers方法如下

public static Map<Integer, Integer> countIntInNumbers(int[][] mat) {
    Map<Integer, Integer> intOccurences = new HashMap<>();
    for (int[] row : mat) {
        for (int intInRow : row) {
            Integer occurences = intOccurences.get(intInRow);
            if (occurences == null) { // first occurrence
                intOccurences.put(intInRow, 1);
            } else { // increment
                intOccurences.put(intInRow, occurences.intValue() + 1);
            }

        }

    }
    return intOccurences;
publicstaticmap countIntInNumbers(int[]]mat){
Map intoccurrences=newhashmap();
对于(int[]行:mat){
用于(int INTINTROW:行){
整数出现次数=intOccurences.get(intInRow);
如果(occurrences==null){//第一次出现
投入产出(投入产出,1);
}else{//increment
intOccurences.put(intInRow,occurences.intValue()+1);
}
}
}
再次发生;

如果要比较映射的值,只需通过键获取它。然后,由于映射的值是包装整数,因此可以使用==,>=, 我试图根据数字的总值将不同表中的数字分开。例如,我想打印总数在3到4之间的所有数字,以便将表中的数字与其他数字分开

我们不确定您在这里询问的是什么,但如果您的意思是要显示总数介于2个数字之间的数字,则可以执行以下操作:

private void printNumbers(Map<Integer, Integer> intOccurences, int minTotal, int maxTotal){
    boolean first = false;
    System.out.print("{");
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) {
        int total = entry.getValue();
        if (total >= minTotal && total <= maxTotal) {
            if (first) {
                first = false;
            } else {
                System.out.print(", ");
            }
            System.out.print(entry.getKey() + "=" + total);
        }
    }
    System.out.print("}");
}
private Map<Integer, Integer> extractNumbers(Map<Integer, Integer> intOccurences,
       int minTotal, int maxTotal) {
    Map<Integer, Integer> result = new HashMap<>();
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) {
        int total = entry.getValue();
        if (total >= minTotal && total <= maxTotal) {
            result.put(entry.getKey(), total);
        }
    }
    // not sure if you want to remove the ones from the original map
    return result;
}
private void printNumber(映射到ccurences、int minTotal、int maxTotal){
布尔优先=假;
系统输出打印(“{”);
for(Map.Entry:intOccurences.entrySet()){
int total=entry.getValue();

如果(total>=minTotal&&total=minTotal&&total您能显示
countIntInNumbers
的代码吗?我想打印总数在3到4之间的所有数字。一个数字只能有一个总数。您想说什么?在上面的示例中预期的输出是什么?我现在很困惑。我也是。Post已用方法更新;的代码仍然不清楚您现在正在尝试执行的操作。好的…方法countIntInNumbers返回2个整数,结果为1=17、2=10、3=16等…我需要获取这些结果,并根据=…之后的数字将1ts编号打印到新表中…希望我现在已清除:(条目:map.entrySet()类型不匹配:无法从元素类型对象转换为映射。Entry@Annu看看我的编辑。我仍然不确定这是否是你想要做的。但正如我说的,它应该给你一个很好的提示。非常感谢你,你真的很有帮助…我说到点子上了…当然是你提到的“我们不知道你在问什么,但如果你的意思是想显示总数在两个数字之间的数字,那么你可以这样做:“你现在可以解释一下,如何将第一个方法的结果添加到你刚刚创建的方法中,以便工作吗?什么是“第一个方法”?你的意思是
countIntInNumbers(…)
。您需要先执行此操作,然后在获得总数后,可以调用print或extract方法。
private void printNumbers(Map<Integer, Integer> intOccurences, int minTotal, int maxTotal){
    boolean first = false;
    System.out.print("{");
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) {
        int total = entry.getValue();
        if (total >= minTotal && total <= maxTotal) {
            if (first) {
                first = false;
            } else {
                System.out.print(", ");
            }
            System.out.print(entry.getKey() + "=" + total);
        }
    }
    System.out.print("}");
}
private Map<Integer, Integer> extractNumbers(Map<Integer, Integer> intOccurences,
       int minTotal, int maxTotal) {
    Map<Integer, Integer> result = new HashMap<>();
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) {
        int total = entry.getValue();
        if (total >= minTotal && total <= maxTotal) {
            result.put(entry.getKey(), total);
        }
    }
    // not sure if you want to remove the ones from the original map
    return result;
}