3:22 am - Friday April 19, 2019

Java Program to Get Size of LinkedHashMap – Hashmap in Java

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

LinkedHashMap is a combination of hash table and linked list. It has a predictable iteration order. Here simple java programming examples of getting hash map size in java.

 


import java.util.LinkedHashMap;

class GetSizeOfLinkedHashMap {

public static void main(String[] args) {

// creating LinkedHashMap Object

LinkedHashMap lHashMap = new LinkedHashMap();

System.out.println("Size of LinkedHashMap : " + lHashMap.size());

lHashMap.put("1", "One");

lHashMap.put("2", "Two");

lHashMap.put("3", "Three");

// size() method is used to find the elements are in likedHashMap

System.out.println("Size of LinkedHashMap after addition : "
+ lHashMap.size());

Object obj = lHashMap.remove("2");

System.out.println("Size of LinkedHashMap after removal : "
+ lHashMap.size());

}

}

output:

D:\CandidJava\Util\LinkedHashMap>javac GetSizeOfLinkedHashMap.java

Note: GetSizeOfLinkedHashMap.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint: unchecked for details.

D:\CandidJava\Util\LinkedHashMap>java GetSizeOfLinkedHashMap

Size of LinkedHashMap : 0

Size of LinkedHashMap after addition : 3

Size of LinkedHashMap after removal : 2

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply