Packagetest2;ImportJava.util.*;Import Staticjava.lang.System.out; Public classTest2extendsstaticsuper{//put input, get, get Static voidmaptest () {//TODO Auto-generated method stubshashmap<string, string> map =NewHashmap<string, string>(); Map.put ("1", "A1"); Map.put ("2", "A2"); Map.put ("3", "A3"); //the method of traversal one, disadvantage, time-consumingIterator<string> Iterator =Map.keyset (). iterator (); while(Iterator.hasnext ()) {//Object key = iterator;Object key =Iterator.next (); System.out.print ("Key =" +key); System.out.println (", Value =" +Map.get (key)); //Iterator.next (); } //traversal method Two, time-savingIterator entries =Map.entryset (). iterator (); while(Entries.hasnext ()) {Map.entry Entry=(Map.entry) entries.next (); Object Key=Entry.getkey (); Object value=Entry.getvalue (); System.out.println ("key =" + key + ", Value =" +value); } System.out.println ("Map end!"); } Static voidsettest () {Set<String> Set1 =NewHashset<string>(); Set1.add ("S1_1"); Set1.add ("S1_2"); Set1.add ("S1_3"); Set1.add ("One"); System.out.println (SET1); Set<String> Set2 =NewHashset<string>(); Set2.add ("S2_1"); Set2.add ("S2_1"); Set2.add ("S2_3"); Set2.add ("One"); System.out.println (Set2); Set<String> sn =NewHashset<string>(SET1); Sn.retainall (Set2); SYSTEM.OUT.PRINTLN (SN); } Public Static voidMain (string[] args) {}}
Simple use of Map,set in Java