HashMap集合的键值对的不同类型
程序员文章站
2024-02-10 13:21:40
...
HashMap:是基于哈希表的Map接口实现。
哈希表的作用是用来保证键的唯一性的。
HashMap<String,String>
键:String
值:String
//创建集合
HashMap<String,String> map=new HashMap<String,String>();
//添加元素
map.put("邓超", "孙俪");
map.put("黄晓明", "杨颖");
map.put("周杰伦","昆凌");
//获取所有的键集合
Set<String> key=map.keySet();
//遍历
for(String s:key){
String value=map.get(s);
System.out.println(s+"----"+value);
}
HashMap<Integer,String> s=new HashMap<Integer,String>();
键:Integer
值:String
//创建集合
HashMap<Integer,String> map=new HashMap<Integer,String>();
//添加元素
map.put(23, "孙俪");
map.put(34, "杨颖");
map.put(45,"昆凌");
//获取所有的键集合
Set<Integer> key=map.keySet();
//遍历
for(Integer s:key){
String value=map.get(s);
System.out.println(s+"----"+value);
}
HashMap<String,Student> s=new HashMap<String,Student>();
键:String
值:Student
//创建集合
HashMap<String,Student> m=new HashMap<String,Student>();
Student s1=new Student("ss",12);
Student s2=new Student("aa",23);
Student s3=new Student("bb",23);
m.put("1",s1);
m.put("2",s2);
m.put("3",s3);
//
Set<String> set=m.keySet();
for(String s:set){
Student ss=m.get(s);
System.out.println(s+"---"+ss.getName()+"---"+ss.getPassword());
}
HashMap<Student,String> s=new HashMap<Student,String>();
键:Student
值:String
Student.java:
package cn.itcast_07;
public class Student {
private String name;
private int password;
public Student(String name, int password) {
super();
this.name = name;
this.password = password;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
}
test.java;
package cn.itcast_07;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class test {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> m=new HashMap<Student,String>();
Student s1=new Student("ss",12);
Student s2=new Student("aa",23);
Student s3=new Student("bb",23);
Student s4=new Student("ss",12);
m.put(s1,"1");
m.put(s2,"2");
m.put(s3,"3");
m.put(s4, "4");
//
Collection<Student> set=m.keySet();
for(Student s:set){
String ss=m.get(s);
System.out.println(ss+"---"+s.getName()+"---"+s.getPassword());
}
}
}
结果:
3---bb---23
1---ss---12
2---aa---23
4---ss---12
按理来说:
s1和s4应该是键相同了,理论上应该会覆盖掉的,原来是Student.java中需要重写方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + password;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password != other.password)
return false;
return true;
}
再次运行test.java:
结果正确,因此在HashMap中,需要重写这个两个方法,才是对的
2---aa---23
3---bb---23
4---ss---12
上一篇: PHP开发框架流行度排名:Laravel居首_PHP
下一篇: Oracle重建企业管理器