List的遍历方法
List<String> list = new ArrayList<String>(
Arrays.asList("tom","cat","Jane","jerry"));
//方法1 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代
Iterator it1 = list.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
//方法2 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代
for(Iterator it2 = list.iterator();it2.hasNext();){
System.out.println(it2.next());
}
//方法3 增强型for循环遍历
for(String value:list){
System.out.println(value);
}
//方法4 一般型for循环遍历
for(int i = 0;i < list.size(); i ++){
System.out.println(list.get(i));
}
Set的遍历方法
List<String> list = new ArrayList<>(
Arrays.asList("tom","cat","Jane","jerry"));
Set<String> set = new HashSet<>();
set.addAll(list);
//方法1 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代
Iterator it1 = set.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
//方法2 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代
for(Iterator it2 = set.iterator();it2.hasNext();){
System.out.println(it2.next());
}
//方法3 增强型for循环遍历
for(String value: set){
System.out.println(value);
}
Map的遍历方法
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("tom1",1);
map.put("tom2",2);
map.put("tom3",3);
map.put("tom5",5);
map.put("tom4",4);
//1.4版本之前
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.print(key+" -->: "+value+"\t");
}
System.out.println("\n================");
//1.5 for-each 循环操作
for(String o : map.keySet()){
System.out.print (map.get(o)+"\t");
}
//方法一: 用entrySet()
//返回的 set 中的每个元素都是一个 Map.Entry 类型。
// 推荐,尤其是容量大时
System.out.println("\n=====用entrySet()=======");
Iterator it1 = map.entrySet().iterator();
while(it1.hasNext()){
Map.Entry m=(Map.Entry)it1.next();
System.out.println("[name = " + m.getKey()
+ "] age = " + m.getValue());
}
// 方法二:jdk1.5支持,用entrySet()和For-Each循环()
System.out.println("\n=用entrySet()和For-Each===");
for (Map.Entry<String, Integer> m : map.entrySet()) {
System.out.println("[name = " + m.getKey() + "] age = " + m.getValue());
}
// 方法三:用keySet() 普遍使用,二次取值
System.out.println("\n=====用keySet()=======");
Iterator it2 = map.keySet().iterator();
while (it2.hasNext()){
String key = (String) it2.next();
System.out.println("[name = " + key
+ "] age = " + map.get(key) );
}
// 方法四:jdk1.5支持,用keySEt()和For-Each循环
System.out.println("\n=====用keySet()和For-Each=====");
for(Object key: map.keySet()){
System.out.println("[name = " + key
+ "] age = " + map.get(key) );
}
参考博客:http://blog.youkuaiyun.com/sunrainamazing/article/details/71577662