java遍历List
1、(性能最差)
- for(String tmp:list)
- {
- //System.out.println(tmp);
- }
2、(性能最好)
- for(int i = 0; i < list.size(); i++)
- {
- list.get(i);
- //System.out.println(list.get(i));
- }
3、
- Iterator<String> iter = list.iterator();
- t1=System.currentTimeMillis();
- while(iter.hasNext())
- {
- iter.next();
- //System.out.println(iter.next());
- }
java中遍历Map
第一种用for循环
Java代码
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代
Java代码
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());
}
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代码
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);
}
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代码
Iterator it=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}