LinkedHashMap is a class which is available in java.util package. The LinkedHashMap is a class of the Java collections framework provides the functionality of hash table and linked list. In Java LinkedHashMap, elements are stored in key/value pairs. Keys are unique values associated with individual values. LinkedHashMap cannot contains duplicate keys.
LinkedHashMap Syntax:
LinkedHashMap<K, V> numbers = new LinkedHashMap<>();
LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
By default,
Method Name | Description |
---|---|
put() | inserts the specified key/value mapping to the map. |
putAll() | inserts all the entries from the specified map to this map. |
putIfAbsent() | inserts the specified key/value mapping to the map if the specified key is not present in the map. |
entrySet() | returns a set of all the key/value mapping of the map. |
keySet() | returns a set of all the keys of the map. |
values() | returns a set of all the values of the map. |
get() | Returns the value associated with the specified key. If the key is not found, it returns null. |
getOrDefault() | Returns the value associated with the specified key. If the key is not found, it returns the specified default value. |
remove(key) | returns and removes the entry associated with the specified key from the map. |
remove(key, value) | removes the entry from the map only if the specified key mapped to be the specified value and return a boolean value. |
clear() | removes all the entries from the map. |
containsKey() | checks if the map contains the specified key and returns a boolean value. |
containsValue() | checks if the map contains the specified value and returns a boolean value. |
size() | returns the size of the map. |
isEmpty() | checks if the map is empty and returns a boolean value. |
import java.util.LinkedHashMap;
class Main {
public static void main(String[] args) {
// Creating LinkedHashMap of even numbers
LinkedHashMap<String, Integer> evenNumbers = new LinkedHashMap<>();
// Using put()
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
System.out.println("Original LinkedHashMap: " + evenNumbers);
// Using putIfAbsent()
evenNumbers.putIfAbsent("Six", 6);
System.out.println("Updated LinkedHashMap(): " + evenNumbers);
//Creating LinkedHashMap of numbers
LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
numbers.put("One", 1);
// Using putAll()
numbers.putAll(evenNumbers);
System.out.println("New LinkedHashMap: " + numbers);
}
}
import java.util.LinkedHashMap;
class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("LinkedHashMap: " + numbers);
// Using entrySet()
System.out.println("Key/Value mappings: " + numbers.entrySet());
// Using keySet()
System.out.println("Keys: " + numbers.keySet());
// Using values()
System.out.println("Values: " + numbers.values());
// Using get()
int value1 = numbers.get("Three");
System.out.println("Returned Number: " + value1);
// Using getOrDefault()
int value2 = numbers.getOrDefault("Five", 5);
System.out.println("Returned Number: " + value2);
}
}
import java.util.LinkedHashMap;
class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("LinkedHashMap: " + numbers);
// remove method with single parameter
int value = numbers.remove("Two");
System.out.println("Removed value: " + value);
// remove method with two parameters
boolean result = numbers.remove("Three", 3);
System.out.println("Is the entry Three removed? " + result);
System.out.println("Updated LinkedHashMap: " + numbers);
}
}
Both the LinkedHashMap and the HashMap implements the Map interface. However, there exist some differences between them.