遍历集合的常见的集中方法
package com.sina.www;
import java.util.*;
publicclass Test3 {
publicstaticvoid main(String[] args) {
List l = new ArrayList();
l.add("世界你好!");
l.add("上海");
for (int i = 0; i < l.size(); i++) { // 普通的for循环取出集合中的元素
System.out.print(l.get(i));
}
System.out.println();
for (Object o : l) {// 增强for循环取出集合中的元素
System.out.print(o);
}
System.out.println();
for (Iterator it = l.iterator(); it.hasNext();) {// 利用迭代器取出集合中的元素
System.out.print(it.next());
}
System.out.println();
Iterator it = l.iterator();//利用迭代器取出集合中的元素
while (it.hasNext()) {
System.out.print(it.next());
}
}
}
java中遍历MAP的几种方法
1. Map<String,String> map=new HashMap<String,String>();
2. map.put("username", "qq");
3. map.put("passWord", "123");
4. map.put("userID", "1");
5. map.put("email", "qq@qq.com");
Map<String,String> map=new HashMap<String,String>();
map.put("username", "qq");
map.put("passWord", "123");
map.put("userID", "1");
map.put("email", "qq@qq.com");
第一种用for循环
Java代码
1. for(Map.Entry<String, String> entry:map.entrySet()){
2. System.out.println(entry.getKey()+"--->"+entry.getValue());
3. }
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代
Java代码
1. Set set = map.entrySet();
2. Iterator i = set.iterator();
3. while(i.hasNext()){
4. Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
5. System.out.println(entry1.getKey()+"=="+entry1.getValue());
6. }
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
用keySet()迭代
Java代码
1. Iterator it=map.keySet().iterator();
2. while(it.hasNext()){
3. String key;
4. String value;
5. key=it.next().toString();
6. value=map.get(key);
7. System.out.println(key+"--"+value);
8. }
Iterator it=map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=map.get(key);
System.out.println(key+"--"+value);
}
用entrySet()迭代
Java代码
1. Iterator it=map.entrySet().iterator();
2. System.out.println( map.entrySet().size());
3. String key;
4. String value;
5. while(it.hasNext()){
6. Map.Entry entry = (Map.Entry)it.next();
7. key=entry.getKey().toString();
8. value=entry.getValue().toString();
9. System.out.println(key+"===="+value);
10. }
package com.sina.www;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest2 {
public static void main(String[] args) {
Map<String, String> m = new HashMap<String, String>();
// m.put(001, new Person("关羽", 23));
// m.put(002, new Person("刘备", 24));
// m.put(003, new Person("百度", 22));
//
m.put("sa", "123");
m.put("sb", "143");
m.put("sc", "153");
m.put("sd", "184");
Set keyset = m.keySet(); // 对Map集合的迭代方法一keyset 迭代
Iterator it = keyset.iterator();
while (it.hasNext()) {
String p = (String) it.next();
System.out.print("value=" + m.get(p) + " ");
}
// System.out.println(m.get("sa"));
Iterator it2 = m.entrySet().iterator(); // Map集合的迭代方法二entrySet迭代
System.out.println();
System.out.println("长度为" + m.entrySet().size());
String key;
String value;
while (it2.hasNext()) {
Map.Entry entry = (Map.Entry) it2.next();
key = entry.getKey().toString();
value = entry.getValue().toString();
System.out.print(key + "=" + value + " ");
}
System.out.println();
System.out.println("------------------");
for (Map.Entry entry : m.entrySet()) { // // Map集合的迭代方法三for循环迭代
System.out.print(entry.getKey() + "=" + entry.getValue() + " ");
}
}
}