Map 接口
子类 hashmap 初探 。。
1 不允许重复..
2.允许NULL key 和 null value
package com.fox_ice.test;
import java.util.HashMap; import java.util.Iterator; import java.util.Map;
public class MapTest {
/** * @param args */ public static void main(String[] args) {
Map <Object,Object>map=new HashMap<Object,Object>(); map.put(0, 1); map.put(null, 1); map.put(1, "abc"); map.put(2, false); map.put(4, 1.2222); Person1 p1=new Person1(); p1.setAge(20); p1.setName("张三"); map.put(p1.getAge(),p1.getName()); MapIterator(map); System.out.println("-------------"); map.remove(0); MapIterator(map);
System.out.println("---------------------------"); map.put(0, "1---------add"); MapIterator(map);
}
public static void MapIterator(Map<Object, Object> map) { Iterator <Object>iter=map.keySet().iterator(); Object key=null; while(iter.hasNext()) { key=iter.next();
System.out.println("键是"+key+" 键值是"+map.get(key)); }
}
} class Person1 { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private int age;
}
|