一.遍历HashMap
package struct;import java.util.*;import java.util.Map.Entry;/** * Created by Mickel.Xiang on 2018/4/23. */public class TraverseHashMap { public static void main(String[] args) { Mapmap=new HashMap (); map.put("1", "value1"); map.put("2", "value2"); map.put("3", "value3"); map.put("4", "value4"); //第一种:普通使用,二次取值 System.out.println("\n通过Map.keySet遍历key和value:"); for(String key:map.keySet()) { System.out.println("Key: "+key+" Value: "+map.get(key)); } //第二种 System.out.println("\n通过Map.entrySet使用iterator遍历key和value: "); Iterator map1it=map.entrySet().iterator(); while(map1it.hasNext()) { Map.Entry entry=(Entry ) map1it.next(); System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue()); } //第三种:推荐,尤其是容量大时 System.out.println("\n通过Map.entrySet遍历key和value"); for(Map.Entry entry: map.entrySet()) { System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue()); } //第四种 System.out.println("\n通过Map.values()遍历所有的value,但不能遍历key"); for(String v:map.values()) { System.out.println("The value is "+v); } }}
二.遍历HashSet
package struct;import java.util.*;/** * Created by Mickel.Xiang on 2018/4/23. */public class TraverseHashSet { public static void main(String[] args){ Setset = new HashSet (); set.add("aaa"); set.add("bbb"); set.add("ccc"); //通过迭代器遍历HashSet Iterator it = set.iterator(); while(it.hasNext()) { System.out.println(it.next()); } for(Iterator it1=set.iterator();it1.hasNext();) { System.out.println(it1.next()); } System.out.println("=================="); //通过加强for循环遍历HashSet for(String s: set) { System.out.println(s); } }}
Set set = new HashSet();
for(int i=0;i<100;i++)
{ set .add("123"); }for(Iterator it=set.iterator();it.hasNext();)
{ System.out.println(it.next()); }三.遍历Hashtable(同步、线程安全的)
package struct;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;public class TraverseHashTable { public static void main(String[] args) { Hashtablehashtable=new Hashtable (); hashtable.put("1", "aa"); hashtable.put("2", "bb"); hashtable.put("3", "cc"); //第一种hashtable遍历方式 使用keySet() System.out.println("第一种遍历方式"); for(Iterator iterator=hashtable.keySet().iterator();iterator.hasNext();){ String key=iterator.next(); System.out.println("key-----"+key); System.out.println("value--------"+hashtable.get(key)); } //第二种hashtable遍历方式 使用entrySet() System.out.println("第二种遍历方式"); for(Iterator > iterator=hashtable.entrySet().iterator();iterator.hasNext();){ Entry entry=iterator.next(); System.out.println("key---------"+entry.getKey()); System.out.println("value------------"+entry.getValue()); } //第三种hashtable遍历方式 System.out.println("第三种遍历方式"); for(Map.Entry entry: hashtable.entrySet()){ System.out.println("key---------"+entry.getKey()); System.out.println("value--------"+entry.getValue()); }//第四种遍历方式 使用keys() System.out.println("第四种遍历方式"); Enumeration e=hashtable.keys(); while(e.hasMoreElements()){ String key=e.nextElement(); System.out.println("key-----"+key); System.out.println("value-------"+hashtable.get(key)); }//第五中遍历方式(获取所有的值) 使用elements() System.out.println("第五种遍历方式"); Enumeration e2=hashtable.elements(); while (e2.hasMoreElements()) { String string = (String) e2.nextElement(); System.out.println(string); } }}