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 that the garbage collector is allowed to break the reference; i.e. replace the reference, it contains, with null value. And the rule is that a weak reference to an object…

Continue

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 or PhantomReference. Garbage collector reclaims memory eagerly from an object if WeakReference is pointing to it. On the other hand, an object with SoftReference is garbage collected if JVM absolutely…

Continue

NavigableMap in Java

A NavigableMap in Java extends a SortedMap¬†with navigation methods returning the closest matches for given search targets. Methods lowerEntry(), floorEntry(), ceilingEntry(), and higherEntry() return Map.Entry objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning null if there is no such key. Similarly, methods lowerKey(), floorKey(), ceilingKey(), and higherKey() return only the associated keys. All of these methods are designed for locating, not traversing entries. A NavigableMap may be accessed and traversed in either ascending or descending key…

Continue

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 how you can create your own HashMap though in this example I have used some code snippets from Java’s HashMap API. We know also how HashMap works internally and here…

Continue

Collect and Convert Objects using Lambda Expression in Java 8

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. We 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 from database tables you want to apply some logic and want to map to custom Data Transfer Objects.

Continue

Custom Object as a Key in HashMap

Introduction 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 tell Java how to compare user defined objects by overriding the hashCode() and equals() methods. Most of the time we generally use Java PI provided classes (i.e., String) as keys for HashMap. So in situations, where you need to use custom object as a key in HashMap then you can use this example.

Continue

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

Continue