定义的实体类:
package com.tang.test;
public class Person {
private String name;
private int age;
private String phone;
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;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Person(String name, int age, String phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
public Person() {
}
}
关于Map集合:
package com.tang.test;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Object, Object> map = new HashMap<Object, Object>();
map.put(1, "啦啦啦啦啦啦");
map.put("数据1", 123);
Person pe = new Person("测试人员1", 17, "13088940010");
map.put("人员1", pe);
System.err.println(map.get(1) + "\n" +
map.get("数据1") + "\n" +
map.get("人员1"));
Person p = (Person)map.get("人员1");
System.err.println(p.getName() + "\t" +
p.getAge() + "\t" +
p.getPhone());
}
}
输出:
总结:
Map(HashMap)的key和value可为任意数据类型(自定义的对象),当生命Map集合时,如下声明:
Map<Object, Object> map = new HashMap<Object, Object>();
可满足多种数据存储的需求!