In this article, we will discuss how to find size or length of a HashMap or implementation classes of Map interface like LinkedHashMap or TreeMap
1. Map implementation classes:
- HashMap –> stores entries or Key-Value pairs, in random-order
- LinkedHashMap –> stores entries or Key-Value pairs, as per insertion-order
- TreeMap –> stores entries or Key-Value pairs, as per some sorting-order
2. To get size of HashMap :
- use size() method of Map interface
- which returns number of Entries or Key-Value pairs in the invoking Map object
Syntax:
int hmSize = hm.size();
FindSizeOfAHashMap.java
package in.bench.resources.java.map;
import java.util.HashMap;
public class FindSizeOfAHashMap {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
HashMap<String, String> hm = new HashMap<String, String>();
// adding key-value pairs to HashMap object
hm.put("Google", "Sundar Pichai");
hm.put("Facebook", "Mark Zuckerberg");
hm.put("LinkedIn", "Reid Hoffman");
hm.put("Apple", "Steve Jobs");
hm.put("Microsoft", "Bill Gates");
// get size of HashMap
int hmSize = hm.size();
// printing size to console
System.out.println("Size of an HashMap is : " + hmSize);
// printing all Key-Value pairs
System.out.println("\n\nall Key-Value pairs:\n\n" + hm);
}
}
Output:
Size of an HashMap is : 5
all Key-Value pairs:
{LinkedIn=Reid Hoffman, Microsoft=Bill Gates, Apple=Steve Jobs,
Google=Sundar Pichai, Facebook=Mark Zuckerberg}
Related Articles:
- Java – How to get all keys of a HashMap ?
- Java – How to get all values of a HashMap ?
- Java – How to get all Entries or Key-Value pairs of HashMap ?
- Java – How to get size or length of HashMap ?
- Java – How to check whether a particular key is present in HashMap ?
- Java – How to check whether a particular value is present in HashMap ?
- Java – How to check whether HashMap is empty or not ?.
- Java – Adding one HashMap to another HashMap using putAll method
- Java – How to delete an entry of HashMap ?
- Java – How to delete all entries of HashMap ?
- Java 8 – How to remove an entry from HashMap by comparing values
- Java 8 – How to remove an entry from HashMap by comparing keys
- Java – Various ways to iterate through HashMap
- Java – To reverse order the LinkedHashMap contents
- Java – How ConcurrentModificationException can be handled ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!