Java Learning lesson 39th (Common Object API)-set frame (vii)-MAP collection and its subclass objects

Source: Internet
Author: User

First, the Map collection common subclass


HashTable: The internal structure is a hash table, synchronous, this implementation provides all the optional mapping operations, does not allow null values and NULL keys

(sub-class under Hashtable Properties, with very high frequency, used to store key-value pairs of profile information and IO technology combination)

HASHMAP: The internal structure is a hash table, not synchronized, this implementation provides all the optional mapping operations, and allows null values and NULL keys to be used.

TREEMAP: The internal structure is a two-fork tree, which is not synchronized and can be sorted by the keys in the map collection.


Second, HashMap demo

Import Java.util.collection;import java.util.hashmap;import Java.util.iterator;import Java.util.Map;import Java.util.set;class man{private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Mans (String name, int age) {super (); this.name = Name;this.age = age;} Public Man () {super ();//TODO auto-generated constructor stub} @Overridepublic int Hashcode () {final int prime = 31;int Res Ult = 1;result = Prime * result + Age;result = Prime * result + ((name = = null)? 0:name.hashcode ()); return result; @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Man other = (mans) 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;}} public class Main {public STAtic void Main (string[] args) {Hashmapdemo ();} public static void Hashmapdemo () {//person and place of origin stored in HashMap by key-value pair Hashmap<man, string> HM = new hashmap<man,string> () Hm.put (New Man ("Week", 1), "Shanghai"), Hm.put (New Man ("Week", 1), "Northeast");//To guarantee a hash table, so the hashcode and equals method in Man must be Hm.put ("King", 4 ), "Beijing"), Hm.put (New Man ("Sun", 2), "Guangzhou"), Hm.put ("Zhao", 6), "Shandong");iterator<man> it = Hm.keyset (). Iterator (); while (It.hasnext ()) {Mans man = It.next (); String bir = Hm.get (man); System.out.println ("Man-bir:" +man.getname () + "," +man.getage () + "-" +bir);}}}

Three, TreeMap demo

Import Java.util.collection;import java.util.comparator;import Java.util.iterator;import Java.util.Map;import Java.util.set;import Java.util.treemap;class Man extends object{private string name;private int age;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Mans (String name, int age) {super (); this.name = Name;this.age = age;} Public Man () {super ();//TODO auto-generated constructor stub} @Overridepublic int Hashcode () {final int prime = 31;int Res Ult = 1;result = Prime * result + Age;result = Prime * result + ((name = = null)? 0:name.hashcode ()); return result; @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Man other = (mans) 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;}} Class Compararule implements comparator<object>{public int Compare (object arg0, Object arg1) {if (! (                 arg0 instanceof Man) throw new ClassCastException ();              Man mans = (man) arg0;        Mans man2 = (man) arg1;         int te = Man.getage ()-man2.getage ();      Return Te==0?man.getname (). CompareTo (Man2.getname ()): TE; }}public class Main {public static void Main (string[] args) {Treemapdemo ();} public static void Treemapdemo () {treemap<man, string> tm = new treemap<man,string> (new Compararule ()); Tm.put (New Man ("Zhou", 1), "Shanghai"), Tm.put (New Man ("Zhou", 1), "Northeast");//To guarantee the hash table, so the hashcode and Equals method in man is to replicate Tm.put (New Man ( "Wang", 4), "Beijing"), Tm.put (New Man ("Sun", 2), "Guangzhou"), Tm.put ("Zhao", 6), "Shandong"); iterator<map.entry<man,string >> it = Tm.entryset (). iterator (); while (It.hasnext ()) {Map.entry<man, string> mentry = It.next (); Mans man = Mentry.getkey (); String bir = Mentry.getvalue (); System.out.println ("Man-bir:" +man.geTname () + "," +man.getage () + "-" +bir);}}} 


The three big sets of collections: List, set, Map, and that's the end.
See array, arrays, angular labels, fast query speed
See link, linked list, add, get, remove Frist/last method
See hash, hash table, element uniqueness, overwrite Hashcode method and Equals method
See tree, Binary tree, sort, two big interfaces comparator,comparable

Map itself does not have an iterator, but it can return the set map via Keyset,entry, and then through the set iterator, access the key value pair

Tomorrow is the study of generics. To focus on mastering, refueling!!!

Java Learning lesson 39th (Common Object API)-set frame (vii)-MAP collection and its subclass objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.