Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  When will resizing be triggered in Java HashMap?

HashMap is one of the most frequently used collection types in Java, it stores key-value pairs. Ideally it expects to use hash table which expects the data access time complexity to be O(1), however, due to hash conflicts, in reality, it uses linked list or red-black tree to store data which makes the worst case time complexity to be O(logn). Although collections are using data structures like arrays and linked lists, unlike arrays, they will dynamically resize when there is not enough space to store data  It involves copying data from old array to the new array which is considered a...

16,938 1       JAVA HASHMAP RESIZE THRESHOLD


  Why accessing Java HashMap may cause infinite loop in concurrent environment

HashMap in Java is frequently used to handle key/value pairs. But it is not a good candidate to be used in concurrent environment where ConcurrentHashMap should be used instead. If used in concurrent environment, it may cause lots of unexpected behavior even including make the program getting into an infinite loop situation.To understand how this can happen, let's first discuss how HaspMap works internally. In this post we will use implementation of HashMap before Java 8 as example, Java 8 provides a different version of HashMap implementation which is more efficient but more complicated as we...

8,191 0       JAVA HASHMAP INFINITE LOOP


  Why exception would be thrown when deleting element while looping through HashMap in Java

HashMap and other Collection types are frequently used in Java application programming. This post will explain why exception would be thrown when deleting element with Map.remove() while looping through a Map using Iterator. This issue would also occur to other Collection types such as Set, List.Below is a sample code snippet demonstrating the exception thrown.Map<String,String> map = Collections.synchronizedMap(new TreeMap<String,String>()); map.put("key1","value1");map.put("key2","value2");map.put("key3","value3"); Set<Entry<String,String>> entries = map.entrySet(); I...

3,408 0       JAVA HASHMAP CONCURRENTMODIFICATIONEXCEPTION