- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to get Tail Map from TreeMap
To get Tail Map from TreeMap, let us first create a TreeMap and set key-value pair −
TreeMap<String, String>tMap = new TreeMap<String, String>(); tMap.put("1", "A"); tMap.put("2", "B"); tMap.put("3", "C"); tMap.put("4", "D"); tMap.put("5", "E"); tMap.put("6", "F"); tMap.put("7", "G");
Now, let’s say you need to get the tailmap from 2 i.e. all the key-value pairs after 2. For that, use the method tailMap() −
SortedMapmap = tMap.tailMap("2");
Example
import java.util.SortedMap; import java.util.TreeMap; public class Demo { public static void main(String[] args) { TreeMap<String, String>tMap = new TreeMap(); tMap.put("1", "A"); tMap.put("2", "B"); tMap.put("3", "C"); tMap.put("4", "D"); tMap.put("5", "E"); tMap.put("6", "F"); tMap.put("7", "G"); tMap.put("8", "H"); tMap.put("9", "I"); tMap.put("10", "J"); SortedMap map = tMap.tailMap("2"); System.out.println("Sub Map = " + map); map = tMap.tailMap("8"); System.out.println("Sub Map = " + map); map = tMap.tailMap("1"); System.out.println("Sub Map = " + map); } }
Output
Sub Map = {2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I} Sub Map = {8=H, 9=I} Sub Map = {1=A, 10=J, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
- Related Articles
- Get Tail Map from TreeMap in Java
- Java Program to get Sub Map from TreeMap
- Get Sub Map from TreeMap in Java
- Get Head Map from TreeMap in Java
- Java Program to get Tail Set from TreeSet
- Get Tail Set from TreeSet in Java
- Java Program to get highest key stored in TreeMap
- Java Program to convert HashMap to TreeMap
- Program to convert HashMap to TreeMap in Java
- Get lowest key stored in Java TreeMap
- Create NavigableMap from TreeMap in Java
- Remove all values from TreeMap in Java
- Remove a value from TreeMap in Java
- Remove a key from TreeMap in Java
- Java Program to convert a Map to a read only map

Advertisements