1.Map Collection Overview and Features
* A:map Interface Overview
* To Repeat,
* See API to know,
* The object that maps the key to the value,
* A map cannot contain duplicate keys,
* Each key can be mapped to at most one value.
* B:map interface and collection interface are different
* Map is double-row, collection is single-row.
* Map key is unique, collection sub-system set is unique, that is, do not repeat.
* The data structure value of the map collection is valid for the key, regardless of the value; The data structure of the collection collection is valid for the element.
Overview of features for 2.MAP collections
* A: Add features
* V put (K key,v value): add element.
* If the key is stored for the first time, the element is stored directly, returning NULL
* If the key does not exist for the first time, replace the previous value with a value and return the previous value
* B: Delete function
* Void Clear (): Removes all key-value pair elements
* V Remove (Object key): Deletes the key value pair element according to the key, and returns the value
* C: Judging function
* Boolean ContainsKey (Object key): Determines whether the collection contains the specified key
* Boolean Containsvalue (Object value): Determines whether the collection contains the specified value
* Boolean isEmpty (): Determines whether the collection is empty
* D: Get function
* set<map.entry<k,v>> EntrySet ():???
* Entry is the interface within the map interface, the internal interface,
* V----Get (Object key): Gets the value based on the key
* Set<k>--KeySet (): Gets the collection of all the keys in the collection
* Collection<v> values (): Gets the collection of all values in the collection
* E: Length function
* int size (): Returns the number of key-value pairs in the collection.
The value of the traversal key for the 3.Map collection
* Key to find value ideas:
* Gets the collection of all Keys keyset ()
* Traverse the collection of keys, get to each key get ()
* Find values by key
new hashmap<> (); Hm.put ( "Zhang San", 23); Hm.put ( "Zhang San", 23); Hm.put ( "John Doe", 24); Hm.put ( "Harry", 25); Hm.put ( "Harry", 25); Hm.put ( "Harry", 25); Hm.put ( "Zhao Liu", 26); 1.keyset (), get (), enhanced for loop for Span style= "color: #000000;" > (String Key:hm.keySet ()) {Integer value = Hm.get (key); System.out.println (Key + "," +value "); }
The traversal key value of the 4.Map collection for the object to find the key and value
*: Key value pairs object to find key and value idea:
* Gets the collection of all key-value pairs of objects
* Iterate over the collection of key-value pairs of objects and get to each key-value pair object
* Find keys and values for objects based on key values
The key value of the traversal of the map collection for the object to find the keys and values 2.entrySet (),entry<>, GetValue (), GetKey (), enhanced for loop for (entry<string,integer> Entry:hm.entrySet ()) { = entry.getkey (); = Entry.getvalue (); System.out.println (Key+ "," +value); }
hashmap collection key is a case where the STIRNG value is a String HashMap <string, string> HM = new hashmap<> (); Hm.put ( "Zhang San", "Beijing" "John Doe", "Dalian" "Harry", "Shanghai" "Zhao Liu", "Nanjing" // {Zhao Liu = nanjing, Zhang San = BEIJING, John Doe = Dalian, Harry = shanghai} }
TreeMap Collection key is a case of string value is string TreeMapNew treemap<>(); Ts.put ("1", "Beijing"); Ts.put ("5", "Nanjing"); Ts.put ("2", "Tian Jing"); Ts.put ("4", "Tokyo"); Ts.put ("3", "West Beijing"); Ts.put ("6", "Mid-all"); SYSTEM.OUT.PRINTLN (TS); // {1= BEIJING, 2 = Tian Jing, 3 = Nishi-Kyo, 4 = Tokyo, 5= nanjing, 6 = central}
The TreeMap Collection key is a case where the student value is string TreeMap<student, string> ts =NewTreemap<> (NewComparator<student>() {@Override Public intCompare (Student s1, Student S2) {intnum =s1.getname (). CompareTo (S2.getname ()); returnNum ==0? S1.getage ()-s2.getage (): num; } }); Ts.put (NewStudent ("Zhang San", 23), "Beijing"); Ts.put (NewStudent ("John Doe", 24), "Tokyo"); Ts.put (NewStudent ("John Doe", 25), "Tokyo"); Ts.put (NewStudent ("John Doe", 24), "Tokyo"); Ts.put (NewStudent ("Harry", 25), "Nanjing"); Ts.put (NewStudent ("Harry", 25), "Nanjing"); SYSTEM.OUT.PRINTLN (TS);
Requirement: Count the number of occurrences of each character in the string str= "AABBBCCC"; Char[] arr = Str.tochararray ();//Convert a string to a character arrayHashmap<character, integer> HM =NewHashmap<> ();//Create a HashMap object for(CharC:arr) {//Enhanced for Loop if(! Hm.contaionskey (c)) {//If there is no value in the HM Add, key is a single character, the value is initially 1, after each has the same added 1. Hm.put (c,1); }Else{hm.put (C,hm.get (c)+1); } }
Set nesting HashMap nested HashMap HashMap<string, hashmap<student, string>> HM =NewHashmap<>(); HashMap<student, string> hm1 =NewHashmap<>(); Hm1.put (NewStudent ("Zhang San", 23), "Beijing" ); Hm1.put (NewStudent ("John Doe", 24), "Shanghai" ); Hm1.put (NewStudent ("Zhang San", 23), "Nanjing" ); Hm1.put (NewStudent ("Harry", 25), "Tokyo" ); Hm1.put (NewStudent ("Harry", 25), "Nishi-kyo" ); Hm1.put (NewStudent ("Zhao Liu", 26), "Central" ); HashMap<student, string> hm2 =NewHashmap<>(); Hm2.put (NewStudent ("Zhang 3", 23), "Beijing" ); Hm2.put (NewStudent ("Li 4", 24), "Shanghai" ); Hm2.put (NewStudent ("Zhang 3", 23), "Nanjing" ); Hm2.put (NewStudent ("Wang 5", 25), "Tokyo" ); Hm2.put (NewStudent ("Wang 5", 25), "Nishi-kyo" ); Hm2.put (NewStudent ("Zhao 6", 26), "Zhong June" ); Hm.put ("First group.", HM1); Hm.put ("The second group of people", HM2); for(String key:hm.keySet ()) {HashMap<student, string> value =Hm.get (key); for(Student val:value.keySet ()) {System.out.println (key+ "," + val + "," +Value.get (val)); } } } /*First Group of people, Student [Name= Zhang San, age=23], Nanjing First Group, Student [name= John Doe, age=24], Shanghai First group, Student [Nam E= Harry, Age=25], the first group of people in western Beijing, Student [name= Zhao Liu, age=26], the second group of people, Student [name= Zhang 3, age=23], Nanjing The second group of people, Student [Name= Wang 5, age=25], the second group of the West, Student [Name= Zhao 6, age=26], the second group of people, Student [Name= Lee 4, age =24], Shanghai*/
The difference between 5.HashMap and Hashtable
* the difference between HashMap and Hashtable * The bottom is the hash table , * hashmap:jdk2.0, different steps (thread unsafe), high efficiency, can store null values and NULL keys * hashtable:jdk1.0,synchronous (thread-safe), inefficient, cannot store null values and NULL keys
Overview of 6.Collections tool classes and common ways to explain
* A:collections class overview
* Collection----The root interface of the collection hierarchy
* Collections----The tool class for Operation collection
* Tool classes for collection operations
* B:collections Member method
public static <T> void sort (list<t> List)//Sort
public static <T> int BinarySearch (list<?> list,t key)//binary lookup
public static <T> T Max (collection<?> coll)//MAX
public static void reverse (list<?> List)//Invert
public static void Shuffle (list<?> List) unordered
7. Generic fixed down boundary
* ? Super E----E is sub-class
* ? Extends e----E is the parent class
Collections Tool class, Map collection, HashMap, Hashtable (18)