1. Map
2. HashMap 1. Add Elements
Import java. util. hashMap; import java. util. map; public class MapDemo {public static void main (String [] args) {/* defines a Map set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); System. out. println ("keys and values in the Map set:" + map); System. out. println ("----------------");/* use the put Method to add elements. The returned value is the value corresponding to the current key. If not, null is returned. * when the same key is added, then the new key and value will replace the original key and value **/System. out. println (map. put ("111", "Zhao "); // here, Li Si is returned, because the first value of the 111 key in this collection is System. out. println (map. put ("113", "Wang Gang"); // at this time, the return value is null, because there is no System prior to the 113 key. out. println ("key and value in Map set:" + map) ;}result: Key and value in Map set: {112 = Wang Wu, 110 = Zhang San, 111 = Li Si} ---------------- key and value in the Li Si nullMap set: {112 = Wang Wu, 113 = Wang Gang, 110 = Zhang San, 111 = Zhao Liu}
2. Judgment Element
Import java. util. hashMap; import java. util. map; public class MapDemo {public static void main (String [] args) {/* defines a Map set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); System. out. println ("key and value in the Map set:" + map);/** use booleancontainsKey (Object key) to determine whether the key exists in the set. Use boolean * containsValue (Objectvalue) determines whether this value exists in this set */System. out. println ("whether the set contains the 110 key:" + map. containsKey ("110"); System. out. println ("whether the set contains the 000 key:" + map. containsKey ("000"); System. out. println ("whether the set contains three values:" + map. containsValue ("Zhang San"); System. out. println ("whether the set contains the Wang Mei value:" + map. containsValue ("Wang Mei") ;}result: the key and value in the Map set: {112 = Wang Wu, 110 = Zhang San, 111 = Li Si}. Does the set contain the 110 key: whether the true set contains the 000 key: false, whether the set contains three values: true, whether the set contains the Wang Mei value: false
3. delete an element
Import java. util. hashMap; import java. util. map; public class MapDemo {public static void main (String [] args) {/* defines a Map set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); map. put ("113", "Zhao six"); System. out. println ("key and value in the Map set:" + map);/** use the remove (Objectkey) Key to delete elements. The returned value is the value corresponding to the key, if null is not returned, void * clear () is used to clear the elements in the Set */System. out. println ("delete elements corresponding to the 110 key:" + map. remove ("110"); // here the System is returned. out. println ("delete elements corresponding to the 120 key:" + map. remove ("120"); // null is returned here, because there is no 120 value in this set System. out. println ("Delete the set of specified elements:" + map); map. clear (); // clear the set System. out. println ("set after clearing set:" + map) ;}result: Key and value in Map set: {112 = Wang Wu, 113 = Zhao 6, 110 = Zhang San, 111 = Li Si} Delete the element corresponding to the 110 key: Zhang San Delete the element corresponding to the 120 key: null Delete the set of the specified element: {112 = Wang Wu, 113 = Zhao 6, 111 = Li Si} The set after clearing the set :{}
3. Get element 1. Single get
Import java. util. hashMap; import java. util. map; public class MapDemo {public static void main (String [] args) {/* defines a Map set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); System. out. println ("key and value in the Map set:" + map);/** get a single value corresponding to the key using get (Objectkey). If it does not exist, it will be null */System. out. println ("Get the value of 110:" + map. get ("110"); // here, the System is returned. out. println ("Get the value of 111:" + map. get ("111"); // here return null} result: the key and value in the Map set: {110 = Zhang San} get the corresponding value of 110: john gets the value corresponding to 111: null
2. Get the set of keys
Package www. fuxi. jihe; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class MapDemo {public static void main (String [] args) {/* defines a Map Set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); System. out. println ("key and value in the Map Set:" + map);/** use Set <K> keySet () get the Set return value of the Set key in the Set. * We can use the iterator in the Set to read the key, and then use get (key) obtain the element */Set <String> set = map. keySet (); // The Iterator set for obtaining keys <String> it = set. iterator (); // iterator for getting the key set while (it. hasNext () {String key = it. next (); System. out. println (key + ":" + map. get (key);}/** use Set <K> keySet () to obtain the Set return value of the middle key in the Set. Set the Set and replace it with an array, and then read it. */System. out. println ("---------- \ n"); Object [] keys = set. toArray (); for (int I = 0; I <keys. length; I ++) {System. out. println (String) keys [I] + ":" + map. get (String) keys [I]) ;}} result: Key and value in the Map set: {112 = Wang Wu, 110 = Zhang San, 111 = Li Si} 112: wang Wu 110: Zhang San 111: Li Si ---------- 112: Wang Wu 110: Zhang San 111: Li Si
3. Set of retrieved values
Packagewww. fuxi. jihe; import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class MapDemo {publicstatic void main (String [] args) {/* defines a Map Set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); System. out. println ("key and value in the Map set:" + map);/*** use the method Collection <V> values () to obtain all values in the Set */System. out. println ("value in the Set:"); Collection <String> values = map. values (); Iteratorit = values. iterator (); while (it. hasNext () {System. out. println (it. result: Key and value in the Map set: {112 = Wang Wu, 110 = Zhang San, 111 = Li Si} value in the Set: Wang Wu, Zhang San, Li Si
4. ing
Import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class MapDemo {public static void main (String [] args) {/* defines a Map Set. The key and value types are both String */Map <String, string> map = new HashMap <String, String> (); map. put ("110", "Zhang San"); map. put ("111", "Li Si"); map. put ("112", "Wang Wu"); System. out. println ("key and value in the Map Set:" + map);/** use Set <Map. entry <K, V> entrySet () to obtain the ing relationship in the Set * then read the Set through the iterator * and then use Map. key obtained by K getKey () in Entry, value obtained by V getValue () */Set <Map. entry <String, String> set = map. entrySet (); // obtains the ing set Iterator <Map. entry <String, String> it = set. iterator (); // get iterator while (it. hasNext () {Map. entry <String, String> en = it. next (); // obtain the element String key = en in the set. getKey (); String value = en. getValue (); System. out. println ("key-value:" + key + "-" + value) ;}} result: keys and values in the Map set: {112 = Wang Wu, 110 = Zhang San, 111 = Li Si} key-value: 112-Wang Wu key-value: 110-Zhang three key-value: 111-Li Si
Static interface |
Map. Entry <K, V> Ing (key-value pairs ). |
4. Comparison of HashMap and TreeMap
1. Use HashMap
Import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class HashMapDemo {public static void main (String [] args) {/* defines HashMap, which stores Student and address */HashMap <Student, string> map = new HashMap <Student, String> (); map. put (new Student ("James", 23), "Beijing"); map. put (new Student ("Li Si", 24), "Shanghai"); map. put (new Student ("Wang Wu", 23), "Nanjing"); map. put (new Student ("Zhang San", 23), "Guangzhou"); // stores a duplicate value. // The first read method is Set <Student> set = map. keySet (); Iterator <Student> it = set. iterator (); while (it. hasNext () {Student stu = it. next (); String name = stu. getName (); int age = stu. getAge (); String address = map. get (stu); System. out. println (name + ":" + age + ":" + address);} System. out. println ("---------"); System. out. println ("the following uses ing to read"); Set <Map. entry <Student, String> ens = map. entrySet (); Iterator <Map. entry <Student, String> its = ens. iterator (); while (its. hasNext () {Map. entry <Student, String> me = its. next (); Student stu = me. getKey (); String address = me. getValue (); System. out. println (stu. getName () + ":" + stu. getAge () + ":" + address) ;}}result: Zhang San: 23: Guangzhou Wang Wu: 23: Nanjing Li Si: 24: shanghai --------- the following uses the ing relationship to read Michael JACOB: 23: Guangzhou Wang 5: 23: Nanjing LI 4: 24: Shanghai
2. sort by TreeMap
Import java. util. hashMap; import java. util. iterator; import java. util. set; import java. util. treeMap; public class TreeMapDemo {public static void main (String [] args) {/* defines HashMap, which stores Student and address */TreeMap <Student, string> map = new TreeMap <Student, String> (); map. put (new Student ("James", 23), "Beijing"); map. put (new Student ("Li Si", 21), "Shanghai"); map. put (new Student ("Wang Wu", 24), "Nanjing"); map. put (new Student ("Zhao ", 26), "Guangzhou"); Set <Student> set = map. keySet (); Iterator <Student> it = set. iterator (); while (it. hasNext () {Student stu = it. next (); String name = stu. getName (); int age = stu. getAge (); String address = map. get (stu); System. out. println (name + ":" + age + ":" + address) ;}}result: Li Si: 21: Shanghai Zhang San: 23: Beijing Wang Wu: 24: Nanjing Zhao Liu: 26: Guangzhou
Import java. util. comparator; public class StuNameComparatorimplements Comparator <Student> {/* sort by name. If the name is the same, sort by age */public int compare (Student o1, Student o2) {int num = o1.getName (). compareTo (o2.getName (); if (num = 0) {return o1.getAge ()-o2.getAge () ;}return num ;}} import java. util. hashMap; import java. util. iterator; import java. util. set; import java. util. treeMap; public class TreeMapDemo {public static void main (String [] args) {/* defines HashMap, which stores Student and address */TreeMap <Student, string> map = new TreeMap <Student, String> (newStuNameComparator (); map. put (new Student ("java03", 23), "Beijing"); map. put (new Student ("net02", 21), "Shanghai"); map. put (new Student ("java01", 24), "Nanjing"); map. put (new Student ("net03", 26), "Guangzhou"); Set <Student> set = map. keySet (); Iterator <Student> it = set. iterator (); while (it. hasNext () {Student stu = it. next (); String name = stu. getName (); int age = stu. getAge (); String address = map. get (stu); System. out. println (name + ":" + age + ":" + address) ;}}result: java01: 24: Nanjing java03: 23: Beijing net02: 21: Shanghai net03: 26: Guangzhou
5. Exercise
Packagewww. fuxi. jihe; importjava. util. hashMap; importjava. util. iterator; importjava. util. map; importjava. util. set; public classMapDemo {public static void main (String [] args) {String str = "ahjdkdmkamkmixkxmcsnlasickdj"; char [] cs = str. toCharArray (); HashMap <Character, Integer> map = new HashMap <Character, Integer> (); for (char c: cs) {if (map. containsKey (c) {Integer count = map. get (c); // obtain the number of maps corresponding to this character, count ++; // number ++ map. put (c, count); // re-add it to overwrite the original} else {map. put (c, newInteger (1); // The starting quantity is 1} StringBuffer bu = new StringBuffer (); Set <Character> set = map. keySet (); Iterator <Character> it = set. iterator (); while (it. hasNext () {Character c = it. next (); Integer count = map. get (c); bu. append (c ). append ("("). append (count ). append (")");} System. out. result: d (3) s (2) c (2) a (3) n (1) l (1) m (4) j (2) k (5) h (1) x (2) I (2)
6. simulate a school
Package www. fuxi. jihe; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. set; class CollegeStudent {private Stringnum; private Stringname; CollegeStudent (Stringnum, String name) {super (); this. num = num; this. name = name;} public StringgetNum () {return num;} public StringgetName () {returnname;} public class Demo {public staticvoid main (String [] agrs) {/* school */HashMap <String, List <CollegeStudent> bdxy = new HashMap <String, List <CollegeStudent> (); list <CollegeStudent> ruanjian = new ArrayList <CollegeStudent> (); // software class List <CollegeStudent> jiying = new ArrayList <CollegeStudent> (); // calculate the expected class/* Add the class to the school */bdxy. put ("ruanjian", ruanjian); bdxy. put ("jiying", jiying);/* Add students to the class */ruanjian. add (newCollegeStudent ("110", "zhansgan"); ruanjian. add (newCollegeStudent ("111", "lisi"); ruanjian. add (newCollegeStudent ("112", "Wang Wu"); jiying. add (newCollegeStudent ("210", "wangang"); jiying. add (newCollegeStudent ("211", "wangemi"); jiying. add (newCollegeStudent ("212", "xiaoqiang");/* traverse the class in the school and then read the student information in the class */Set <String> set = bdxy. keySet (); // obtain the class name set Iterator <String> it = set. iterator (); while (it. hasNext () {StringClassName = it. next (); List <CollegeStudent> ClassInfo = bdxy. get (ClassName); // obtain the class System. out. println (ClassName); show (ClassInfo) ;}/ * output class student */public staticvoid show (List <CollegeStudent> list) {Iterator <CollegeStudent> it = list. iterator (); while (it. hasNext () {CollegeStudentstu = it. next (); System. out. println ("|-" + stu. getNum () + ":" + stu. getName () ;}} result: jiying |-210: wangang |-211: wangemi |-212: xiaoqiangruanjian |-110: zhansgan |-111: lisi |-112: wang Wu
7. Add knowledge points
Package www. fuxi. jihe; import java. util. hashMap; import java. util. map; public class Text {public static void main (String [] args) {Map <String, String> map = new HashMap <String, String> (); map. put ("110", "zhangsan"); map. put ("111", "lisi"); map. put ("112", "wangwu"); System. out. println ("set:" + map);/* boolean isEmpty () determines whether it is null */System. out. println (map. isEmpty ();/* int size () determines the number of ing relationships in the Set */System. out. println ("Size:" + map. size () ;}} result: Set: {112 = wangwu, 110 = zhangsan, 111 = lisi} false size: 3