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 subMap() method in Java
Next article icon

SortedMap subMap() method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The subMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
  • 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> subMap(K fromKey,                        K toKey)  
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 two parameter fromKey and toKey which represents low endpoint (inclusive) and high endpoint (exclusive) respectively of the keys in the returned map. Return Value: It returns a view of the portion of this map whose keys range from fromKey to toKey. 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 ranging         // between 2 and 5         System.out.print("Elements in range from 2 to 5 in the map is : "                          + mp.subMap(2, 5));     } } 
Output:
  Elements in range from 2 to 5 in the map is : {2=Two, 3=Three, 4=Four}  
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 range between D and Z         System.out.print("Key in range from D to Z in the map is : "                          + mp.subMap("D", "Z"));     } } 
Output:
  Key in range from D to Z 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#subMap(K)

Next Article
SortedMap subMap() 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 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 headMap() method in Java
    The headMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys are strictly less than toKey. 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 m
    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 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
    TreeMap tailMap() Method in Java
    The java.util.TreeMap.tailMap(from_Key) method in Java is used to get a part or view of the map whose keys are greater than equal to the from_key in the parameter. Any changes made in one map will reflect the change in the other map. Syntax: Tree_Map.tailMap(from_Key) Parameters: The method takes on
    3 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