Java Tour (22)--map Overview, sub-class object characteristics, common methods, keyset,entryset,map small exercises
Keep on going, ladies and gentlemen!
In fact, our data structure, only the knowledge point of this map, usually developed, but also can see him, so it is very worthwhile to learn a point of knowledge, we drove directly
I. Map overview
Generic < k,v> key-value pairs, mapping relationships
Basic Features
- The set stores key-value pairs, is a pair of pairs, and guarantees the uniqueness of the key.
- 1. Add
- Put (key, values)
- Putall ()
- 2. Delete
- Clear ()
- Remove (Object key)
- 3. Judging
- Containsvalue (Object value)
- ContainsKey (Object key)
- IsEmpty ()
- 4. Get
- Get (Object key)
- Size ()
- VALUES ()
- EntrySet ()
- KeySet ()
And that's how we do our learning steps.
Two. sub-class object characteristics
Map has three sub-classes
- Hashtable
- The underlying is a hash table data structure that cannot be stored in null values or keys, which are thread-synchronized
- HashMap
- The underlying is the hash table data structure, which allows null key-value pairs to be used, and threads to be unsynchronized. High efficiency
- TreeMap
- The bottom layer is a two-fork tree data structure, which can be used to sort the keys in the map collection with different threads.
Map and set are very much like, actually the set bottom is using the Map collection
Three. Common methods
Let's see what they have in common.
Packagecom. LGL. Hellojava;Import Java. Util. Collection;Import Java. Util. HashMap;Import Java. Util. Map;public class Hellojjava {public static void main (string[] args) {map<string, string> Map = new HASHMAP&L T String, String> ();add element Map. Put("001","Zhangsan");Map. Put("002","Lisi");Map. Put("003","Wangwu");System. out. println("Original data:"+MAP);Determine if there is002The key System. out. println(Map. ContainsKey("002"));Delete System. out. println(Map. Remove("002"));System. out. println("After deletion:"+MAP);Get System. out. println("Get:"+map. Get("001"));The return value of the Get method can be used to determine whether a key exists in a map. Put(NULL,"haha");System. out. println("null:"+MAP);Get all the values in the map collection collection<string> value = Map. Values();System. out. println("Map value:"+values);}}
Here you can see the result of the output
However, it is important to note that the added element, if added, the same key, then the subsequent, will be added to overwrite the original key corresponding value, and put method will return the overridden value
Four. KeySet
Want to take out his value, he does not have an iterator, then our train of thought can change to get all of his keys and go get not to get the key value right, let's see
- KeySet
- The values in the map are stored in the set set because the set has an iterator, all the keys that can be taken out of the iteration method, and the value corresponding to each key according to the Get method
Packagecom. LGL. Hellojava;Import Java. Util. HashMap;Import Java. Util. Iterator;Import Java. Util. Map;Import Java. Util. Set;public class Hellojjava {public static void main (string[] args) {map<string, string> Map = new HASHMAP&L T String, String> ();Map. Put("001","Zhangsan");Map. Put("002","Lisi");Map. Put("003","Wangwu");Gets all the keys in the map collection first.SetCollectionSet<String> KeySet = Map. KeySet();With theSetThe collection can get the iterator iterator<string> Iterator = KeySet. Iterator();while (iterator. Hasnext()) {String string = Iterator. Next();A key can get its corresponding value through the Get method of the map collection String value = Map. Get(string);System. out. println("key:"+ string +"Values:"+ value);} }}
This method is better understood, right, but this is more troublesome, we look at another
Five. EntrySet
Packagecom. LGL. Hellojava;Import Java. Util. HashMap;Import Java. Util. Iterator;Import Java. Util. Map;Import Java. Util. Map. Entry;Import Java. Util. Set;public class Hellojjava {public static void main (string[] args) {map<string, string> Map = new HASHMAP&L T String, String> ();Map. Put("001","Zhangsan");Map. Put("002","Lisi");Map. Put("003","Wangwu");The map in the map collection is taken out and deposited intoSetIn the collectionSet<entry<string, string>> entryset = map. EntrySet();iterator<entry<string, string>> Iterator = EntrySet. Iterator();while (iterator. Hasnext()) {Map. Entry<string, string> entry = iterator. Next();System. out. println(Entry. GetKey() +":"+ Entry. GetValue());} }}
Defining generics is cumbersome, but it's easier to take it out, but what's the rationale? We can actually write a pseudo-code to illustrate
Package Com.lgl.hello; Public class HashMap implements Map { class hahs implements Map. Entry {@Override PublicObject GetKey () {//TODO auto-generated method stub return NULL; } @Override PublicObject GetValue () {//TODO auto-generated method stub return NULL; } }} interface Map { Public Static interface Entry { Public AbstractObject GetKey (); Public AbstractObject GetValue (); }}
Parent-child interface, direct access, internal rules
Six. Map Little Practice
We can use a little exercise to learn the rules, and the requirements, I write directly on the comments
PackageCom.lgl.hellojava;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map.Entry;ImportJava.util.Set; Public class Hellojjava { Public Static void Main(string[] args) {/** * Each student has a corresponding attribution to the student student, address string student properties: Name and Age * Note: the same name and age are regarded as the same student, guaranteeing the uniqueness of the student * * 1 Description Students 2. Define the map container, save the student as a key, and the address as the value 3. Gets the element in the map container * /Hashmap<student, string> HM =NewHashmap<student, string> (); Hm.put (NewStudent ("Zhangsan", the),"Beijing"); Hm.put (NewStudent ("Lisi", -),"Shanghai"); Hm.put (NewStudent ("Wangwu", -),"Guangzhou"); Hm.put (NewStudent ("Liliu",Ten),"Shenzhen");//First removal method keysetset<student> KeySet = Hm.keyset (); iterator<student> Iterator = Keyset.iterator (); while(Iterator.hasnext ()) {Student Student = Iterator.next (); String addr = hm.get (student); SYSTEM.OUT.PRINTLN (student +":"+ addr); }//second method of removal EntrySetSet<entry<student, string>> entryset = Hm.entryset (); Iterator<entry<student, string>> Iterator2 = Entryset.iterator (); while(Iterator2.hasnext ()) {entry<student, string> next = Iterator2.next (); System.out.println (Next.getkey () +":"+next.getvalue ()); } }}/** * Description Student * * @author LGL * */Class Student implements Comparable<student> {PrivateString name;Private intAge Public Student(String name,intAge) { This. name = name; This. Age = Age; }@Override Public int hashcode() {//TODO auto-generated method stub returnName.hashcode () + age * the; }@Override Public Boolean equals(Object obj) {if(! (objinstanceofStudent))Throw NewRuntimeException ("Type Mismatch"); Student s = (Student) obj;return This. Name.equals (S.name) && This. age = = S.age; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }@Override Public int CompareTo(Student s) {intnum =NewInteger ( This. Age). CompareTo (NewInteger (S.age));if(num = =0)return This. Name.compareto (S.name);returnNum }}
OK, the example is to take two out of the way, I believe you can also do a good job, OK, we have this lesson to the end of this, the next section goodbye
If you are interested, add the following group: 555974449
Java Tour (22)--map Overview, sub-class object characteristics, common methods, keyset,entryset,map small exercises