3:53 pm - Monday March 25, 8813

java program to Get Size of HashMap

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

import java.util.HashMap;

class GetSizeOfHashMap {
public static void main(String[] args) {
// creating HashMap Object
HashMap hMap = new HashMap();
System.out.println("Size of HashMap : " + hMap.size());
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Three");
// Using size() method the hashMap size is calculated
System.out.println("Size of HashMap after addition : " + hMap.size());
// Using remove() method the particular element has been removed from
// the Map
Object obj = hMap.remove("2");
System.out.println("Size of HashMap after removal : " + hMap.size());
}
}

Output:

D:\CandidJava\Util\HashMap>javac GetSizeOfHashMap.java

Note: GetSizeOfHashMap.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashMap>java GetSizeOfHashMap

Size of HashMap : 0

Size of HashMap after addition : 3

Size of HashMap after removal : 2

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply