This article describes the Java collection set, List, map traversal method, share to everyone for your reference.
Here's how:
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576 |
package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.junit.Test; public class TestCollection { public static void print(Collection<? extends Object> c){ Iterator<? extends Object> it = c.iterator(); while (it.hasNext()) { Object object = (Object) it.next(); System.out.println(object); } } @Test public void demo1(){ Set<String> set = new HashSet<String>(); set.add("AAA"); set.add("BBB"); set.add("CCC"); print(set); //Set的第一种遍历方式:利用Iterator Iterator<String> it1 = set.iterator(); for (String ss : set) { System.out.println(ss); } //Set的第一种遍历方式:利用foreach for (String sss : set) { System.out.println(sss); } List<String> list = new ArrayList<String>(); list.add("DDDDD"); list.add("EEEEE"); list.add("FFFFF"); print(list); //List的第一种遍历方式:因为list有顺序,利用size()和get()方法获取 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } //List的第二种遍历方式:利用Iterator Iterator<String> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } //List的第三种遍历方式:利用foreach for (String s2 : list) { System.out.println(s2); } Map<String,String> map = new TreeMap<String, String>(); map.put("Jerry", "10000"); map.put("shellway", "20000"); map.put("Kizi", "30000"); print(map.entrySet()); //Map的第一种遍历方式:先获得key,再获得值value Set<String> sett = map.keySet(); for (String s : sett) { System.out.println(s+":"+map.get(s)); } //Map的第二种遍历方式:获得键值对 for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey()+" : "+entry.getValue()); } } } |
Use generics here
The difference between for loop traversal and iterator traversal
1. The effect of first enhancing the For loop and iterator traversal is the same, saying
The inside of the enhanced for loop is called Iteratoer implementation, but there are some drawbacks to enhancing the For loop, such as the inability to dynamically delete collection content in the enhanced loop. Cannot get subscript and so on.
2.ArrayList because of the use of arrays implementation, so the subscript is clear, it is best to use the normal loop.
3. For LinkedList because it takes an element to get back from the beginning, it is recommended to use an enhanced for loop, which is iterator.
The following example proves that the 1th
①public static void RemoveEvensVer2 (list<integer> lst)
{
For (Integer x:lst)
if (x% 2 = = 0)
lst.remove (x);
System.out.println (LST);
}
②public static void RemoveEvensVer3 (list<integer> lst)
{
iterator<integer> ITR = Lst.iterator ();
While (Itr.hasnext ())
if (Itr.next ()% 2 = = 0)
Itr.remove ();
System.out.println (LST);
}
① throws an exception at run time, ② normal
cause Analysis: Because the enhanced for loop interior is called iterator implementation, the list is converted to an iterator at the time of traversal, and when the iterator is created, if the list is modified from the structure, except through the Remove, add method of the iterator itself, Any other modification at any time in any way, the iterator throws a Concurrentmodificationexception exception.
Java set traversal method for set, List, map