HashMap集合的键值对的不同类型

HashMap:是基于哈希表的Map接口实现。

哈希表的作用是用来保证键的唯一性的。
HashMap
键: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 s=new HashMap();
键: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 s=new HashMap();
键: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 s=new HashMap();
键: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

你可能感兴趣的:(Java)