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

Get size of Hashtable example in Java

By Chaitanya Singh | Filed Under: Java Collections

Example

In this example we are gonna see how to get the size of Hashtable. We will be using size() method of Hashtable class to perform this operation.

import java.util.Hashtable;

public class SizeExample{
 
 public static void main(String[] args) {
 
    // Creating a Hashtable instance
    Hashtable<String, String> hashtable = new Hashtable<String, String>();
 
    // Adding key-value pairs to Hashtable
    hashtable.put("A", "Apple");
    hashtable.put("B", "Orange");
    hashtable.put("C", "Mango");
    hashtable.put("D", "Banana");
    hashtable.put("E", "Grapes");
 
    /* int size(): method returns number of key value pairs present
     * in this hashtable. 
     */
    System.out.println("Size of Hashtable: " + hashtable.size()); 
 }
}

Output:

Size of Hashtable: 5

Enjoyed this post? Try these related posts

  1. How to clone an ArrayList to another ArrayList
  2. Java – Search elements in LinkedList example
  3. How to remove Vector elements using index in java example
  4. Java – Remove all mappings from HashMap example
  5. Check key & Value existence in Hashtable example – Java
  6. How to get sublist of an ArrayList with 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