WeakHashMap Set> entrySet() method Example Program


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.

Program

package com.candidjava;

import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/* 
 * @author : Mohan 
 * @description : The entrySet() method is used to return a collection view of the mappings contained in this map.
 */
public class WeakHashMapEntrySet {
	public static void main(String[] args) {
		Map<String, String> weakHashMap = new WeakHashMap<String, String>();

		weakHashMap.put("1", "anandh");
		weakHashMap.put("2", "hari");
		weakHashMap.put("3", "vinoth");
		weakHashMap.put("4", "kamal");
		weakHashMap.put("5", "karthic");
		weakHashMap.put("6", "mohan");

		System.out.println("Map: " + weakHashMap);
		Set set = weakHashMap.entrySet();
		System.out.println("Set: " + set);
		weakHashMap.put("3", "ramki");
		System.out.println("Changed Map: " + weakHashMap);
		System.out.println("Changed Set: " + set);

	}
}

Output

Map: {4=kamal, 5=karthic, 6=mohan, 1=anandh, 2=hari, 3=vinoth}
Set: [4=kamal, 5=karthic, 6=mohan, 1=anandh, 2=hari, 3=vinoth]
Changed Map: {4=kamal, 5=karthic, 6=mohan, 1=anandh, 2=hari, 3=ramki}
Changed Set: [4=kamal, 5=karthic, 6=mohan, 1=anandh, 2=hari, 3=ramki]

Explanation

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.
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>
Returns:
a set view of the mappings contained in this map


Related Post

Comments


©candidjava.com