Java Basic Learning Framework 9 (Map Set with Repeatable key: Identity HashMap)

Keywords: Java encoding

1. key in Map does not allow duplication, duplication is coverage.

_The value of key in all Map operations described earlier can not be repeated. For example, the value of key in HashMap operations can not be repeated. If repeated, it will certainly override the previous content.

Example 1 code:
package self.learn.setdemo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Person{             // Define Person class to implement comparator
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}		
	
	public boolean equals(Object obj) {       // Override equals() method
		if(this == obj) {                     // Address equality
			return true;                      // It's the same object.
		}
		if(!(obj instanceof Person)) {        // It's not this class of objects that are passed in
			return false;                     // Not the same object
		}
		Person p = (Person)obj;               // Downward transition
		if(this.name.equals(p.name)&&this.age == p.age) {  // Property comparison in turn
			return true;                                   // All attributes are equal, and they are the same object.
		}else {                  
			return true;                                   // Attributes are not equal, not the same object
		}		
	}
	
	public int hashCode() {          // Override hashCode() method
		return this.name.hashCode()*this.age;  // Specified encoding formula
	}				
	@Override
	public String toString() {                               // Override the toString() method
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public static void main(String args[]) {
		Map<Person, String> map = null;
		map = new HashMap<Person, String>();
	    map.put(new Person("Zhang San", 30),"Zhangsan_1" );
	    map.put(new Person("Zhang San", 30),"Zhangsan_2" );
	    map.put(new Person("Li Si", 31),"lisi");
	    Set<Map.Entry<Person, String>> allSet = null;    // Declare a Set set
	    allSet = map.entrySet();                         // Turn Map interface instance into Set interface instance
	    Iterator<Map.Entry<Person, String>> iter = null; // Declare an Iterator instance
	    iter = allSet.iterator();                        // Instantiating Iterator objects
	    while(iter.hasNext()) {
	    	Map.Entry<Person, String> me = iter.next();   // Each object is a Map.Entry instance
	    	System.out.println(me.getKey()+"--->"+me.getValue());
	    }
	    
	}	
}
Operating results screenshot:


_From the running results of the program, we can find that the second content covers the first content, so we can use Identity HashMap at this time. If the address is not equal (key!= key2), we can add the same key to the collection.

Example 2 code: Modify the program using Identity HashMap
package self.learn.setdemo;

import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Person{             // Define Person class to implement comparator
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}		
	
	public boolean equals(Object obj) {       // Override equals() method
		if(this == obj) {                     // Address equality
			return true;                      // It's the same object.
		}
		if(!(obj instanceof Person)) {        // It's not this class of objects that are passed in
			return false;                     // Not the same object
		}
		Person p = (Person)obj;               // Downward transition
		if(this.name.equals(p.name)&&this.age == p.age) {  // Property comparison in turn
			return true;                                   // All attributes are equal, and they are the same object.
		}else {                  
			return true;                                   // Attributes are not equal, not the same object
		}		
	}
	
	public int hashCode() {          // Override hashCode() method
		return this.name.hashCode()*this.age;  // Specified encoding formula
	}				
	@Override
	public String toString() {                               // Override the toString() method
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public static void main(String args[]) {
		Map<Person, String> map = null;
		map = new IdentityHashMap<Person, String>();
	    map.put(new Person("Zhang San", 30),"Zhangsan_1" );
	    map.put(new Person("Zhang San", 30),"Zhangsan_2" );
	    map.put(new Person("Li Si", 31),"lisi");
	    Set<Map.Entry<Person, String>> allSet = null;    // Declare a Set Collection
	    allSet = map.entrySet();                         // Turn Map interface instance into Set interface instance
	    Iterator<Map.Entry<Person, String>> iter = null; // Declare an Iterator instance
	    iter = allSet.iterator();                        // Instantiating Iterator objects
	    while(iter.hasNext()) {
	    	Map.Entry<Person, String> me = iter.next();   // Each object is a Map.Entry instance
	    	System.out.println(me.getKey()+"--->"+me.getValue());
	    }	    
	}	
}
Operating results screenshot:


_From the results of the program operation, we can find that the current key allows duplication as long as the addresses of the two objects are not equal.

Posted by duncanwilkie on Wed, 02 Oct 2019 05:14:45 -0700