Here, the remove() method takes the key as its parameter. It then returns the value associated with the key and removes the entry. We can also remove the entry only under certain conditions. For example: remove(4, "C Sharp");
Here, the remove() method only removes the entry if the key 4 is associated with the value "C Sharp."
Example Two - Create Hashmap and Remove HashMap Elements
Here, we will do multiple things. We will first create a Hashmap. We will then get its values one by one. After that, we will copy all data of the HashMap to a brand-new HashMap. And finally, we will remove one item and get Hashmap sizes. If the size is lower by one, the decrease of size by removal is confirmed. Create a new Class named “ExampletwoHashMap,” and then write the code below in the class.
importjava.util.HashMap;publicclassExampletwoHashMap {publicstaticvoidmain(String[] args) {HashMap<String,String> newHashMap =newHashMap<>();// Addition of key and valuenewHashMap.put("Key1","Lenovo");newHashMap.put("Key2","Motorola");newHashMap.put("Key3","Nokia");newHashMap.put("Key4",null);newHashMap.put(null,"Sony");System.out.println("Original map contains:"+ newHashMap);//getting size of HashmapSystem.out.println("Size of original Map is:"+newHashMap.size());//copy contains of one Hashmap to anotherHashMap<String,String> copyHashMap =newHashMap<>();copyHashMap.putAll(newHashMap);System.out.println("copyHashMap mappings= "+ copyHashMap);//Removal of null keyString nullKeyValue =copyHashMap.remove(null);System.out.println("copyHashMap null key value = "+ nullKeyValue);System.out.println("copyHashMap after removing null key = "+ copyHashMap);System.out.println("Size of copyHashMap is:"+copyHashMap.size()); }}
Output:
Original map contains:{Key2=Motorola, null=Sony, Key1=Lenovo, Key4=null, Key3=Nokia}
Size of original Map is:5
copyHashMap mappings= {Key2=Motorola, null=Sony, Key1=Lenovo, Key4=null, Key3=Nokia}
copyHashMap null key value = Sony
copyHashMap after removing null key = {Key2=Motorola, Key1=Lenovo, Key4=null, Key3=Nokia}
Size of copyHashMap is:4
TreeMap Examples
Example One: Remove TreeMap Elements.
remove(key) - returns and removes the entry associated with the specified key from a TreeMap. remove(key, value) - removes the entry from the map only if the specified key is associated with the specified value, and returns a boolean value.
Create a new Class named “exampleTreemapOne,” and then write the code below in the class.
importjava.util.TreeMap;publicclassexampleTreemapOne {publicstaticvoidmain(String[] args) {TreeMap<String,Integer> numbers =newTreeMap<>();numbers.put("One",1);numbers.put("Two",2);numbers.put("Three",3);System.out.println("TreeMap: "+ numbers);// remove method with single parameterint value =numbers.remove("Two");System.out.println("Removed value: "+ value);// remove method with two parametersboolean result =numbers.remove("Three",3);System.out.println("Is the entry {Three=3} removed? "+ result);System.out.println("Updated TreeMap: "+ numbers); }}
Output:
TreeMap: {One=1, Three=3, Two=2}
Removed value = 2
Is the entry {Three=3} removed? True
Updated TreeMap: {One=1}
Example Two: Methods for Navigation.
TreeMap class also implements NavigableMap; it provides various methods to navigate over the elements of the treemap. firstKey() - returns the first key of the map. firstEntry() - returns the key/value mapping of the first key of the map. lastKey() - returns the last key of the map. lastEntry() - returns the key/value mapping of the last key of the map. Create a new Class named “exampleTreemapTwo,” and then write the code below in the class.
importjava.util.TreeMap;publicclassexampleTreemapTwo {publicstaticvoidmain(String[] args) {TreeMap<String,Integer> numbers =newTreeMap<>();numbers.put("First",1);numbers.put("Second",2);numbers.put("Third",3);System.out.println("TreeMap: "+ numbers);// Using the firstKey() methodString firstKey =numbers.firstKey();System.out.println("First Key: "+ firstKey);// Using the lastKey() methodString lastKey =numbers.lastKey();System.out.println("Last Key: "+ lastKey);// Using firstEntry() methodSystem.out.println("First Entry: "+numbers.firstEntry());// Using the lastEntry() methodSystem.out.println("Last Entry: "+numbers.lastEntry()); }}
Output
TreeMap: {First=1, Second=2, Third=3}
First Key: First
Last Key: Third
First Entry: First=1
Last Entry: Third=3
Example Three: TreeMap Comparator.
TreeMap elements are sorted in ascending order. However, we can also customize the ordering of keys. For this, we need to create our comparator class based on which keys in a TreeMap are sorted.
Create a new Class named “CustomComparator,” and then write the below code in the class.
importjava.util.Comparator;publicclassCustomComparatorimplementsComparator<String> { @Overridepublicintcompare(String number1,String number2) {int value =number1.compareTo(number2);// elements are sorted in reverse orderif (value >0) {return-1; }elseif (value <0) {return1; }else {return0; } }}
Create a new Class named “maincomparator,” and then write the code below in the class.
importjava.util.TreeMap;publicclassmaincomparator {publicstaticvoidmain(String[] args) {// Creating a treemap with a customized comparatorTreeMap<String,Integer> numbers =newTreeMap<>(newCustomComparator());numbers.put("First",1);numbers.put("Second",2);numbers.put("Third",3);numbers.put("Fourth",4);System.out.println("TreeMap: "+ numbers);}}