HashMap

结论: 使用HashMap集合存储元素,要保证元素的唯一性,需要依赖于元素的两个方法一个是hashCode方法一个是equals方法 ;

只需要让元素重写hashCode方法和equals方法即可 ; 我们可以使用eclipse中的快捷键生成出来 , shift + alt + s ; h + enter ;

import java.util.HashMap;

import com.loaderman.bean.Student;

public class Demo_HashMap {
	/*
	 * * A:案例演示
	 * HashMap集合键是Student值是String的案例
	 * 键是学生对象,代表每一个学生
	 * 值是字符串对象,代表学生归属地
	 */
	public static void main(String[] args) {
		HashMap<Student, String> hm = new HashMap<>();
		hm.put(new Student("张三", 23), "北京");
		hm.put(new Student("张三", 23), "上海");
		hm.put(new Student("李四", 24), "广州");
		hm.put(new Student("王五", 25), "深圳");
		
		System.out.println(hm);
	}
}
public class Student implements Comparable<Student> {
	private String name;
	private int age;
	public Student() {
		super();
		
	}
	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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		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 (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public int compareTo(Student o) {
		int num = this.age - o.age;					//以年龄为主要条件
		return num == 0 ? this.name.compareTo(o.name) : num;
	}
}

 集合嵌套之HashMap嵌套HashMap

package com.loaderman.map;

import java.util.HashMap;

import com.loaderman.bean.Student;

public class Demo8_HashMapHashMap {

	/**
	 * * A:案例演示
	 * 集合嵌套之HashMap嵌套HashMap
	 * 
	 * 需求:
	 * 学校有很多班
	 * 第1班定义成一个双列集合,键是学生对象,值是学生的归属地
	 * 第2础班定义成一个双列集合,键是学生对象,值是学生的归属地
	 * 
	 * 无论1班还是2班都是班级对象,所以为了便于统一管理,把这些班级对象添加到集合中
	 */
	public static void main(String[] args) {
		//定义1班
		HashMap<Student, String> hm88 = new HashMap<>();
		hm88.put(new Student("张三", 23), "北京");
		hm88.put(new Student("李四", 24), "北京");
		hm88.put(new Student("王五", 25), "上海");
		hm88.put(new Student("赵六", 26), "广州");
		
		//定义2班
		HashMap<Student, String> hm99 = new HashMap<>();
		hm99.put(new Student("唐僧", 1023), "北京");
		hm99.put(new Student("孙悟空",1024), "北京");
		hm99.put(new Student("猪八戒",1025), "上海");
		hm99.put(new Student("沙和尚",1026), "广州");
		
		//定义班级
		HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
		hm.put(hm88, "1班");
		hm.put(hm99, "2班");
		
		//遍历双列集合
		for(HashMap<Student, String> h : hm.keySet()) {		//hm.keySet()代表的是双列集合中键的集合
			String value = hm.get(h);						//get(h)根据键对象获取值对象
			//遍历键的双列集合对象
			for(Student key : h.keySet()) {					//h.keySet()获取集合总所有的学生键对象
				String value2 = h.get(key);			
				System.out.println(key + "=" + value2 + "=" + value);
			}
		}
	}
}
原文地址:https://www.cnblogs.com/loaderman/p/6407408.html