GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java EnumMap remove() Method
Selected Reading
Java EnumMap remove() MethodDescriptionThe Java EnumMap remove(Object key) method removes the mapping for this key from this map if present. DeclarationFollowing is the declaration for java.util.EnumMap.remove() method public V remove(Object key) Parameterskey − the key whose mapping is to be removed from the map Return ValueThis method returns the previous value associated with specified key, or null if there was no entry for key. ExceptionNA Removing A Value from an EnumMap of Enum, Integer Pairs ExampleThe following example shows the usage of Java EnumMap remove(Object) method to remove a value from the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added and enumMap is printed. Using remove() method, a value of enumMap is remove and enumMap is printed. package com.tutorialspoint;
import java.util.EnumMap;
public class EnumMapDemo {
// create an enum
public enum Numbers{ONE, TWO, THREE, FOUR, FIVE};
public static void main(String[] args) {
EnumMap<Numbers,Integer> map =
new EnumMap<>(Numbers.class);
// associate values in map
map.put(Numbers.ONE, 1);
map.put(Numbers.TWO, 2);
map.put(Numbers.THREE,3);
map.put(Numbers.FOUR, 4);
// print the whole map
System.out.println(map);
// remove a value based on a key
System.out.println(map.remove(Numbers.TWO));
// print the map again
System.out.println(map);
}
}
OutputLet us compile and run the above program, this will produce the following result − {ONE=1, TWO=2, THREE=3, FOUR=4}
2
{ONE=1, THREE=3, FOUR=4}
Removing A Value from an EnumMap of Enum, String Pairs ExampleThe following example shows the usage of Java EnumMap remove(Object) method to remove a value from the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Strings. Few entries are added and enumMap is printed. Using remove() method, a value of enumMap is remove and enumMap is printed. package com.tutorialspoint;
import java.util.EnumMap;
public class EnumMapDemo {
// create an enum
public enum Numbers{ONE, TWO, THREE, FOUR, FIVE};
public static void main(String[] args) {
EnumMap<Numbers,String> map =
new EnumMap<>(Numbers.class);
// associate values in map
map.put(Numbers.ONE, "1");
map.put(Numbers.TWO, "2");
map.put(Numbers.THREE,"3");
map.put(Numbers.FOUR, "4");
// print the whole map
System.out.println(map);
// remove a value based on a key
System.out.println(map.remove(Numbers.TWO));
// print the map again
System.out.println(map);
}
}
OutputLet us compile and run the above program, this will produce the following result − {ONE=1, TWO=2, THREE=3, FOUR=4}
2
{ONE=1, THREE=3, FOUR=4}
Removing A Value from an EnumMap of Enum, Object Pairs ExampleThe following example shows the usage of Java EnumMap remove(Object) method to remove a value from the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Student objects. Few entries are added and enumMap is printed. Using remove() method, a value of enumMap is remove and enumMap is printed. package com.tutorialspoint;
import java.util.EnumMap;
public class EnumMapDemo {
// create an enum
public enum Numbers{ONE, TWO, THREE, FOUR, FIVE};
public static void main(String[] args) {
EnumMap<Numbers,Student> map =
new EnumMap<>(Numbers.class);
// associate values in map
map.put(Numbers.ONE, new Student(1, "Julie"));
map.put(Numbers.TWO, new Student(2, "Robert"));
map.put(Numbers.THREE,new Student(3, "Adam"));
map.put(Numbers.FOUR, new Student(4, "Jene"));
// print the whole map
System.out.println(map);
// remove a value based on a key
System.out.println(map.remove(Numbers.TWO));
// print the map again
System.out.println(map);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
OutputLet us compile and run the above program, this will produce the following result − {ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ], FOUR=[ 4, Jene ]}
[ 2, Robert ]
{ONE=[ 1, Julie ], THREE=[ 3, Adam ], FOUR=[ 4, Jene ]}
java_util_enummap.htm
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |