Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
SortedMap firstKey() method in Java
Next article icon

SortedMap tailMap() method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The tailMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys are greater than or equal to fromKey.
  • The map returned by this method is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
  • The map returned by this method supports all optional map operations that this map supports.
Note: The map returned by this method will throw an IllegalArgumentException if an attempt is made to insert a key outside its range. Syntax:
  SortedMap<K, V> tailMap(K fromKey)  
Where, K is the type of key maintained by this Set and V is the type of values associated with the Key. Parameters: This function accepts a single parameter fromKey which represents high endpoint (exclusive) of the keys in the returned map. Return Value: It returns a view of the portion of this map whose keys are strictly greater than or equal to fromKey. Exception:
  • ClassCastException: If the parameter fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable).
  • NullPointerException: If the parameter fromKey is null and this map does not permit null keys.
  • IllegalArgumentException: If this map itself has a restricted range, and fromKey lies outside the bounds of the range
Below programs illustrate the above method: Program 1: Java
// A Java program to demonstrate // working of SortedSet import java.util.*;  public class Main {     public static void main(String[] args)     {         // Create a TreeSet and inserting elements         SortedMap<Integer, String> mp = new TreeMap<>();          // Adding Element to SortedSet         mp.put(1, "One");         mp.put(2, "Two");         mp.put(3, "Three");         mp.put(4, "Four");         mp.put(5, "Five");          // Returning the key greater         // than or equal to 2         System.out.print("Last Key in the map is : "                          + mp.tailMap(2));     } } 
Output:
  Last Key in the map is : {2=Two, 3=Three, 4=Four, 5=Five}  
Program 2: Java
// A Java program to demonstrate // working of SortedSet import java.util.*;  public class Main {     public static void main(String[] args)     {         // Create a TreeSet and inserting elements         SortedMap<String, String> mp = new TreeMap<>();          // Adding Element to SortedSet         mp.put("One", "Geeks");         mp.put("Two", "For");         mp.put("Three", "Geeks");         mp.put("Four", "Code");         mp.put("Five", "It");          // Returning the key greater         // than or equal to         System.out.print("Last Key in the map is : "                          + mp.tailMap("D"));     } } 
Output:
  Last Key in the map is : {Five=It, Four=Code, One=Geeks, Three=Geeks, Two=For}  
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/SortedMap.html#tailMap(K)

Next Article
SortedMap firstKey() method in Java

B

barykrg
Improve
Article Tags :
  • Misc
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • Java-SortedMap
Practice Tags :
  • Java
  • Java-Collections
  • Misc

Similar Reads

    SortedMap Interface in Java
    SortedMap is an interface in the collection framework that is a part of java.util package and extends the Map interface. It represents a map that maintains its keys in a sorted order. The keys in a SortedMap are sorted according to their natural ordering or by a Comparator provided at the time of ma
    10 min read
    SortedMap tailMap() method in Java
    The tailMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys are greater than or equal to fromKey. The map returned by this method is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The map returned b
    3 min read
    SortedMap firstKey() method in Java
    The firstKey() method of SortedMap interface in Java is used to return the first or the lowest key currently in this map. Syntax: K firstKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the first print the lo
    2 min read
    SortedMap lastKey() method in Java
    The lastKey() method of SortedMap interface in Java is used to return the last or the greatest key currently in this map. Syntax: K lastKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the last or the greates
    2 min read
    SortedMap comparator() method in Java with Examples
    The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th
    2 min read
    SortedMap values() method in Java with Examples
    The values() method of SortedMap interface in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: SortedMap.values() Parameters: The method does not accept any parameters. Return Value: The method is used to retur
    2 min read
    SortedMap keySet() method in Java with Examples
    The keySet() method of SortedMap Interface in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in an ascending order. Since the set is backed by the map, any change
    2 min read
    SortedMap entrySet() method in Java with Examples
    The entrySet() method of SortedMap interface in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or creates a new set and store the map elements into them. Syntax: SortedMap.entrySet() Parameters: The method does not take any para
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences