JAVA hashmap的用法

来源:百度知道 编辑:UC知道 时间:2024/07/05 02:24:13
1.加入元素
2.遍历这个map
3.移除元素

已经给楼主写了个例子..

import java.util.HashMap;
import java.util.HashSet;

import java.util.Iterator;

public class HashMapTest {

public static void main(String[] args){
HashMap<String,Object> hm=new HashMap<String,Object>();

People p1=new People();
People p2=new People();
People p3=new People();
People p4=new People();

hm.put("People3", p1);
hm.put("People1", p2);
hm.put("People4", p3);
hm.put("People2", p4);

Iterator<String> it=hm.keySet().iterator();

while(it.hasNext()){
System.out.println(it.next());
}
}
}
class People {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;