WeakHashMap Collection values() method Example Program


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.

Program

package com.candidjava;

import java.util.Collection;
import java.util.Map;
import java.util.WeakHashMap;
/* 
 * @author : Mohan 
 * @description : The values() method is used to return a collection view of the values contained in this map.
 */
public class WeakHashMapValues {
	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 values: " + weakHashMap);

		Collection col = weakHashMap.values();
		System.out.println("Collection values: " + col);
	}
}

Output

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

Explanation

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


Related Post

Comments


©candidjava.com