HashMap儲存自定義物件
阿新 • • 發佈:2018-12-24
將學生物件和學生的歸屬地通過鍵與值儲存到map集合中。
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import
java.util.Set;
-
-
-
class Student {
-
String name;
-
int age;
-
-
public Student(String name,int age){
-
super
();
-
this.name=name;
-
this.age=age;
-
}
-
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
class HashMapDemo{
-
public static void main(String[] args){
-
HashMap<Student,String> hm=
new HashMap<Student,String>();
-
-
hm.put(
new Student(
“lisi”,
38),
“北京”);
-
hm.put(
new Student(
“zhaosi”,
34),
“上海”);
-
hm.put(
new Student(
“xiaoqiang”,
31),
“瀋陽”);
-
hm.put(
new Student(
“wangcai”,
28),
“大連”);
-
hm.put(
new Student(
“zhaosi”,
34),
“鐵嶺”);
-
-
Set<Student> keySet=hm.keySet();
-
-
Iterator<Student> it=keySet.iterator();
-
-
while(it.hasNext()){
-
Student key=it.next();
-
String value=hm.get(key);
-
System.out.println(key.getName()+
“:”+key.getAge()+
“…”+value);
-
}
-
}
-
-
}
執行結果:
當鍵一樣時,其值並沒有被覆蓋,所以要判斷鍵是否相同,HashMap需要定義hashCode和equals方法,在Student類中要複寫方法:
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.Set;
-
-
-
class Student {
-
String name;
-
int age;
-
-
public Student(String name,int age){
-
super();
-
this.name=name;
-
this.age=age;
-
}
-
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 int hashCode(){
//複寫hashCode()
-
return name.hashCode()+age;
-
}
-
-
public boolean equals(Object obj){
//複寫equals
-
-
if(
this==obj)
return
true;
-
-
if(!(obj
instanceof Student))
throw
new ClassCastException(
"型別錯誤");
//輸入型別錯誤
-
-
Student p = (Student)obj;
//強制轉換
-
return
this.name.equals(p.name) &&
this.age==p.age;
//說明姓名和年齡相同則為同一元素
-
}
-
-
}
-
public
class HashMapDemo{
-
public static void main(String[] args){
-
HashMap<Student,String> hm=
new HashMap<Student,String>();
-
-
hm.put(
new Student(
"lisi",
38),
"北京");
-
hm.put(
new Student(
"zhaosi",
34),
"上海");
-
hm.put(
new Student(
"xiaoqiang",
31),
"瀋陽");
-
hm.put(
new Student(
"wangcai",
28),
"大連");
-
hm.put(
new Student(
"zhaosi",
34),
"鐵嶺");
-
-
Set<Student> keySet=hm.keySet();
-
Iterator<Student> it=keySet.iterator();
-
-
//以下被註釋的程式碼可以替換上面兩句:
-
//Iterator<Student> it=hm.keySet().iterator();
-
-
while(it.hasNext()){
-
Student key=it.next();
-
String value=hm.get(key);
-
System.out.println(key.getName()+
":"+key.getAge()+
"..."+value);
-
}
-
}
-
-
}
執行結果:
將學生物件和學生的歸屬地通過鍵與值儲存到map集合中。
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.Set;
-
-
-
class Student {
-
String name;
-
int age;
-
-
public Student(String name,int age){
-
super();
-
this.name=name;
-
this.age=age;
-
}
-
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
class HashMapDemo{
-
public static void main(String[] args){
-
HashMap<Student,String> hm=
new HashMap<Student,String>();
-
-
hm.put(
new Student(
“lisi”,
38),
“北京”);
-
hm.put(
new Student(
“zhaosi”,
34),
“上海”);
-
hm.put(
new Student(
“xiaoqiang”,
31),
“瀋陽”);
-
hm.put(
new Student(
“wangcai”,
28),
“大連”);
-
hm.put(
new Student(
“zhaosi”,
34),
“鐵嶺”);
-
-
Set<Student> keySet=hm.keySet();
-
-
Iterator<Student> it=keySet.iterator();
-
-
while(it.hasNext()){
-
Student key=it.next();
-
String value=hm.get(key);
-
System.out.println(key.getName()+
“:”+key.getAge()+
“…”+value);
-
}
-
}
-
-
}
執行結果:
當鍵一樣時,其值並沒有被覆蓋,所以要判斷鍵是否相同,HashMap需要定義hashCode和equals方法,在Student類中要複寫方法:
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.Set;
-
-
-
class Student {
-
String name;
-
int age;
-
-
public Student(String name,int age){
-
super();
-
this.name=name;
-
this.age=age;
-
}
-
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 int hashCode(){
//複寫hashCode()
-
return name.hashCode()+age;
-
}
-
-
public boolean equals(Object obj){
//複寫equals
-
-
if(
this==obj)
return
true;
-
-
if(!(obj
instanceof Student))
throw
new ClassCastException(
"型別錯誤");
//輸入型別錯誤
-
-
Student p = (Student)obj;
//強制轉換
-
return
this.name.equals(p.name) &&
this.age==p.age;
//說明姓名和年齡相同則為同一元素
-
}
-
-
}
-
public
class HashMapDemo{
-
public static void main(String[] args){
-
HashMap<Student,String> hm=
new HashMap<Student,String>();
-
-
hm.put(
new Student(
"lisi",
38),
"北京");
-
hm.put(
new Student(
"zhaosi",
34),
"上海");
-
hm.put(
new Student(
"xiaoqiang",
31),
"瀋陽");
-
hm.put(
new Student(
"wangcai",
28),
"大連");
-
hm.put(
new Student(
"zhaosi",
34),
"鐵嶺");
-
-
Set<Student> keySet=hm.keySet();
-
Iterator<Student> it=keySet.iterator();
-
-
//以下被註釋的程式碼可以替換上面兩句:
-
//Iterator<Student> it=hm.keySet().iterator();
-
-