LinkedHashMap in Java extends HashMap to store key-value pairs in insertion order, which means the order of elements is maintained based on the sequence in which they were inserted. It combines the features of HashMap with a linked list implementation of the entries, allowing predictable iteration order.
import java.util.LinkedHashMap;
// Syntax for creating a LinkedHashMap
LinkedHashMap<KeyType, ValueType> mapName = new LinkedHashMap<>();
Code language: JavaScript (javascript)
Methods:
- Adding Elements:
- boolean add(element): Adds the specified element to the set if it is not already present.
- Removing Elements:
- boolean remove(Object o): Removes the specified element from this set if it is present (optional operation).
- void clear(): Removes all of the elements from this set (optional operation).
- Querying Set Information:
- int size(): Returns the number of elements in this set.
- boolean isEmpty(): Returns true if this set contains no elements.
- Checking Membership:
- boolean contains(Object o): Returns true if this set contains the specified element.
- Bulk Operations:
- boolean containsAll(Collection<?> c): Returns true if this set contains all of the elements of the specified collection.
- boolean removeAll(Collection<?> c): Removes from this set all of its elements that are contained in the specified collection (optional operation).
- boolean retainAll(Collection<?> c): Retains only the elements in this set that are contained in the specified collection (optional operation).
- Conversion to Array:
- Object[] toArray(): Returns an array containing all of the elements in this set.
- Iteration:
- Iterator<E> iterator(): Returns an iterator over the elements in this set.
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapExample { public static void main(String[] args) { // Creating a LinkedHashMap to store student names and their roll numbers LinkedHashMap<String, Integer> studentMap = new LinkedHashMap<>(); // Adding students to the LinkedHashMap studentMap.put("Paani", 101); studentMap.put("Mahesh", 102); studentMap.put("Datta", 103); studentMap.put("Ganesh", 104); studentMap.put("Harsha", 105); // Displaying the LinkedHashMap System.out.println("LinkedHashMap of students:"); System.out.println(studentMap); // Removing a student from the LinkedHashMap Integer removedRollNumber = studentMap.remove("Datta"); System.out.println("\nRemoved student 'Datta' with roll number: " + removedRollNumber); // Displaying the updated LinkedHashMap System.out.println("\nUpdated LinkedHashMap of students:"); System.out.println(studentMap); // Checking if a student exists in the LinkedHashMap String searchStudent = "Harsha"; boolean containsStudent = studentMap.containsKey(searchStudent); System.out.println("\nDoes the LinkedHashMap contain student '" + searchStudent + "'? " + containsStudent); // Iterating over the entries of the LinkedHashMap System.out.println("\nIterating over LinkedHashMap entries:"); for (Map.Entry<String, Integer> entry : studentMap.entrySet()) { System.out.println("Student: " + entry.getKey() + ", Roll Number: " + entry.getValue()); } // Size of the LinkedHashMap System.out.println("\nSize of the LinkedHashMap: " + studentMap.size()); // Clearing the LinkedHashMap studentMap.clear(); System.out.println("\nIs the LinkedHashMap empty now? " + studentMap.isEmpty()); } }
D:\>javac HashMapExample.java
D:\>java HashMapExample
HashMap of students:
{Ganesh=104, Datta=103, Mahesh=102, Harsha=105, Paani=101}
Removed student 'Datta' with roll number: 103
Updated HashMap of students:
{Ganesh=104, Mahesh=102, Harsha=105, Paani=101}
Does the HashMap contain student 'Harsha'? true
Iterating over HashMap entries:
Student: Ganesh, Roll Number: 104
Student: Mahesh, Roll Number: 102
Student: Harsha, Roll Number: 105
Student: Paani, Roll Number: 101
Size of the HashMap: 4
Is the HashMap empty now? true
Code language: JavaScript (javascript)
LinkedHashSet in Java combines the uniqueness property of HashSet with the predictable iteration order provided by a linked list. It offers efficient performance for basic operations and guarantees the order of elements based on insertion sequence. Understanding when to use LinkedHashSet based on its features can help in designing effective data structures and algorithms in Java applications.