Tuesday, 6 November 2007

Map example

import java.util.*;

public class HashMapTest
{
public static void main(String argv[])
{
HashMap hm = new HashMap();
System.out.println(hm.put("aaa", "111"));
System.out.println(hm.put("bbb", "222"));
System.out.println(hm.put("aaa", "444"));
System.out.println(hm.put("ccc", "333"));
System.out.println(hm.put("ccc", null));

System.out.println("HashMap size : " + hm.size());

Set set = hm.keySet();
Object []hmKeys = set.toArray();
for(int i = 0; i < hmKeys.length; i++)
{
String key = (String)hmKeys[i];
System.out.print(key);
System.out.print(" - ");
System.out.println((String)hm.get(key));
}
}
}


/**
run:java HashMapTest
output:
null
null
111
null
333
HashMap size : 3
ccc - null
bbb - 222
aaa - 444

No comments: