Set represents an unordered set, the set element is not repeatable; Map represents a collection of multiple key-value pairs
From the inheritance system diagram of set and map, if the name analysis, set and map are inextricably linked. And from the map interface provided in the JDK, you will find that a method is provided: Set<k> KeySet (); This means that if you throw away the value in the map, then all the keys in the map are actually a set set, And we often query the time through the key to find value, then we can bind the value and key together, the value as a key appendage, then you will be surprised to find that the map is actually a set set, so we can assume that map is a set extension, is a special set of sets. The following will attempt to expand the map with set
Import Java.util.hashset;import Java.util.iterator;import Java.util.map;public class Simpleentry <K,V> Implements Map.entry<k,v>,java.io.serializable{private final K Key;private v value;public simpleentry (k key, V Value) {This.key = Key;this.value = value;} Public Simpleentry (map.entry<? extends K,? extends V> Entry) {This.key = Entry.getkey ();} Get Keypublic K GetKey () {return this.key;} Get Valuepublic V GetValue () {return this.value;} Change the value of Key-value public V SetValue (v value) {v OldValue = This.value;this.value = Value;return oldValue;} Compares two simpleentry based on key whether public boolean equals (Object o) {if (o = = this) {return true;} if (o.getclass () = = Simpleentry.class) {simpleentry se = (simpleentry) O;return Se.getkey (). Equals (GetKey ()); return false;} Computes hashcodepublic int hashcode () {return key = = NULL according to key); 0:key.hashcode (); Public String toString () {return key + "=" + Value;}} Inherit HashSet implements a map public class settomap<k, v> extends Hashset<simpleentry<k, v>&Gt {//Implement the method of emptying all key-value pairs public void Clear () {super.clear ();} Determines whether to include a keypublic boolean containskey (K key) {return super.contains (new simpleentry<k, v> (key, NULL));} Determines whether to include a valuepublic boolean containsvalue (V value) {for (simpleentry<k, v> se:this) {if (Se.getvalue (). Equals ( value); return true;} return false;} The corresponding valuepublic V get (Object key) {for (simpleentry<k, v> se:this) {if (Se.getkey (). Equals (key)) {return} is removed according to the specified key Se.getvalue ();}} return null;} Puts the specified key-value pair into the collection public V put (K key, V value) {Add (new simpleentry<k,v> (key, value)); return value;} Place the Key-value pair of another map into the map public void Putall (map<? extends K,? extends v> m) {for (K Key:m.keyset ()) {Add (new) Entry<k, v> (Key, M.get (key)));}} Deletes the key-value to public V removeentry (Object key) {for (iterator<simpleentry<k, v>> it = this.iterator) According to the specified key ( ); It.hasnext (); {simpleentry<k, v> en = (simpleentry<k, v>) It.next (); if (En.getkey (). Equals (key)) {V v = en.getvalue (); It.remove ();return v;}} return null;} Gets how many key-value are contained in the map to the public int size () {return super.size ();}}
Java Collection Research one: set and map