Here we will discuss about how an entry gets garbage collected from WeakhashMap. You may check first how garbage collection works in Java. WeakHashMap uses a special class called WeakReference to refer to the keys. A weak reference is an object that acts like an indirect reference (a reference to an object holding another reference)….

Posted in Java

How Garbage Collection works in WeakHashMap

Here we are going to discuss about the StrongReference, PhantomReference, WeakReference and SoftReference. Reference classes are important in the context of how garbage collection works in Java memory model. Garbage collector reclaims memory from objects which are eligible for garbage collection and this eligibility is decided upon what kind if reference is pointing to the…

Posted in Java

What are WeakReference, SoftReference, StrongReference, PhantomReference in Java

Introduction Here we will see an example on creating a custom HashMap in Java. We have seen the built-in implementation of HashMap in Java API and we know how HashMap works and its usages. The intention of this example is not to create a rigid HashMap that provides the same functionalities as Java API provides…

Posted in Java

Creating Custom HashMap in Java

In this example I am going to show you how to iterate over map of list of objects. Let’s say you have keys as strings and values as list of objects in a HashMap. Now you want to iterate list of objects and want make one attribute of the object as keys and other attribute(s)…

Posted in Java

Java 8: iterate over Map of List of Objects

Introduction In this tutorial, I will show you how you are going to collect and convert objects using lambda expression in Java 8 stream API. I will collect objects and map to another custom objects using Java 8 stream API with the help of Lambda Expression. Suppose you have entity classes generated from your database…

Posted in Java

Collect and Convert Objects using Lambda Expression in Java 8 or Later Version

Java 8 Filter This tutorial will show you how to use Java 8 stream API‘s filter() and map() methods. The filter() method in Java 8 or onward is used to produce a stream containing only a certain type of objects into the result output and map() method is used to transform a stream from one…

Posted in Java

Java 8 Stream Filter Example

In this example I am going to show you an example on using Comparator in HashMap will show you how to use Comparator to sort values in HashMap. I will use custom object as a key in the HashMap. The object which is used as an object as a key in HashMap must override hashCode()…

Posted in Java

Using Java Comparator in HashMap to Sort Elements

Introduction In this Java HashMap example I am going to tell you how to use custom object as a key in HashMap. In custom object as a key in HashMap example, I will show you how to work with user defined objects as keys in Map. To use user defined objects as keys in Map…

Posted in Java

Custom Object as a Key in HashMap

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…

Posted in Java

WeakHashMap 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…

Posted in Java

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…

Posted in Java

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…

Posted in Java

ConcurrentHashMap in Java