Program
import java.util.EnumMap; import java.util.Collection; public class EnumMappWithMethods { public enum Point{One,Two,Three,Four,Five} public static void main(String[] args) { EnumMap<Point,String> map1 = new EnumMap<Point,String>(Point.class); //put​(K key, V value) map1.put(Point.One,"Value 1"); map1.put(Point.Two,"Value 2"); map1.put(Point.Three,"Value 3"); map1.put(Point.Four,"Value 4"); map1.put(Point.Five,"Value 5"); System.out.println("Enum Map :" + map1); System.out.println(""); //hashCode() System.out.println("1. Hash Code Values :" + map1.hashCode()); EnumMap<Point,String> map2 = new EnumMap<Point,String>(Point.class); System.out.println(""); //clone() map2 = map1.clone(); System.out.println("2. map1 Clone Map2 :" + map2); System.out.println(""); //clear() map1.clear(); System.out.println("3. after map1 Clear :" + map1 + "\n" + "map 2 :" + map2); String Old1 = map2.put(Point.Three,"Value 7"); String Old2 = map2.put(Point.Four,"Value 8"); System.out.println(""); System.out.println("4. put Updated map2 :" + map2); System.out.println(""); //values() Collection<String> values = map2.values(); System.out.println("5. Collection :" + map2.values()); } }
Output
Enum Map :{One=Value 1, Two=Value 2, Three=Value 3, Four=Value 4, Five=Value 5} 1. Hash Code Values :870755000 2. map1 Clone Map2 :{One=Value 1, Two=Value 2, Three=Value 3, Four=Value 4, Five=Value 5} 3. after map1 Clear :{} map 2 :{One=Value 1, Two=Value 2, Three=Value 3, Four=Value 4, Five=Value 5} 4. put Updated map2 :{One=Value 1, Two=Value 2, Three=Value 7, Four=Value 8, Five=Value 5} 5. Collection :[Value 1, Value 2, Value 7, Value 8, Value 5]
Description
public int hashCode()
Returns the hash code value for this map. The hash code of a map is defined to be the sum of
the hash codes of each entry in the map.
Specified by:
hashCode in interface Map<K extends Enum<K>, V>
Overrides:
hashCode in classs AbstractMap<K extends Enum<K>, V>
Returns:
the hash code value for this map
See Also:
Map. Entry. hashCode (), Object.equals (Object) , Set.Equal(object)
public EnumMap clone()
Returns a shallow copy of this enum map. The values themselves are not cloned.
Overrides:
clone in class AbstractMap<K extends Enum<K>,V>
Returns:
a shallow copy of this enum map
See Also:
cloneable
public void clear()
Removes all mappings from this map.
Specified by:
clear in interface Map<K extends Enum<K>,V>
Overrides:
clear in class AbstractMap<K extends Enum<K>,V>
public V put​(K key, V value)
Associates the specified value with the specified key in this map. If the map previously
contained a mapping for this key, the old value is replaced.
Specified by:
put in interface Map<K extends Enum<K>,V>
Overrides:
put in class AbstractMap<K extends Enum<K>,V>
Parameters:
key – the key with which the specified value is to be associated
value – the value to be associated with the specified key
Returns:
the previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null wit6h the specified key.)
Throws:
NullPointerException – if the specified key is null
public Collection<V> values()
Returns a Collection view of the values contained in this map. The returned collection obeys the
general contract outlined in Map.values(). The collection’s iterator will return the values
in the order their corresponding keys appear in map, which is their natural order (the order in which the
enum constants are declared).
Specified by:
values in interface Map<K extends Enum<K>,V>
Overrides:
values in class AbstractMap<K extends Enum<K>,V>
Returns:
a collection view of the values contained in this map