Annu - 3 years ago 90
Java Question

# How to get and compare the value results of a map<Integer, Integer>

By calling a method ,at my case countInNumbers, is returning results as an array.

``````System.out.println(countIntInNumbers(Array));
``````

result:

``````{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,}
``````

I try to separate the numbers on different table dipending their total value.
example... i want to print all numbers that their total is between 3 and 4 to separate table than the other numbers

Facing this problem cause the results as you may notice are Map since i am new in Java and i am so confused at this point.

Anyone can suggest from something to start of?

Updated:::
countIntInNumbers method as follows

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

If you want to compare the value of a map just get it by key. Then since the values of the map are of wrapper Integer you can compare using ==, >=, <= since the Integer equals() method simply compares the int value it wraps with the other Integer's int value. In example:

`````` // Adding some test values to the map
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 5);
map.put(2, 6);
map.put(3, 5);

// Get values by key map.get(key)
// Compare values map.get(key) == map.get(key) or use >=, <=
System.out.println(map.get(1) <= map.get(2)); // true
System.out.println(map.get(1) == map.get(3)); // true
System.out.println(map.get(1) >= map.get(2)); // false
``````

In your countIntInNumbers it seems you are just returning and printing the map by using its toString() method. If I got you right you want to print the keys which values are between 3 and 4. In this case the values are Integer so there will not be any value between 3 and 4 other than the integers themselves.

Okay after seeing your edit, convert your raw matrix to a map and then search for the values you need, and put them into a new map. Something like this:

``````public static Map<Integer, Integer> countIntInNumbers(int[][] mat) {
Map<Integer, Integer> matConvertedToMap = new HashMap<>();

for(int i=0; i<mat.length; i++)
{
matConvertedToMap.put(mat[i][0], mat[i][1]);
}

Map<Integer, Integer> intOccurences = new HashMap<>();

for (Map.Entry<Integer, Integer> entry : matConvertedToMap.entrySet())
{
if(entry.getValue() ==  3 || entry.getValue() ==  4)
{
intOccurences.put(entry.getKey(), entry.getValue());
}
}

return intOccurences;
}
``````

Not sure what the comparison really is and what you are expected to return but that should give you a general feeling of how to iterate through the map.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download