Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Java HashMap example.
- This Java HashMap example describes the basic operations performed on the HashMap.
- */
- import java.util.HashMap;
- import java.util.Iterator;
- public class HashMapExample{
- public static void main(String args[]){
- // constructs a new empty HashMap with default initial capacity
- HashMap hashMap = new HashMap();
- /*
- To specify initial capacity, use following constructor
- HashMap HashMap = new HashMap(100);
- To create HashMap from map use following constructor
- HashMap HashMap = new HashMap(Map myMap);
- */
- hashMap.put("One", new Integer(1)); // adding value into HashMap
- hashMap.put("Two", new Integer(2));
- hashMap.put("Three", new Integer(3));
- /*
- IMPORTANT : We CAN NOT add primitives to the HashMap. We have to wrap it into
- one of the wrapper classes before adding.
- */
- /*
- To copy all key - value pairs from any Map to HashMap use putAll method.
- Signature of putAll method is,
- void putAll(Map m)
- */
- //get number of keys present in the HashMap
- System.out.println("HashMap contains " + hashMap.size() + " key value pairs.");
- /*
- To check whether HashMap is empty or not, use isEmpty() method.
- isEmpty() returns true is HashMap is empty, otherwise false.
- /*
- Finding particular value from the HashMap:
- HashMap's containsValue method returns boolean depending upon
- the presence of the value in given HashMap
- Signature of the containsValue method is,
- boolean containsValue(Object value)
- */
- if(hashMap.containsValue(new Integer(1))){
- System.out.println("HashMap contains 1 as value");
- }else{
- System.out.println("HashMap does not contain 1 as value");
- }
- /*
- Finding particular Key from the HashMap:
- HashMap's containsKey method returns boolean depending upon the
- Presence of the key in given HashMap
- Signature of the method is,
- boolean containsKey(Object key)
- */
- if( hashMap.containsKey("One") ){
- System.out.println("HashMap contains One as key");
- }else{
- System.out.println("HashMap does not contain One as value");
- }
- /*
- Use get method of HashMap to get value mapped to particular key.
- Signature of the get method is,
- Object get(Object key)
- */
- Integer one = (Integer) hashMap.get("One");
- System.out.println("Value mapped with key \"One\" is " + one);
- /*
- IMPORTANT: get method returns Object, so we need to downcast it.
- */
- /*
- To get all keys stored in HashMap use keySet method
- Signature of the keysSet method is,
- Set keySet()
- */
- System.out.println("Retrieving all keys from the HashMap");
- Iterator iterator = hashMap.keySet().iterator();
- while(iterator. hasNext()){
- System.out.println(iterator.next());
- }
- /*
- To get all values stored in HashMap use entrySet() method.
- Signature of the entrySet() method is,
- Set entrySet()
- */
- System.out.println("Retrieving all values from the HashMap");
- iterator = hashMap.entrySet().iterator();
- while(iterator. hasNext()){
- System.out.println(iterator.next());
- }
- /*
- To remove particular key - value pair from the HashMap use remove method.
- Signature of remove method is,
- Object remove(Object key)
- This method returns value that was mapped to the given key,
- otherwise null if mapping not found.
- */
- System.out.println( hashMap.remove("One") + " is removed from the HashMap.");
- }
- }
- /*
- OUTPUT of the above given Java HashMap Example would be :
- HashMap contains 3 key value pair.
- HashMap contains 1 as value
- HashMap contains One as key
- Value mapped with key "One" is 1
- Retrieving all keys from the HashMap
- Three
- Two
- One
- Retrieving all values from the HashMap
- Three=3
- Two=2
- One=1
- 1 is removed from the HashMap.
- */

Post new comment