java 遍历List 和 Map的几种方法

java遍历List

1、(性能最差)

  1. for(String tmp:list)
  2. {
  3. //System.out.println(tmp);
  4. }

2、(性能最好)

  1. for(int i = 0; i < list.size(); i++)
  2. {
  3. list.get(i);
  4. //System.out.println(list.get(i));
  5. }

3、

  1. Iterator<String> iter = list.iterator();
  2. t1=System.currentTimeMillis();
  3. while(iter.hasNext())
  4. {
  5. iter.next();
  6. //System.out.println(iter.next());
  7. }

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);                     
}

上一篇:python multiprocessing多进程模块


下一篇:python笔记9 线程进程 threading多线程模块 GIL锁 multiprocessing多进程模块 同步锁Lock 队列queue IO模型