Java.util.TreeMap also contains functions that support retrieval and deletion at both, high and low end of values and hence give a lot of flexibility in applicability and daily use. This function is poll() and has 2 variants discussed in this article.
1. pollFirstEntry() : It removes and retrieves a key-value pair with the least key value in the map and “null” is map is empty.
Syntax :
public Map.Entry pollFirstEntry()
Parameters:
NA
Return Value:
Retrieves and removes the least key-value if map is filled else returns null.
Exception:
NA
import java.io.*;
import java.util.*;
public class pollFirstEntry {
public static void main(String[] args)
{
TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
System.out.println
("The smallest key value pair is : " + tmp.pollFirstEntry());
tmp.put("Geeks", 1);
tmp.put("for", 4);
tmp.put("geeks", 1);
System.out.println
("The initial Map before deletion is : " + tmp);
System.out.println
("The smallest key value pair is : " + tmp.pollFirstEntry());
System.out.println
("The resultant Map after deletion is : " + tmp);
}
}
|
Output:
The smallest key value pair is : null
The initial Map before deletion is : {Geeks=1, for=4, geeks=1}
The smallest key value pair is : Geeks=1
The resultant Map after deletion is : {for=4, geeks=1}
2. pollLastEntry() : It removes and retrieves a key-value pair with the largest key value in the map and “null” is map is empty.
Syntax :
public Map.Entry pollLastEntry()
Parameters:
NA
Return Value:
Retrieves and removes the largest key-value if map is filled else returns null.
Exception:
NA
import java.io.*;
import java.util.*;
public class pollLastEntry {
public static void main(String[] args)
{
TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
System.out.println
("The largest key value pair is : " + tmp.pollFirstEntry());
tmp.put("Geeks", 1);
tmp.put("for", 4);
tmp.put("geeks", 1);
System.out.println
("The initial Map before deletion is : " + tmp);
System.out.println
("The largest key value pair is : " + tmp.pollLastEntry());
System.out.println
("The resultant Map after deletion is : " + tmp);
}
}
|
Output:
The largest key value pair is : null
The initial Map before deletion is : {Geeks=1, for=4, geeks=1}
The largest key value pair is : geeks=1
The resultant Map after deletion is : {Geeks=1, for=4}
Practical Application : There are many applications that can be thought using the concept of deque or priority queueing. One such example is shown in the code below.
import java.io.*;
import java.util.*;
public class pollAppli {
public static void main(String[] args)
{
TreeMap<Integer, String> que = new TreeMap<Integer, String>();
que.put(10, "astha");
que.put(4, "shambhavi");
que.put(7, "manjeet");
que.put(8, "nikhil");
String prio = "high";
System.out.println("The initial queue is : " + que);
if (prio == "high") {
System.out.println
("The largest valued person is : " + que.pollLastEntry());
System.out.println
("The resultant queue after deletion is : " + que);
}
else {
System.out.println
("The lowest valued person is : " + que.pollFirstEntry());
System.out.println
("The resultant queue after deletion is : " + que);
}
}
}
|
Output:
The initial queue is : {4=shambhavi, 7=manjeet, 8=nikhil, 10=astha}
The largest valued person is : 10=astha
The resultant queue after deletion is : {4=shambhavi, 7=manjeet, 8=nikhil}
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
20 Nov, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...