I came across a situation where I required to fetch data from a HashMap and add it to an ArrayList. So I will show you here how to iterate a Map and form an object and finally add it to the List using Java 8 lambda expression.
Suppose we have keys as strings and values as list of objects in a HashMap. Now we want to iterate list of objects and want make one attribute of the object as keys and other attribute(s) as values and want to put as key/value pairs in another map.
So this example is going to show you in step by step. Continue reading “Java 8: iterate over Map of Lists”
What is WeakHashMap
WeakHashMap is based on Hash table implementation of the Map interface but with weak keys.
An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
When a key has been discarded due to weakness characteristics of keys its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.
Like HashMap both null values and the null key are supported.
WeakHashMap has performance characteristics similar to the HashMap and has the same efficiency parameters of initial capacity and load factor.
Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. Therefore a key will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.
Unlike HashMap, WeakHashMap does not implement Serializable and Cloneable interface. Continue reading “WeakHashMap in Java”
What is TreeMap
It is a Red-Black tree based NavigableMap implementation.
Like HashMap it contains only unique elements.
Unlike HashMap it cannot have null key but like HashMap it can have multiple null values.
The map is sorted according to the natural ordering (ascending order) of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Continue reading “TreeMap in Java”
What is LinkedHashMap
A LinkedHashMap like HashMap is a simple yet powerful way to store and get data.
Unlike HashMap, LinkedHashMap is based on HashTable and Linked list implementation of the Map interface and stores items as key/value pairs.
Like HashMap, LinkedHashMap permits only unique keys. It also permits only one null key (whereas HashTable does not allow any null key) but may have more than one null values.
Unlike HashMap, it does make garuntee of the order of the elements stored in it.
All operations happen in LinkedHashMap(in HashMap also) are unsynchronized whereas, in HashTable all operations are synchronized.
Continue reading “LinkedHashMap in Java”
What is HashMap
A HashMap is a simple yet powerful way to store and get data.
HashMap is based on HashTable implementation, that implements the Map interface and stores items as key/value pairs.
HashMap permits only unique keys. It also permits only one null key (whereas HashTable does not allow any null key) but may have more than one null values.
It does not make garuntee of the order of the elements stored in it. For garuntee of the order please look at LinkedHashMap
All operations happen in HashMap are unsynchronized whereas, in HashTable all operations are synchronized. Continue reading “HashMap in Java”
Though we have thread-safe collections classes like HashTable, Synchronized Map, which can be used in multi-threaded environment but there are significant differences between them, which arise from the fact that how they achieve their thread-safety.
All methods of Hashtable are synchronized which make them quite slow due to the number of thread increases. Synchronized Map is also similar to the Hashtable and provides similar performance in concurrent environments. Synchronized Map can be achieved by using Collections.synchronizedMap() method.
You can check How HashMap works in java
ConcurrentHashMap is specially designed for use in concurrent environment where multiple threads can be used to access the map. By default it simultaneously allows 16 threads to read and write from Map without any external synchronization. Continue reading “ConcurrentHashMap in Java”
This tutorial will show you how to use Java 8 forEach loop to iterate Collection using Lambda Expression. Prior to Java 8 or JDK 8 we used to use the for loop though it was for-each style but in Java 8 the inclusion of forEach loop simplifies the iteration process in mainly one line.
Let’s look at the below example to see how it can be used and how simple it is using Java forEach loop.
In this tutorial I am going to show you how we can use foreach loop in Mule ESB. We will use File Connector to take an XML file as input then we will iterate each entry of XML file content using foreach loop of JAXB object.