TreeMap Collection values() Method Example Program


Returns a Collection view of the values contained in this map. The collection's iterator returns the values in ascending order of the corresponding keys. 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.TreeMap;
import java.util.Collection;
import java.util.SortedMap;

/**
 * @author: Hari Haran
 * @discription: The values() method is used to return a Collection view of the
 *               values contained in this map. The collection's iterator returns
 *               the values in ascending order of the corresponding keys.
 */

public class TreeMapValues {
	public static void main(String[] args) {
		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
		SortedMap<Integer, String> tmin = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		System.out.println("Getting collection from the map");
		Collection coll = tm.values();
		System.out.println("Value of the collection: " + coll);
	}
}

Output

Getting collection from the map
Value of the collection: [Anand, Hari Haran, Karthikeyan, Mohan, Vinoth, Hari Haran, Kamal]

Explanation

public Collection<V> values()
Returns a Collection view of the values contained in this map. The collection's iterator returns the values in ascending order of the corresponding keys. 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>
Specified by:
values in interface SortedMap<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