Guava ImmutableMap has noticeably slower access than HashMap

As Louis Wasserman said, ImmutableMap is not optimized for objects with slow equals method. I think the main difference is here: HashMap: if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; ImmtubleMap: if (key.equals(candidateKey)) { return entry.getValue(); As you can see, to check for collisions, HashMap first check the hashes. …

Read more

Java HashMap vs JSONObject

As you said, JSONObject is backed by a HashMap. Because of this, performance will be almost identical. JSONObject.get() adds a null check, and will throw an exception if a key isn’t found. JSONObject.put() just calls map.put(). So, there is almost no overhead. If you are dealing with JSON objects, you should always use JSONObject over …

Read more

How to make HashMap work with Arrays as key?

You cannot do it this way. Both t and a will have different hashCode() values because the the java.lang.Array.hashCode() method is inherited from Object, which uses the reference to compute the hash-code (default implementation). Hence the hash code for arrays is reference-dependent, which means that you will get a different hash-code value for t and …

Read more

How can I have a HashMap with unique keys in java?

Hash map key is unique. Add duplicate key, then it will be overwritten. HashMap hm = new HashMap(); hm.put(“1”, new Integer(1)); hm.put(“2”, new Integer(2)); hm.put(“3”, new Integer(3)); hm.put(“4”, new Integer(4)); hm.put(“1”, new Integer(5));// value integer 1 is overwritten by 5 By default Hashmap is not synchronized.