TreeMap Set> entrySet() Method Example Program


Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order. 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.TreeMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author: Hari Haran
 * @discription: The entrySet() method is used to return a Set view of the
 *               mappings contained in this map. The set's iterator returns the
 *               entries in ascending key order. The set is backed by the map,
 *               so changes to the map are reflected in the set, and vice-versa.
 */

public class TreeMapEntrySet {
	public static void main(String[] args) {
		TreeMap<Integer, String> tm = 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");

		Set<Entry<Integer, String>> s = tm.entrySet();

		System.out.println("Checking value");
		System.out.println("Entry set values: " + s);
	}
}

Output

Checking value
Entry set values: [1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal]

Explanation

public Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order. 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 interface SortedMap<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