BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to get the size of TreeMap example – Java

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we willl learn how to get the size of a TreeMap. We are using size() method of TreeMap class to get the number of key-value mappings of a TreeMap. Here is the complete code:

Example

import java.util.TreeMap;

class TreeMapSize {
  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", "Value1");
    treemap.put("Key2", "Value2");
    treemap.put("Key3", "Value3");
    treemap.put("Key4", "Value4");
    treemap.put("Key5", "Value5");
 
    // Displaying size of the TreeMap
    /* public int size(): Returns the number of key-value 
     * mappings in this map.
     */
    System.out.println("Size of TreeMap: "+treemap.size());
  }
}

Output:

Size of TreeMap: 5

Leave a Reply Cancel reply

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

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap