beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • 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

Enjoyed this post? Try these related posts

  1. Hashtable Iterator example – Java
  2. How to initialize an ArrayList
  3. Vector Iterator example in Java
  4. How to get sublist of an ArrayList with example
  5. How to compare two ArrayList in Java
  6. Java ArrayList addAll(Collection c) Method example

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 – 2021 BeginnersBook . Privacy Policy . Sitemap