How does the ‘accessOrder’ Configuration in LinkedHashMap Work?
Last Updated :
16 Feb, 2024
In Java Programming, LinkedHashMap is a pre-defined class, and it can be extended HashMap. It provides the hash table with a predictable iteration order. LinkedHashMap is a part of the java.util package and it maintains a doubly linked list.
In this article, we will learn how the accessOrder configuration in LinkedHashMap works in Java.
Syntax:
LinkedHashMap<K, V> map = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
- K can be defined as the types of the keys in the map.
- V can be defined as the types of the values in the map.
- intialCapacity can be defined as an integer that specifies the initial capacity of the LinkedHashMap.
- loadFactory can be defined as the specifying the load factor of the LinkedHashMap.
- accessOrder can be defined as indicating whether the LinkedHashMap maintains the access order or insertion order.
Step-by-Step Implementation
- Create the class named as the GfGAccessOrder and write the main method into the class.
- Create the instance of the LinkedHashMap and named as the linkedHashMap.
- Adding the element into the LinkedHashMap using the pre-defined put method.
- Performing the three-time accessing operation into the LinkedHashMap.
- Print the result.
Note: LinkedHashMap elements will be printed based on the access order you can perform on the LinkedHashMap then first access element will be print first second access be second etc.
Program to Know the Working of accessOrder Configuration in LinkedHashMap in Java
Below is the code implementation of accessOrder configuration in LinkedHashMap.
Java
import java.util.LinkedHashMap;
import java.util.Map;
public class GfGAccessOrder {
public static void main(String[] args)
{
Map<Integer, String> linkedHashMap = new LinkedHashMap<>( 16 , 0 .75f, true );
linkedHashMap.put( 1 , "One" );
linkedHashMap.put( 2 , "Two" );
linkedHashMap.put( 3 , "Three" );
linkedHashMap.get( 2 );
linkedHashMap.get( 1 );
linkedHashMap.get( 3 );
System.out.println( "Entries in LinkedHashMap with access order:" );
for (Map.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
|
Output
Entries in LinkedHashMap with access order:
2: Two
1: One
3: Three
Explanation of the Program:
The above program is an example of access order configuration in LinkedHashMap. Enabling access order is the creation of the instance of the LinkedHashMap.
Map<Integer, String> linkedHashMap = new LinkedHashMap<>(16, 0.75f, true);
If we observe, the creation of the LinkedHashMap contains three parameters:
- Parameter 1 (16) can be defined as the initial capacity of the LinkedHashMap, and it specifies the initial number of buckets in the hash table.
- Parameter 2 (0.75f) can be defined as the load factor, which then determines when the LinkedHashMap will resize itself.
- Parameter 3 (true) can enable the access order it sets to be true, and then the LinkedHashMap maintains the order of entries based on the access order.
In the above program, which one will be accessed first, once we set the access order to true, values will print based on the access order into the program.
Share your thoughts in the comments
Please Login to comment...