Wednesday, 4 May 2016

HashMap in java util package

	import java.util.*; 
	class HashMapDemo
	{
		public static void main(String args[])
		{
			// Create a hash map.
			HashMap hm = new HashMap(); 
			// Put elements to the map 
			hm.put("Devesh",new Double(1000.00)); 
			hm.put("Rahul",1500.00); 
			hm.put("Rakesh",1200.00); 
			hm.put("Manohar",900.00); 
			hm.put("Prateek",1300.00); 
			// Get a set of the entries.
			Set set = hm.entrySet(); 
			// Display the set.
			Iterator i = set.iterator();
			while(i.hasNext())
			{
				Map.Entry me = (Map.Entry) i.next();
				System.out.print(me.getKey() + ": "); 
				System.out.println(me.getValue()); 
			} 
			System.out.println(); 
			// Deposit 1000 into Prateek's account.
			double balance = (Double) hm.get("Prateek");
			hm.put("Prateek", balance + 1000); 
			balance = (Double)hm.get("Prateek");
			System.out.println("Prateek's new balance: " + balance); 
		} 
	}

No comments:

Post a Comment