Java --- 29 --- two methods for retrieving elements in Map, javamap deletes Elements
There is no iterator in Map. How do elements in Map be extracted?
There are two methods:
One is to use Set <k> keySet ()
One is to use Set <Map. Entry <k, v> entrySet ()
KeySet: stores all the keys in the map to the Set. You can obtain the value of value through the getKey () method of map.
EntrySet: returns the Set view of the ing contained in the ing. Contains both keys and values. The data type of this ing is Map. Entry.
Public class MapDemo {public static void main (String [] args) {// TODO Auto-generated method stubkeySet_method (); entrySet_method ();} public static void entrySet_method () {Map <String, String> map = new HashMap <String, String> (); map. put ("01", "a5"); map. put ("02", "a1"); map. put ("03", "a2"); map. put ("04", "a3"); map. put ("05", "a4"); Set <Map. entry <String, String> entryset = map. entrySet (); Iterator <Map. entry <String, String> it = entryset. iterator (); while (it. hasNext () {Map. entry <String, String> me = it. next (); String key = me. getKey (); String value = me. getValue (); System. out. println ("key:" + key + "Value:" + value) ;}} public static void keySet_method () {Map <String, String> map = new HashMap <String, string> (); map. put ("01", "a5"); map. put ("02", "a1"); map. put ("03", "a2"); map. put ("04", "a3"); map. put ("05", "a4"); Set <String> keyset = map. keySet (); Iterator <String> it = keyset. iterator (); while (it. hasNext () {// obtain the key String key = it. next (); String value = map. get (key); System. out. println ("key:" + key + "Value:" + value );}}}
Store custom elements in two ways:
Class Stu implements Comparable <Stu> {private String name; private int age; public Stu (String name, int age) {super (); this. name = name; this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}@ Overridepublic boolean equals (Object obj) {// TODO Auto-generated method stubif (! (Obj instanceof Stu) throw new ClassCastException ("not the object of this class"); Stu s = (Stu) obj; return this. name. equals (s. name) & this. age = s. age ;}@ Overridepublic int hashCode () {// TODO Auto-generated method stubreturn name. hashCode () + age * 27;} public int compareTo (Stu o) {// TODO Auto-generated method stubInteger num = new Integer (this. age ). compareTo (new Integer (o. age); if (num = 0) return this. name. compareTo (o. name); return num ;}} public class MapDemo2 {public static void main (String [] args) {// TODO Auto-generated method stubMap <Stu, string> map = new HashMap <Stu, String> (); map. put (new Stu ("01", 10), "beijing"); map. put (new Stu ("02", 9), "shanghai"); map. put (new Stu ("03", 8), "huangzhou"); map. put (new Stu ("04", 15), "tianjin"); Set <Stu> keySet = map. keySet (); Iterator <Stu> it = keySet. iterator (); while (it. hasNext () {Stu key = it. next (); String value = map. get (key); System. out. println ("key:" + key. getName () + "---" + key. getAge () + "Value:" + value );} /*************************************** * *********/Set <Map. entry <Stu, String> entrySet = map. entrySet (); Iterator <Map. entry <Stu, String> iterator = entrySet. iterator (); while (iterator. hasNext () {Map. entry <Stu, String> me = iterator. next (); Stu key = me. getKey (); String value = me. getValue (); System. out. println ("key:" + key. getName () + "---" + key. getAge () + "Value:" + value );}}}