map和list遍历基础
生活随笔
收集整理的這篇文章主要介紹了
map和list遍历基础
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文屬原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:http://www.cnblogs.com/robinjava77/p/5456085.html?(Robin)
Map
1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4 5 /** 6 * Created by robin on 2016/4/12. 7 * 8 * @author robin 9 */ 10 public class MapTest { 11 12 public static void main(String args[]){ 13 traversalKey(); 14 } 15 16 /** 17 * 在不知道m(xù)ap keys 的情況下 遍歷map的key和value 18 * 19 * 總結(jié) 20 * 1.僅需要鍵(keys) map.keySet() 21 * 2.僅需要值(values) map.values() 22 * 3.使用的語(yǔ)言版本低于java 5,或是打算在遍歷時(shí)刪除entries,必須使用方法三 23 * 4.鍵值都要使用方法二。 24 */ 25 public static void traversalKey(){ 26 Map<String,String> map = new HashMap<String, String>(); 27 map.put("1","a"); 28 map.put("2","b"); 29 map.put("3","c"); 30 map.put("4","d"); 31 map.put("5","e"); 32 map.put("6","e"); 33 /**第一種方法:keySet() 獲取key值,通過(guò)key值get Value 效率最低!由key get value 耗時(shí)*/ 34 for (String key:map.keySet()){ 35 System.out.println("key:"+key+"--value:"+map.get(key)); 36 } 37 /**第二種方法:Map.Entry 效率高于第一種,一般推薦這種寫(xiě)法*/ 38 for(Map.Entry<String,String> entry:map.entrySet()){ 39 System.out.println("key:"+entry.getKey()+"--value:"+entry.getValue()); 40 } 41 /**第三種方法:Iterator遍歷 這種遍歷方法新舊java版本皆通用,在遍歷刪除map的key-value時(shí),這是唯一可選擇的方法*/ 42 Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator(); 43 while (entries.hasNext()) { 44 Map.Entry<String, String> entry = entries.next(); 45 System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 46 } 47 /**擴(kuò)展 直接獲取map的所有value值*/ 48 for(String value:map.values()){ 49 System.out.println(value); 50 } 51 52 } 53 }List
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.List; 4 5 /** 6 * Created by robin on 2016/4/13. 7 * 關(guān)于list遍歷有三種辦法:①普通方法;②增強(qiáng)for循環(huán);③iterator 8 * 效率 ①>③>② 9 * 10 * ①內(nèi)部不鎖定, 效率最高, 但是當(dāng)寫(xiě)多線程時(shí)要考慮并發(fā)操作的問(wèn)題 11 * ②內(nèi)部實(shí)現(xiàn)是使用了③ 12 * ③執(zhí)行過(guò)程中會(huì)進(jìn)行數(shù)據(jù)鎖定, 性能稍差 13 * 14 * list遍歷刪除數(shù)據(jù)建議采用方法③,若采用方法①,請(qǐng)使用倒序遍歷刪除,詳情參考:ordinaryDelDesc; 15 * 不可使用②進(jìn)行遍歷刪除,會(huì)產(chǎn)生bug 16 * @author robin 17 */ 18 public class ListTest { 19 20 public static void main(String args[]){ 21 List<Student> stuList = initList(); 22 /** 23 * 普通遍歷方法 24 */ 25 for(int i= 0;i< stuList.size();i++){ 26 Student stuShow = stuList.get(i); 27 // System.out.println("stuId:"+stuShow.getStuId()+"----stuName:"+stuShow.getName()); 28 } 29 30 System.out.println("----普通遍歷方法----"); 31 32 /** 33 * 增強(qiáng)for循環(huán)遍歷 34 */ 35 for (Student stu:stuList){ 36 // System.out.println("stuId:"+stu.getStuId()+"----stuName:"+stu.getName()); 37 } 38 System.out.println("----增強(qiáng)for循環(huán)遍歷----"); 39 /** 40 * Iterator 遍歷 41 */ 42 Iterator<Student> iterator = stuList.iterator(); 43 while (iterator.hasNext()){ 44 Student stuShow = iterator.next(); 45 // System.out.println("stuId:"+stuShow.getStuId()+"----stuName:"+stuShow.getName()); 46 } 47 System.out.println("----Iterator循環(huán)遍歷----"); 48 49 //list中刪除元素方法 50 ordinaryDelAsc(initList()); 51 System.out.println("----普通遍歷正序刪除----"); 52 ordinaryDelDesc(initList()); 53 System.out.println("----普通遍歷倒序刪除----"); 54 // heighten(stuList); 55 iteratorDel(initList()); 56 System.out.println("----Iterator循環(huán)遍歷刪除----"); 57 } 58 59 public static List<Student> initList(){ 60 List<Student> stuList = new ArrayList<Student>(); 61 Student stuA = new Student("10001","路飛"); 62 stuList.add(stuA); 63 Student stuB = new Student("10002","索隆"); 64 stuList.add(stuB); 65 Student stuC = new Student("10003","山治"); 66 stuList.add(stuC); 67 Student stuD = new Student("10004","娜美"); 68 stuList.add(stuD); 69 Student stuE = new Student("10005","羅賓"); 70 stuList.add(stuE); 71 Student stuF = new Student("10006","烏索普--1號(hào)"); 72 stuList.add(stuF); 73 Student stuJ = new Student("10007","弗蘭奇"); 74 stuList.add(stuJ); 75 Student stuH = new Student("10006","烏索普--2號(hào)"); 76 stuList.add(stuH); 77 Student stuK = new Student("10008","喬巴"); 78 stuList.add(stuK); 79 Student stuI = new Student("10006","烏索普--3號(hào)"); 80 stuList.add(stuI); 81 Student stuM = new Student("10006","烏索普--4號(hào)"); 82 stuList.add(stuM); 83 Student stuL = new Student("10009","布魯克"); 84 stuList.add(stuL); 85 return stuList; 86 } 87 88 /** 89 * 普通遍歷方法 正序 刪除預(yù)期數(shù)據(jù) 90 * 這種方法存在bug:刪除元素時(shí)涉及到數(shù)組元素的移動(dòng),遍歷下標(biāo)n的元素。 91 * 當(dāng)前元素符合條件,執(zhí)行刪除操作,當(dāng)前n位置的元素被刪除,n+1位置的元素,移動(dòng)到n位置上。 92 * 搜索光標(biāo)卻還是n,并未n-1,下次循環(huán)搜索時(shí),會(huì)從下標(biāo)n+1開(kāi)始檢索。 93 * 此時(shí)n+1位置的元素,原本是n+2的元素,那么此次循環(huán)遍歷檢索就會(huì)遺漏n+1位置的元素檢查。 94 * 倘若n+1位置的元素符合刪除條件,那么程序就將出現(xiàn)bug。 95 * 因?yàn)榻ㄗh多用iterator進(jìn)行遍歷刪除,如果非得使用普通循環(huán)遍歷方法刪除元素,請(qǐng)采用倒序的辦法,詳見(jiàn)ordinaryDesc 96 * @param stuList 97 */ 98 public static void ordinaryDelAsc(List<Student> stuList){ 99 for(int i= 0;i< stuList.size();i++){ 100 Student stuShow = stuList.get(i); 101 if(stuShow.getStuId().equals("10006")){ 102 stuList.remove(stuShow); 103 } 104 } 105 System.out.println(listToString(stuList)); 106 } 107 /** 108 * 普通遍歷方法 倒序 刪除預(yù)期數(shù)據(jù) 109 * @param stuList 110 */ 111 public static void ordinaryDelDesc(List<Student> stuList){ 112 for(int i= stuList.size()-1;i>0 ;i--){ 113 Student stuShow = stuList.get(i); 114 if(stuShow.getStuId().equals("10006")){ 115 stuList.remove(stuShow); 116 } 117 } 118 System.out.println(listToString(stuList)); 119 } 120 121 /** 122 * 增強(qiáng)for循環(huán)遍歷刪除預(yù)期數(shù)據(jù) 123 * @param stuList 124 */ 125 public static void heighten(List<Student> stuList){ 126 for (Student stu:stuList){ 127 if(stu.getStuId().equals("10006")){ 128 stuList.remove(stu); 129 } 130 } 131 System.out.println(listToString(stuList)); 132 } 133 134 /** 135 * Iterator 循環(huán)遍歷刪除預(yù)期數(shù)據(jù) 136 * @param stuList 137 * @return 138 */ 139 public static void iteratorDel(List<Student> stuList){ 140 Iterator<Student> iterator = stuList.iterator(); 141 while (iterator.hasNext()){ 142 Student stuShow = iterator.next(); 143 if(stuShow.getStuId().equals("10006")){ 144 iterator.remove(); 145 } 146 } 147 System.out.println(listToString(stuList)); 148 } 149 150 private static String listToString(List<Student> list){ 151 StringBuilder sb = new StringBuilder(""); 152 for (Student s:list){ 153 sb.append("[stuId:"+s.getStuId()+"---name:"+s.getName()+"] \n"); 154 } 155 return sb.toString(); 156 } 157 158 159 } 160 161 class Student{ 162 163 public Student(String stuId,String name){ 164 this.stuId =stuId; 165 this.name = name; 166 } 167 168 private String stuId; 169 170 private String name; 171 172 public void setStuId(String stuId){ 173 this.stuId = stuId; 174 } 175 176 public String getStuId(){ 177 return this.stuId; 178 } 179 180 public void setName(String name){ 181 this.name = name; 182 } 183 184 public String getName(){ 185 return this.name; 186 } 187 }轉(zhuǎn)載于:https://www.cnblogs.com/robinjava77/p/5456085.html
總結(jié)
以上是生活随笔為你收集整理的map和list遍历基础的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CentOS 6.9下的Setup工具(
- 下一篇: 数组实用类:Arrays