迭代器为我们提供了统一的遍历容器的方式,参见以下示例代码:
迭代器遍历List:
public class Testl { public static void main(String[] args) { List<String> aList = new ArrayList<String>(); for (int i = 0; i < 5; i++) { aList.add("a" + i); } System.out.println(aList); for (Iterator<String> iter = aList.iterator(); iter.hasNext();) { String temp = iter.next(); System.out.print(temp + "\t"); if (temp.endsWith("3")) {// 删除3结尾的字符串 iter.remove(); } } System.out.println(); System.out.println(aList); } }
执行结果如图所示:
注:
如果遇到遍历容器时,判断删除元素的情况,使用迭代器遍历!
迭代器遍历Set:
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Tesths { public static void main(String[] args) { Set<String> set = new HashSet<String>(); for (int i = 0; i < 5; i++) { set.add("a" + i); } System.out.println(set); for (Iterator<String> iter = set.iterator(); iter.hasNext();) { String temp = iter.next(); System.out.print(temp + "\t"); } System.out.println(); System.out.println(set); } }
执行结果如图所示:
迭代器遍历Map一:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Testma { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("A", "张三"); map.put("B", "李四"); Set<Entry<String, String>> ss = map.entrySet(); for (Iterator<Entry<String, String>> iterator = ss.iterator(); iterator.hasNext();) { Entry<String, String> e = iterator.next(); System.out.println(e.getKey() + "--" + e.getValue()); } } }
执行结果如图所示:
我们也可以通过map的keySet()、valueSet()获得key和value的集合,从而遍历它们。
迭代器遍历Map二:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Testhm { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("A", "张三"); map.put("B", "李四"); Set<String> ss = map.keySet(); for (Iterator<String> iterator = ss.iterator(); iterator.hasNext();) { String key = iterator.next(); System.out.println(key + "--" + map.get(key)); } } }
执行结果如图所示:
遍历List方法一:普通for循环
for(int i=0;i<list.size();i++){//list为集合的对象名 String temp = (String)list.get(i); System.out.println(temp); }
遍历List方法二:增强for循环(使用泛型!)
for (String temp : list) { System.out.println(temp); }
遍历List方法三:使用Iterator迭代器(1)
for(Iterator iter= list.iterator();iter.hasNext();){ String temp = (String)iter.next(); System.out.println(temp); }
遍历List方法四:使用Iterator迭代器(2)
Iterator iter =list.iterator(); while(iter.hasNext()){ Object obj = iter.next(); iter.remove();//如果要遍历时,删除集合中的元素,建议使用这种方式! System.out.println(obj); }
遍历Set方法一:增强for循环
for(String temp:set){ System.out.println(temp); }
遍历Set方法二:使用Iterator迭代器
for(String temp:set){ System.out.println(temp); }
遍历Map方法一:根据key获取value
Map<Integer, Man> maps = new HashMap<Integer, Man>(); Set<Integer> keySet = maps.keySet(); for(Integer id : keySet){ System.out.println(maps.get(id).name); }
遍历Map方法二:使用entrySet
Set<Entry<Integer, Man>> ss = maps.entrySet(); for (Iterator iterator = ss.iterator(); iterator.hasNext();) { Entry e = (Entry) iterator.next(); System.out.println(e.getKey()+"--"+e.getValue());