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.
- */

10x for U !
!!!!!!!!!!!!!!!!!!!!!!!!!
nicw code buddy....really
Indeed simple and effective
Thanks for this class.Its
is this true??
ya....its very good code to
completely agree nice and
Thnx it really helpfull
i love the way presentation
thank u:)
Thanks buddy!
well commented source code i
well commented source code i
Thanks
Thanks its very handy.. :)
hey hot to creat a array of
Thanks for the example -
Simple and easy way to
Thank you. Good work.
Really great tutorial...Love
Thanks for the good example!
Well constructed .. looks
Great tutorial Understood it
:)
nice one...:)
excellent tutorial for
Good Presentation
Superb code.... please keep
Should have used Generics as
Excellent Code to learn
thanks u , sweet and simple
tnks man. really really good
Excellent Code to learn
thankx it is really helpful
chimpenav ..........good code
ya gr8 code to learn abt
Thankyou
Well written, Thanks for that
thank you , it is very
good raess
thank yu=ou
Nice post
super cool example
Goods.Thanks verymuch
thank you very useful
Nice tutorial, Clear consice
really tacky one should
How does one return a hashmap
its very nice and
Post new comment