TreeMap is the implementation class of the Map interface. It allows the objects to be sorted according to the keys and sorting can be natural sorting or you can use a comparator. Insertion order is also not preserved in Tree Map.
Syntax:
TreeMap<String,Integer> map = new TreeMap<>();
In the below example we will create one Tree Map and insert records in it using the put() method. As the Tree Map sorts the records on the basis of keys, you can see the output is in the sorted order of keys from a to z. For iterating over the Tree Map we iterate over all the keys of Tree Map using the key Set() method which gives all the keys of Tree Map in a set.
Implementation:
Java
// Java Program to remove the first entry // or last entry from TreeMap import java.io.*; import java.util.TreeMap; import java.util.Set; class GFG { public static void main(String[] args) { // treemap with keys as string and // values of type integer TreeMap<String, Integer> gfg = new TreeMap<>(); // adding values to treemap gfg.put( "interviews" , 97 ); gfg.put( "fang" , 86 ); gfg.put( "competitive programming" , 95 ); gfg.put( "dsa" , 99 ); gfg.put( "java" , 99 ); gfg.put( "c++" , 99 ); // iterating over the treemap for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } } } |
c++,99 competitive programming,95 dsa,99 fang,86 interviews,97 java,99
Removing the first and last entry from TreeMap:
1. Using pollFirstEntry() and pollLastEntry() methods:
TreeMap has two method which can remove the first and last entry .pollFirstEntry() for removing the first entry and for last entry we use pollLastEntry().
Syntax:
mapObject.pollFirstEntry(); mapObject.pollLastEntry();
Return Type: The return type of both the method is entry object removed from the map.
First, we will create a TreeMap and add records in it using the put() method and then print all the records using for each loop. For printing, we will use all the keys which we can get using the keySet() method. Now we will use pollFirstEntry() and pollLastEntry() on TreeMap to remove first and last entry. After removing the first and last entry we again will iterate over the TreeMap using the for each loop.
Implementation:
Java
// Java Program remove the first entry // or last entry from the TreeMap import java.io.*; import java.util.TreeMap; import java.util.Set; class GFG { public static void main(String[] args) { // treemap with keys as string and values of type // integer TreeMap<String, Integer> gfg = new TreeMap<>(); // adding values to tree map gfg.put( "Interviews" , 97 ); gfg.put( "fang" , 86 ); gfg.put( "competitive programming" , 95 ); gfg.put( "dsa" , 99 ); gfg.put( "java" , 99 ); gfg.put( "c++" , 99 ); System.out.println( "-------before removing first and last entry-------" ); // iterating over the treemap in java for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } // removing and printing first entry System.out.println( "----printing the first removed entry----" ); System.out.println(gfg.pollFirstEntry()); // removing and printing last entry System.out.println( "----printing the last removed entry" ); System.out.println(gfg.pollLastEntry()); System.out.println( "-------after removing first and last entry-------" ); // iterating over the treemap for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } } } |
-------before removing first and last entry------- Interviews,97 c++,99 competitive programming,95 dsa,99 fang,86 java,99 ----printing the first removed entry---- Interviews=97 ----printing the last removed entry java=99 -------after removing first and last entry------- c++,99 competitive programming,95 dsa,99 fang,86
2. Using lastKey() and firstKey():
Syntax:
map.remove(map.lastKey()); map.remove(map.firstKey());
Approach:
First, we will create the TreeMap and inserts a record in it using a put() method. Now we will print all the records using for each loop and the output will be in the sorting order of keys from a to z. For removing the first entry and last entry we will use the remove(key) method. In the remove method, we need to pass the key as an argument. Now if we somehow get the first and last key we will be able to remove the first and last entry. TreeMap gives us two such methods which can give us first and the last key which is firstKey() and lastkey(), after getting the keys we will pass these keys in the remove() method and then again print the values present in treemap using for each loop.
Implementation:
Java
// Java Program to remove the first entry // or last entry from the TreeMap import java.io.*; import java.util.TreeMap; import java.util.Set; class GFG { public static void main(String[] args) { // treemap with keys as string and values of type // integer TreeMap<String, Integer> gfg = new TreeMap<>(); gfg.put( "Interviews" , 97 ); gfg.put( "fang" , 86 ); gfg.put( "competitive programming" , 95 ); gfg.put( "dsa" , 99 ); gfg.put( "java" , 99 ); gfg.put( "c++" , 99 ); // iterating over the treemap in java System.out.println( "-------before removing first and last entry-------" ); for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } // removing first entry through firstKey() gfg.remove(gfg.firstKey()); // removing last entry through lastKey() gfg.remove(gfg.lastKey()); System.out.println( "-------after removing first and last entry-------" ); // iterating over the treemap for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } } } |
-------before removing first and last entry------- Interviews,97 c++,99 competitive programming,95 dsa,99 fang,86 java,99 -------after removing first and last entry------- c++,99 competitive programming,95 dsa,99 fang,86
3. Using Streams():
Here we will get the first and last key using streams then call the remove method on it.
Syntax:
firstkey = map.entrySet().stream().findFirst().get() treemap.remove(firstkey); lastkey = map.entrySet().stream().skip(map.size()-1).findFirst().get() treemap.remove(key);
Approach:
Here we will create a TreeMap and add records in it using the put() method and then print every record using for each loop. Now we will use streams for getting the first and last key. For the first key, we will get a set of all the keys using the keySet() method and then we will use the stream() on it, now for getting the first item(a record) from the stream we will use findFirst() and get() on it to get the first key then pass this in remove method to remove first entry(a record).
For the last Key, we will again use keySet() for all the keys and after it stream(), Now we will skip the n-1 elements because if we skip the first n-1 records then ultimately we will arrive at the last record. For getting the n( number of total records or size of the TreeMap) we use the size() method.
Implementation:
Java
// Java Program remove the first entry // or last entry from the TreeMap import java.io.*; import java.util.TreeMap; import java.util.Set; class GFG { public static void main(String[] args) { // treemap with keys as string and values of type // integer TreeMap<String, Integer> gfg = new TreeMap<>(); gfg.put( "Interviews" , 97 ); gfg.put( "fang" , 86 ); gfg.put( "competitive programming" , 95 ); gfg.put( "dsa" , 99 ); gfg.put( "java" , 99 ); gfg.put( "c++" , 99 ); // iterating over the treemap in java System.out.println( "-------before removing first and last entry-------" ); for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } // removing first entry through firstKey() String firstKey = gfg.keySet().stream().findFirst().get(); gfg.remove(firstKey); // removing last entry through lastKey() String lastKey = gfg.keySet() .stream() .skip(gfg.size() - 1 ) .findFirst() .get(); gfg.remove(lastKey); System.out.println( "-------after removing first and last entry-------" ); // iterating over the treemap for (String key : gfg.keySet()) { System.out.println(key + "," + gfg.get(key)); } } } |
-------before removing first and last entry------- Interviews,97 c++,99 competitive programming,95 dsa,99 fang,86 java,99 -------after removing first and last entry------- c++,99 competitive programming,95 dsa,99 fang,86