Collections

LinkedHashMap in Java Code For All Methods

Table of Contents

Program

import java.util.LinkedHashMap;
public class LinkedHashMapTypes
       {
       public static void main(String[] args)
                {
		LinkedHashMap<Integer, String> mapping = new LinkedHashMap<Integer, String>();
		mapping.put(1, "ok");
		mapping.put(2, "i");
		mapping.put(3, "am");
		mapping.put(4, "fine");
		mapping.put(5, "i");
		mapping.put(6, "am");
		mapping.put(7, "ok");
		
                //containsValue​(Object value)
                boolean valueExists = mapping.containsValue("Two");
		System.out.println(valueExists);
		valueExists = mapping.containsValue("Three");
		
                //values()
                System.out.println(valueExists);
		
                //keySet()
                System.out.println("Keys: " + mapping.keySet());
		System.out.println("Values: " + mapping.values());
		
                //entrySet()
                System.out.println("Key-Value pairs: " + mapping.entrySet());
		
                //get​(Object key)
                System.out.println("The Value is: " + mapping.get(2));
		}
	}

Output

false
false
Keys: [1, 2, 3, 4, 5, 6, 7]
Values: [ok, i, am, fine, i, am, ok]
Key-Value pairs: [1=ok, 2=i, 3=am, 4=fine, 5=i, 6=am, 7=ok]
The Value is: i

Description

public boolean containsValue​(Object value)

Returns true if this map maps one or more keys to the specified value.

Specified by:

containsValue in interface Map<K, V>

Overrides:

containsValue in class HashMap<K, V>

Parameters:

value – value whose presence in this map is to be tested

Returns:

true if this map maps one or more keys to the specified value

public Set<Map. Entry<K, V>> entrySet()

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. Its Spliterator typically provides faster sequential performance but much poorer parallel performance than that of HashMap.

Specified by:

entrySet in interface Map<K, V>

Overrides:

entrySet in class HashMap<K, V>

Returns:

a set view of the mappings contained in this map

public Set<K> keySet()

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations. Its Spliterator typically provides faster sequential performance but much poorer parallel performance than that of HashMap.

Specified by:

keySet in interface Map<K, V>

Overrides:

keySet in class HashMap<K, V>

Returns:

a set view of the keys contained in this map

public Collection<V> values()

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. Its Spliterator typically provides faster sequential performance but much poorer parallel performance than that of HashMap.

Specified by:

values in interface Map<K, V>

Overrides:

values in class HashMap<K, V>

Returns:

a view of the values contained in this map

public V get​(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it’s also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Specified by:

get in interface Map<K, V>

Overrides:

get in class HashMap<K, V>

Parameters:

key – the key whose associated value is to be returned

Returns:

the value to which the specified key is mapped, or null if this map contains no mapping for the key