- public class HashMapExample {
- public static void main(String[] args) {
- Map m1 = new HashMap();
- m1.put("Chinese", new Long(100000));
- m1.put("English", new Long(20000));
- m1.put("French", new Long(3000));
- m1.put("Korean", new Long(400));
- System.out.println("The HashMap holds " + m1.size() + " elements");
- System.out.println("The keys are:");
- // 因为Map的key不可能重复,所以,可以用Set数据结构来存储
- //是这么考虑的啊,由于不能重复的原因啊,难怪不用List,它是可以重复的
- Set keySet = m1.keySet();
- Iterator ikey = keySet.iterator();
- while (ikey.hasNext()) {
- String s =(String) ikey.next();
- System.out.println("/t" + s +"==>"+m1.get(s));
- }
- System.out.println("The values are:");
- // 因为Map的值有可能重复,所以不能用Set,要用Collection
- Collection valueCol = m1.values();
- Iterator ival = valueCol.iterator();
- while (ival.hasNext()) {
- System.out.println("/t" + ival.next());
- }
- // 根据key,取出特定的值
- System.out.println("The value for Key /"Korean/" is "
- + m1.get("Korean").toString());
- }
- }