/*
Creating, Inserting, Iterating and Modifying HashMap
*/
import java.util.*;
class HashMapCreat
{
public static void main(String[]args)
{
HashMap<Integer,String> words = new HashMap<Integer,String>();
// constructing HashMap
words.put(1,"one");
words.put(2,"Two");
System.out.println(words); // printing all
Set e = words.entrySet();
// Iterating through HashMap.
for(Iterator i = e.iterator();i.hasNext();)
{
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey()+"=");
System.out.println(me.getValue());
}
// Duplicates
Object o = words.put(2,"Two");
if(o !=null)
System.out.println("Noo....");
else
System.out.println("Added");
System.out.println(words); // printing all
// Modifications
words.put(1,"ONE");
System.out.println(words);
}
}
0 Comments