0% found this document useful (0 votes)
7 views

LinkedHashMap

This cheat sheet provides an overview of the Java LinkedHashMap, including import statements, creation, basic operations, properties, and iteration methods. It highlights the differences between LinkedHashMap, HashMap, and TreeMap, and includes sample code demonstrating its use. Generics are also explained, specifying key and value types.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

LinkedHashMap

This cheat sheet provides an overview of the Java LinkedHashMap, including import statements, creation, basic operations, properties, and iteration methods. It highlights the differences between LinkedHashMap, HashMap, and TreeMap, and includes sample code demonstrating its use. Generics are also explained, specifying key and value types.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java LinkedHashMap Cheat Sheet

------------------------------------------------------------

1. Import Statement

import java.util.LinkedHashMap;

------------------------------------------------------------

2. Creating a LinkedHashMap

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();


LinkedHashMap<Integer, String> map2 = new LinkedHashMap<>();

------------------------------------------------------------

3. Basic Operations

map.put("Apple", 10); // Add or update key-value


map.get("Apple"); // Get value by key
map.remove("Apple"); // Remove entry
map.containsKey("Apple"); // Check if key exists
map.containsValue(10); // Check if value exists
map.size(); // Number of entries
map.isEmpty(); // Check if empty
map.clear(); // Remove all entries

------------------------------------------------------------

4. Properties

- Maintains insertion order of keys


- Allows one null key
- Allows multiple null values

------------------------------------------------------------

5. Iterating Over LinkedHashMap

// Keys
for (String key : map.keySet()) {
System.out.println(key);
}

// Values
for (Integer value : map.values()) {
System.out.println(value);
}

// Key-Value pairs
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}

------------------------------------------------------------

6. LinkedHashMap vs HashMap vs TreeMap


LinkedHashMap - Maintains insertion order
HashMap - Unordered, faster
TreeMap - Sorted by key, slower

------------------------------------------------------------

7. Sample Code

LinkedHashMap<String, Integer> marks = new LinkedHashMap<>();


marks.put("Math", 95);
marks.put("English", 88);
System.out.println(marks); // Preserves insertion order

------------------------------------------------------------

8. Generics

LinkedHashMap<K, V>
K - key type
V - value type

Example:
LinkedHashMap<Character, Boolean> vowels = new LinkedHashMap<>();

You might also like