概述
ArrayList、LinkedList、HashMap是Java中常用到的几种集合类型,遍历它们是时常遇到的情况。当然还有一些变态的时候,那就是在遍历的过程中动态增加或者删除其中的元素。
下面的例子就是可以实现动态遍历ArrayList、LinkedList、HashMap。
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 import java.util.ListIterator; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 9 /** 10 * @Description: 11 * @author Scott 12 * @date 2014年2月21日 下午8:33:40 13 */ 14 public class TraversalTest { 15 16 ArrayList<String> arrayList = new ArrayList<String>(); 17 LinkedList<String> linkedList = new LinkedList<String>(); 18 HashMap<String, String> map = new HashMap<String, String>(); 19 20 public TraversalTest() { 21 arrayList.add("1"); 22 arrayList.add("2"); 23 arrayList.add("3"); 24 arrayList.add("4"); 25 arrayList.add("5"); 26 27 linkedList.add("1"); 28 linkedList.add("2"); 29 linkedList.add("3"); 30 linkedList.add("4"); 31 linkedList.add("5"); 32 33 map.put("1", "1"); 34 map.put("2", "2"); 35 map.put("3", "3"); 36 map.put("4", "4"); 37 map.put("5", "5"); 38 39 System.out.println("Data has init over ..."); 40 } 41 42 /** 43 * ForEach Traversal. 44 * The Effective-Java recommended this traversal type. 45 * */ 46 public void arrayListTraversalNormal1() { 47 System.out.println("ArrayList ForEach Traversal."); 48 for(String str : arrayList){ 49 System.out.println(str); 50 } 51 } 52 53 /** 54 * Iterator Traversal. 55 * */ 56 public void arrayListTraversalNormal2() { 57 System.out.println("ArrayList Iterator Traversal."); 58 Iterator<String> itor = arrayList.iterator(); 59 while(itor.hasNext()){ 60 System.out.println(itor.next().toString()); 61 } 62 } 63 64 /** 65 * ForEach Traversal. 66 * The Effective-Java recommended this traversal type. 67 * */ 68 public void linkedListTraversalNormal1() { 69 System.out.println("LinkedList ForEach Traversal."); 70 for(String str : linkedList){ 71 System.out.println(str); 72 } 73 } 74 75 /** 76 * Iterator Traversal. 77 * */ 78 public void linkedListTraversalNormal2() { 79 System.out.println("LinkedList Iterator Traversal."); 80 Iterator<String> itor = linkedList.iterator(); 81 while(itor.hasNext()){ 82 System.out.println(itor.next().toString()); 83 } 84 } 85 86 public void mapTraversalNormal() { 87 System.out.println("HashMap Iterator Traversal."); 88 Iterator<Entry<String, String>> itor = map.entrySet().iterator(); 89 Entry<String, String> entry = null; 90 String key = null; 91 String value = null; 92 93 while(itor.hasNext()){ 94 entry = itor.next(); 95 key = entry.getKey(); 96 value = entry.getValue(); 97 98 System.out.println("key is " + key + ", value is " + value); 99 } 100 } 101 102 /** 103 * While traversing the arrayList, add '33' behind the '3' element. 104 * */ 105 public void arrayListTraversalDynamic1() { 106 ListIterator<String> itor = arrayList.listIterator(); 107 String str = null; 108 109 while (itor.hasNext()) { 110 str = itor.next().toString(); 111 if(str.equals("3")){ 112 itor.add("33"); 113 break; 114 } 115 } 116 } 117 118 /** 119 * While traversing the arrayList, remove the '3' element. 120 * */ 121 public void arrayListTraversalDynamic2() { 122 ListIterator<String> itor = arrayList.listIterator(); 123 String str = null; 124 125 while (itor.hasNext()) { 126 str = itor.next().toString(); 127 if(str.equals("3")){ 128 itor.remove(); 129 break; 130 } 131 } 132 } 133 134 /** 135 * While traversing the linkedList, add '33' behind the '3' element. 136 * */ 137 public void linkedListTraversalDynamic1() { 138 ListIterator<String> itor = linkedList.listIterator(); 139 String str = null; 140 141 while (itor.hasNext()) { 142 str = itor.next().toString(); 143 if(str.equals("3")){ 144 itor.add("33"); 145 break; 146 } 147 } 148 } 149 150 /** 151 * While traversing the linkedList, remove the '3' element. 152 * */ 153 public void linkedListTraversalDynamic2() { 154 ListIterator<String> itor = linkedList.listIterator(); 155 String str = null; 156 157 while (itor.hasNext()) { 158 str = itor.next().toString(); 159 if(str.equals("3")){ 160 itor.remove(); 161 break; 162 } 163 } 164 } 165 166 /** 167 * While traversing the arrayList, add '33' when we get the '3' element. 168 * */ 169 @SuppressWarnings("rawtypes") 170 public void mapTraversalDynamic1() { 171 LinkedList<Map.Entry<String, String>> tempList = new LinkedList<Map.Entry<String, String>>(); 172 tempList.addAll(map.entrySet()); 173 ListIterator<Map.Entry<String, String>> itor = tempList.listIterator(); 174 Map.Entry entry = null; 175 176 while (itor.hasNext()) { 177 entry = (Map.Entry) itor.next(); 178 Object key = entry.getKey(); 179 180 if (key.toString().equals("3")) { 181 map.put("33", "33"); 182 } 183 } 184 } 185 186 /** 187 * While traversing the hashMap, remove the '3' element. 188 * */ 189 @SuppressWarnings("rawtypes") 190 public void mapTraversalDynamic2() { 191 LinkedList<Map.Entry<String, String>> tempList = new LinkedList<Map.Entry<String, String>>(); 192 tempList.addAll(map.entrySet()); 193 ListIterator<Map.Entry<String, String>> itor = tempList.listIterator(); 194 Map.Entry entry = null; 195 196 while (itor.hasNext()) { 197 entry = (Map.Entry) itor.next(); 198 Object key = entry.getKey(); 199 200 if(key.toString().equals("3")){ 201 map.remove("3"); 202 break; 203 } 204 } 205 } 206 207 public static void main(String[] args) { 208 TraversalTest test = new TraversalTest(); 209 210 test.arrayListTraversalNormal1(); 211 test.arrayListTraversalNormal2(); 212 213 System.out.println("While traversing the arrayList, add '33' behind the '3' element. The result is:"); 214 test.arrayListTraversalDynamic1(); 215 test.arrayListTraversalNormal1(); 216 217 System.out.println("While traversing the arrayList, remove the '3' element. The result is:"); 218 test.arrayListTraversalDynamic2(); 219 test.arrayListTraversalNormal1(); 220 221 System.out.println("-----------------------------------"); 222 223 test.linkedListTraversalNormal1(); 224 test.linkedListTraversalNormal2(); 225 226 System.out.println("While traversing the linkedList, add '33' behind the '3' element. The result is:"); 227 test.linkedListTraversalDynamic1(); 228 test.linkedListTraversalNormal1(); 229 230 System.out.println("While traversing the linkedList, remove the '3' element. The result is:"); 231 test.linkedListTraversalDynamic2(); 232 test.linkedListTraversalNormal1(); 233 234 System.out.println("-----------------------------------"); 235 test.mapTraversalNormal(); 236 237 System.out.println("While traversing the hashMap, add '33' when we get the '3' element. The result is:"); 238 test.mapTraversalDynamic1(); 239 test.mapTraversalNormal(); 240 241 System.out.println("While traversing the hashMap, remove the '3' element. The result is:"); 242 test.mapTraversalDynamic2(); 243 test.mapTraversalNormal(); 244 } 245 246 }
代码是最好的说明,运行结果如下:
Data has init over ... ArrayList ForEach Traversal. 1 2 3 4 5 ArrayList Iterator Traversal. 1 2 3 4 5 While traversing the arrayList, add '33' behind the '3' element. The result is: ArrayList ForEach Traversal. 1 2 3 33 4 5 While traversing the arrayList, remove the '3' element. The result is: ArrayList ForEach Traversal. 1 2 33 4 5 ----------------------------------- LinkedList ForEach Traversal. 1 2 3 4 5 LinkedList Iterator Traversal. 1 2 3 4 5 While traversing the linkedList, add '33' behind the '3' element. The result is: LinkedList ForEach Traversal. 1 2 3 33 4 5 While traversing the linkedList, remove the '3' element. The result is: LinkedList ForEach Traversal. 1 2 33 4 5 ----------------------------------- HashMap Iterator Traversal. key is 3, value is 3 key is 2, value is 2 key is 1, value is 1 key is 5, value is 5 key is 4, value is 4 While traversing the hashMap, add '33' when we get the '3' element. The result is: HashMap Iterator Traversal. key is 3, value is 3 key is 2, value is 2 key is 1, value is 1 key is 5, value is 5 key is 4, value is 4 key is 33, value is 33 While traversing the hashMap, remove the '3' element. The result is: HashMap Iterator Traversal. key is 2, value is 2 key is 1, value is 1 key is 5, value is 5 key is 4, value is 4 key is 33, value is 33
最后
以上就是狂野橘子为你收集整理的ArrayList、LinkedList、HashMap的遍历及遍历过程中增删元素的全部内容,希望文章能够帮你解决ArrayList、LinkedList、HashMap的遍历及遍历过程中增删元素所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复