LinkedHashMap is a type of data structure in Java that is similar to HashMap, but it also keeps track of the order in which elements were inserted. This means that you can access the elements in the order they were added. It does this by using a doubly-linked list with the hash table, which makes it fast and efficient.
Some important methods in LinkedHashMap are:
get(key)
: gets the value associated with the given keyput(key, value)
: adds a key-value pair to the mapcontainsKey(key)
: checks if the map contains the given keysize()
: returns the size of the mapisEmpty()
: checks if the map is emptycontainsValue(value)
: checks if the map contains the given value
Here's an example of how to use LinkedHashMap:
java// Initialization of a LinkedHashMap
LinkedHashMap<Integer, String> m = new LinkedHashMap<>();
// Inserting the elements
m.put(10, "GfG");
m.put(20, "IDE");
m.put(15, "Courses");
// Outputs the ordered map
System.out.println(m);
This will output: {10=GfG, 20=IDE, 15=Courses}
If you insert a key-value pair with the same key as one that already exists, the value will be updated:
java// Initialization of a LinkedHashMap
LinkedHashMap<Integer, String> m = new LinkedHashMap<>();
// Inserting the elements
m.put(10, "GfG");
m.put(20, "IDE");
m.put(15, "Courses");
// Updates the value for key 20
m.put(20, "Practice");
// Outputs the updated map
System.out.println(m);
This will output: {10=GfG, 20=Practice, 15=Courses}
You can also remove elements from the map using the remove(key)
method:
java// Initialization of a LinkedHashMap
LinkedHashMap<Integer, String> m = new LinkedHashMap<>();
// Inserting the elements
m.put(10, "GfG");
m.put(20, "IDE");
m.put(15, "Courses");
// Removes the element with key 20
m.remove(20);
// Adds a new element to the end of the map
m.put(20, "Practice");
// Outputs the updated map
System.out.println(m);
This will output: {10=GfG, 15=Courses, 20=Practice}
There's also a special constructor for LinkedHashMap that is used for implementing the Least Recently Used (LRU) Cache. It maintains the ordering according to access, rather than insertion:
java// Initialization of a LinkedHashMap with access order
LinkedHashMap<Integer, String> m = new LinkedHashMap<>(4, 0.6f, true);
// Inserting the elements
m.put(10, "GfG");
m.put(20, "IDE");
m.put(15, "Courses");
// Moves the element with key 10 to the end of the map
System.out.println(m.get(10));
// Outputs the updated map
System.out.println(m);
This will output: GfG
and {20=IDE, 15=Courses, 10=GfG}
Now, let's try to modify the value associated with the key "GfG" in the HashMap. We can do this using the put
method again:
csharphm.put("GfG", "GeeksforGeeks");
System.out.println(hm.get("GfG"));
System.out.println(hm);
This will output:
GeeksforGeeks {GfG=GeeksforGeeks, 20=IDE, 15=Courses, 10=GfG}
As you can see, the value associated with the key "GfG" has been modified to "GeeksforGeeks".
We can also remove a key-value pair from the HashMap using the remove
method:
csharphm.remove(10);
System.out.println(hm);
This will output:
{GfG=GeeksforGeeks, 20=IDE, 15=Courses}
As you can see, the key-value pair with key 10 has been removed from the HashMap.
HashMaps are very useful data structures for storing key-value pairs, and are commonly used in programming. They have constant-time average-case performance for most operations, making them efficient for large datasets. However, it is important to note that the order of elements in a HashMap is not guaranteed, and may change over time. If you need to maintain the order of elements, you may want to consider using a different data structure, such as a LinkedHashMap.