Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashMap with Int Array

Tags:

I am using this code to check that array is present in the HashMap:

public class Test {     public static void main(String[] arg) {         HashMap<int[], String> map = new HashMap<int[], String>();         map.put(new int[]{1, 2}, "sun");         System.out.println(map.containsKey((new int[]{1, 2})));     } } 

But this prints False. How can I check that array is present in the HashMap?

like image 418
Sunil Avatar asked Apr 13 '10 08:04

Sunil


People also ask

Can we use array in HashMap in Java?

In a HashMap, keys and values can be added using the HashMap. put() method. We can also convert two arrays containing keys and values into a HashMap with respective keys and values.

How do you initialize an array in Java?

Array Initialization in Java To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size: int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable.

What can you do with arrays in Java?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.


1 Answers

The problem is because the two int[] aren't equal.

System.out.println(     (new int[] { 1, 2 }).equals(new int[] { 1, 2 }) ); // prints "false" 

Map and other Java Collections Framework classes defines its interface in terms of equals. From Map API:

Many methods in Collections Framework interfaces are defined in terms of the equals method. For example, the specification for the containsKey(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))."

Note that they don't have to be the same object; they just have to be equals. Arrays in Java extends from Object, whose default implementation of equals returns true only on object identity; hence why it prints false in above snippet.


You can solve your problem in one of many ways:

  • Define your own wrapper class for arrays whose equals uses java.util.Arrays equals/deepEquals method.
    • And don't forget that when you @Override equals(Object), you must also @Override hashCode
  • Use something like List<Integer> that does define equals in terms of the values they contain
  • Or, if you can work with reference equality for equals, you can just stick with what you have. Just as you shouldn't expect the above snippet to ever print true, you shouldn't ever expect to be able to find your arrays by its values alone; you must hang-on to and use the original references every time.

See also:

  • Overriding equals and hashCode in Java
  • How to ensure hashCode() is consistent with equals()?
  • Understanding the workings of equals and hashCode in a HashMap

API

  • Object.equals and Object.hashCode
    • It's essential for a Java programmer to be aware of these contracts and how to make them work with/for the rest of the system
like image 51
polygenelubricants Avatar answered Oct 21 '22 00:10

polygenelubricants