📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Important Key Points about Java LinkedHashMap
Order of elements:
Null Keys and Values:
Not Synchronized:
Performance:
Usage:
Create LinkedHashMap
// Creating a LinkedHashMap LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();
Add Elements to LinkedHashMap
// Creating a LinkedHashMap
LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();
// Adding new key-value pairs to the LinkedHashMap
dayNumberMapping.put("Mon", 1);
dayNumberMapping.put("Tus", 2);
dayNumberMapping.put("Wen", 3);
dayNumberMapping.put("Thu", 4);
dayNumberMapping.put("Fri", 5);
dayNumberMapping.put("Sat", 6);
System.out.println(dayNumberMapping);
{Mon=1, Tus=2, Wen=3, Thu=4, Fri=5, Sat=6, Sun=7}
Access Elements from LinkedHashMap
We can access elements from a LinkedHashMap using the get() method.import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Create a LinkedHashMap
Map<String, Integer> students = new LinkedHashMap<>();
// Add elements to the LinkedHashMap
students.put("John", 12);
students.put("Alice", 15);
students.put("Bob", 14);
// Access elements from the LinkedHashMap
int johnAge = students.get("John");
int aliceAge = students.get("Alice");
int bobAge = students.get("Bob");
System.out.println("John's Age: " + johnAge);
System.out.println("Alice's Age: " + aliceAge);
System.out.println("Bob's Age: " + bobAge);
}
}
Output:
John's Age: 12
Alice's Age: 15
Bob's Age: 14
Remove Elements from LinkedHashMap
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Create a LinkedHashMap
Map<String, Integer> students = new LinkedHashMap<>();
// Add elements to the LinkedHashMap
students.put("John", 12);
students.put("Alice", 15);
students.put("Bob", 14);
System.out.println("Initial LinkedHashMap: " + students);
// Remove the entry with key 'Alice'
students.remove("Alice");
System.out.println("LinkedHashMap after removing Alice: " + students);
}
}
Output:
Initial LinkedHashMap: {John=12, Alice=15, Bob=14}
LinkedHashMap after removing Alice: {John=12, Bob=14}
Search Elements in LinkedHashMap
Searching for elements in a LinkedHashMap can be performed using the containsKey() and containsValue() methods.import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
// Create a LinkedHashMap
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
// Add some elements to the LinkedHashMap
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Search for a key in the LinkedHashMap
boolean exists = map.containsKey("Two");
System.out.println("Does key 'Two' exist? " + exists);
// Search for a value in the LinkedHashMap
exists = map.containsValue(3);
System.out.println("Does value '3' exist? " + exists);
}
}
Output:
Does key 'Two' exist? true
Does value '3' exist? true
Iterate or Loop over a LinkedHashMap
Iterate over a LinkedHashMap using Java 8 forEach and lambda expression:
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Using Java 8 forEach and lambda expression
map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
}
}
Output:
Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value: 3
Iterate over a LinkedHashMap’s entrySet using Java 8 forEach and lambda expression:
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Using Java 8 forEach and lambda expression over entrySet
map.entrySet().forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
}
}
Output:
Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value: 3
Iterate over a LinkedHashMap’s entrySet using iterator():
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Using iterator over entrySet
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
Output:
Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value: 3
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Using iterator and Java 8 forEachRemaining() method over entrySet
map.entrySet().iterator().forEachRemaining(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
}
}
Output:
Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value: 3
LinkedHashMap with User-Defined Objects
import java.util.LinkedHashMap;
import java.util.Map;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
LinkedHashMap<Integer, Person> map = new LinkedHashMap<>();
// Adding elements to the map
map.put(1, new Person("Alice", 25));
map.put(2, new Person("Bob", 30));
map.put(3, new Person("Charlie", 35));
// Iterating over the map
for(Map.Entry<Integer, Person> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
Output:
Key: 1, Value: Person{name='Alice', age=25}
Key: 2, Value: Person{name='Bob', age=30}
Key: 3, Value: Person{name='Charlie', age=35}
Comments
Post a Comment
Leave Comment