How Garbage Collection works in WeakHashMap

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). It has the interesting property…

What are WeakReference, SoftReference, StrongReference, PhantomReference in Java

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 object – WeakReference or SoftReference…

Creating Custom HashMap 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 but to give an idea…

Java 8: iterate over Map of List of Objects

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) as values and want to…

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

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 tables and after fetching data…

Java 8 Stream Filter Example

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 type to another type into…

Using Java Comparator in HashMap to Sort Elements

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() and equals() methods.

Custom Object as a Key in HashMap

Java Custom Object 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 you need to…

WeakHashMap in Java

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…

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