BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get the Sub Map from TreeMap example – Java

By Chaitanya Singh | Filed Under: java

In this example we are gonna see how to get a sub map from TreeMap. We are using subMap() method of TreeMap class. Please refer the comments in the below program for more details.

Example

import java.util.*;

class TreeMapDemo {
  public static void main(String args[]) {
 
    // Create a TreeMap
    TreeMap<String, String> treemap = 
            new TreeMap<String, String>();

    // Put elements to the map
    treemap.put("Key1", "Jack");
    treemap.put("Key2", "Rick");
    treemap.put("Key3", "Kate");
    treemap.put("Key4", "Tom");
    treemap.put("Key5", "Steve");
    treemap.put("Key6", "Ram");
 
    // Displaying TreeMap elements
    System.out.println("TreeMap Contains : " + treemap);
 
    // Getting the sub map
    /* public SortedMap<K,V> subMap(K fromKey,K toKey): Returns 
     * a view of the portion of this map whose keys range from 
     * fromKey, inclusive, to toKey, exclusive. 
     * (If fromKey and toKey are equal, the returned map is empty.) 
     * The returned map is backed by this map, so changes in the 
     * returned map are reflected in this map, and vice-versa. 
     * The returned map supports all optional map operations that 
     * this map supports.
     */
    SortedMap<String, String> sortedMap = treemap.subMap("Key2","Key5");
    System.out.println("SortedMap Contains : " + sortedMap);
 
    // Removing an element from Sub Map
    sortedMap.remove("Key4");
 
    /* Displaying elements of original TreeMap after 
     * removing an element from the Sub Map. Since Sub Map is 
     * backed up by original Map, the element should be removed
     * from this TreeMap too.
     */
    System.out.println("TreeMap Contains : " + treemap);
  }
}

Output:

TreeMap Contains : {Key1=Jack, Key2=Rick, Key3=Kate, Key4=Tom, Key5=Steve, Key6=Ram}
SortedMap Contains : {Key2=Rick, Key3=Kate, Key4=Tom}
TreeMap Contains : {Key1=Jack, Key2=Rick, Key3=Kate, Key5=Steve, Key6=Ram}

References:
subMap() method – TreeMap javadoc
SortedMap javadoc

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap