This simple Java Example shows how to use Java HashMap. It also describes how to add something to HashMap and how to retrieve the value added from HashMap.
import java.util.HashMap; import java.util.Set; public class JavaHashMapExample { public static void main(String[] args) { // create object of HashMap HashMap phoneBook = new HashMap(); /** * Add key value pair to HashMap using Object put(Object key, Object * value) method of Java HashMap class, where key and value both are * objects put method returns Object which is either the value * previously tied to the key or null if no value mapped to the key. **/ phoneBook.put("John", "245745"); phoneBook.put("Joy", "245786"); phoneBook.put("Roy", "233783"); // retrieve value using Object get(Object key) method of Java HashMap // class Object phoneNo = phoneBook.get("Joy"); System.out.println("Phone number of Joy: " + phoneNo); System.out.println("The Map contains..."); /** * Set keySet() - Returns a set view of the keys contained in this map. * The set is backed by the map, so changes to the map are reflected in * the set, and vice-versa **/ Set nameSet = phoneBook.keySet(); for (Object name : nameSet) { System.out.println("Name: " + name + " Phone: " + phoneBook.get(name)); } /** * put - If the map previously contained a mapping for this key, the old * value is replaced and the put method returns the old value. **/ Object oldPhone = phoneBook.put("Roy", "222222"); System.out.println("Roy's old phone number:" + oldPhone); System.out.println("Roy's new phone number:" + phoneBook.get("Roy")); } }
The output is:
Phone number of Joy: 245786
The Map contains...
Name: Roy Phone: 233783
Name: Joy Phone: 245786
Name: John Phone: 245745
Roy's old phone number:233783
Roy's new phone number:222222
No comments:
Post a Comment