HashMap类 和 LinkedHashMap类

HashMap类 和 LinkedHashMap类

HashMap类

特点:

  • 1.底层数据结构是 哈希表
  • 2.元素无序 , 唯一
  • 3.数据结构仅针对键有效,和值无关
  • 4.保证元素唯一依赖的hashCode和equals方法
  • 5.如果键是系统类,一般都是重写 hashCode和equals方法,如果键是自定义类,需要自己重写

底层hashcode 和 equals

public final int hashCode() {
        return Objects.hashCode(key) ^ Objects.hashCode(value);
    }
    
 public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
```java

```java
public class HashMapDemo01 {
	public static void main(String[] args) {
		HashMap<Employee, String> hm = new HashMap<>();
		hm.put(new Employee("1001", "张三1", 2600.0), "张三1");
		hm.put(new Employee("1002", "张三2", 2700.0), "张三2");
		hm.put(new Employee("1002", "张三2", 2700.0), "张三2");
		hm.put(new Employee("1003", "张三3", 2800.0), "张三3");
		hm.put(new Employee("1003", "张三3", 2800.0), "张三3");
		
		for(Employee e : hm.keySet()) {
			String value = hm.get(e);
			System.out.println(e + "=" + value);
		}
		
	}
}
public class Employee {
	private String id;
	private String name;
	private Double salary;
	public Employee() {
		super();
	}
	public Employee(String id, String name, Double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getSalary() {
		return salary;
	}
	public void setSalary(Double salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
	}
	@Override
	public int hashCode() {//如果键是自定义类,需要自己重写
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((salary == null) ? 0 : salary.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;
		Employee other = (Employee) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (salary == null) {
			if (other.salary != null)
				return false;
		} else if (!salary.equals(other.salary))
			return false;
		return true;
	}
	
	
}

LinkedHashMap类

1.底层数据结构是 链表 + 哈希表

2.链表保证元素有序,哈希表保证元素唯一

public class LinkedHashMapDemo01 {
	public static void main(String[] args) {
		LinkedHashMap<Employee, String> hm = new LinkedHashMap<>();
		hm.put(new Employee("1001", "张三1", 2600.0), "张三1");
		hm.put(new Employee("1002", "张三2", 2700.0), "张三2");
		hm.put(new Employee("1002", "张三2", 2700.0), "张三2");
		hm.put(new Employee("1003", "张三3", 2800.0), "张三3");
		hm.put(new Employee("1003", "张三3", 2800.0), "张三3");
		
		for(Employee e : hm.keySet()) {
			String value = hm.get(e);
			System.out.println(e + "=" + value);
		}
	}
}
public class Employee {
	private String id;
	private String name;
	private Double salary;
	public Employee() {
		super();
	}
	public Employee(String id, String name, Double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getSalary() {
		return salary;
	}
	public void setSalary(Double salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
	}
	@Override
	public int hashCode() {//如果键是自定义类,需要自己重写
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((salary == null) ? 0 : salary.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;
		Employee other = (Employee) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (salary == null) {
			if (other.salary != null)
				return false;
		} else if (!salary.equals(other.salary))
			return false;
		return true;
	}
	
	
}

你可能感兴趣的:(HashMap类 和 LinkedHashMap类)