HashMap類とLinked HashMap類

42295 ワード

HashMap類とLinked HashMap類
HashMap類
特徴:
  • .底のデータ構造は、ハッシュテーブル
  • である.
  • .要素無秩序、唯一の
  • .データ構造はキーに対してのみ有効であり、値に関係なく
  • .要素に固有の依存性を保証するhashCodeとequals方法
  • .キーがシステムクラスの場合、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;
    	}
    	
    	
    }
    
    Linked HashMap類
    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;
    	}
    	
    	
    }