This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.
You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.
The LinkedHashMap class supports five constructors. The first form constructs a default LinkedHashMap:
LinkedHashMap( )
The second form initializes the LinkedHashMap with the elements from m:
LinkedHashMap(Map m)
The third form initializes the capacity:
LinkedHashMap(int capacity)
The fourth form initializes both capacity and fill ratio. The meaning of capacity and fill ratio are the same as for HashMap:
LinkedHashMap(int capacity, float fillRatio)
The last form allows you to specify whether the elements will be stored in the linked list by insertion order, or by order of last access. If Order is true, then access order is used. If Order is false, then insertion order is used.
LinkedHashMap(int capacity, float fillRatio, boolean Order)
Hierarchy of LinkedHashMap class:
SN | Methods with Description |
---|---|
1 | void clear() Removes all mappings from this map. |
2 | boolean containsKey(Object key) Returns true if this map maps one or more keys to the specified value. |
3 | Object get(Object key) Returns the value to which this map maps the specified key. |
4 | protected boolean removeEldestEntry(Map.Entry eldest) Returns true if this map should remove its eldest entry. |
Example of LinkedHashMap class:
import java.util.*; class Simple{ public static void main(String args[]){ LinkedHashMap hm=new LinkedHashMap(); hm.put(100,"Dinesh"); hm.put(101,"Sweety"); hm.put(102,"Nimmo"); Set set=hm.entrySet(); Iterator itr=set.iterator(); while(itr.hasNext()){ Map.Entry m=(Map.Entry)itr.next(); System.out.println(m.getKey()+" "+m.getValue()); } } }
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…