<div class="iteye-blog-content-contain" style="font-size: 14px"></div>
/** * @author xiongqingyuan * @version * @since * @see http://www.cnblogs.com/nayitian/archive/2013/03/08/2950730.html * @see http://kb.cnblogs.com/page/517741/ * Java:集合,Array、Collection(List/Set/Queue)、Map的遍历,比如:ArrayList,LinkedList,HashSet,HashMap * * 1) Set 接口: * a): 存入 Set 的每个元素都必须是唯一的, Set 接口不保证维护元素的次序; * b): HashSet 类,为快速查找设计的 Set, 存入HashSet 的对象必须定义 hashCode(), 它不保证集合的迭代顺序; * c): LinkedHashSet 类, 具有HashSet 的查询速度,且内部使用链表维护元素的顺序(插入的顺序) * * 2) List 接口: * a): List 按对象进入的顺序保存对象,不做排序等操作; * b): ArrayList类: 由数组实现的List, 允许对元素进行快速随机访问,但是向List 中间插入与移除元素的速度很慢; * c):LinkedList类: 对顺序访问进行了优化,向List 中间插入与删除的开销并不大,随机访问则相对较慢; * * 3) Queue 接口: * a): Queue 用于模拟队列这种数据结构,实现"FIFO"->first in first out等数据结构,通常,队列不允许随机访问队列中的元素; * b): ArrayDeque类: 为Queue 子接口Deque 的实现类,数组方式实现; * c): LinkedList类: 是List接口的实现类,同时它也实现了Deque 接口(Queue子接口),因此它也可以当做一个双端队列来用,也可以 * 当做"栈"来使用; * * 4) Map 接口: * a): 添加,删除操作put/remove/putAll/clear * b): 查询操作 get/containsKey/containsValue/size/isEmpty * c): 视图操作 keySet/values/entrySet * d): Map.Entry接口(Map 的entrySet()方法返回的一个实现Map.Entry接口的对象集合) getKey/getValue/setValue; * * 特别说明下遍历 Map 集合的问题:<Map.Entry<String, String>> 是首选,遍历keySet 是很不合理的: * 1.遍历数组速度最快 * 2.Map在Entry方式与keySet方式遍历时,耗时有着非常大的区别,原因应该是keySet方式遍历时 * 再次调用map.get(key)方法造成的,当数据量大时,需要特别考虑 */ public class CollectionReview { private static final int TOTAL_COUNT = 100000; private String[] array; private Set<String> set; private List<String> list; private Queue<String> queue; private Map<String, String> map; @Before public void init() { array = new String[TOTAL_COUNT]; set = new HashSet<String>(); list = new ArrayList<String>(); queue = new LinkedList<String>(); map = new HashMap<String, String>(); for (int collectionCounter = 0; collectionCounter < TOTAL_COUNT; collectionCounter++) { String key = "user" + collectionCounter; String value = "pass" + collectionCounter; array[collectionCounter] = key; set.add(key); list.add(key); queue.add(key); map.put(key, value); } } @After public void destory() { } @SuppressWarnings("unused") @Test public void testArray() { long startTime = Calendar.getInstance().getTimeInMillis(); // 常规的遍历 for (int arrayCounter = 0, arrayLen = array.length; arrayCounter < arrayLen; arrayCounter++) { String str = array[arrayCounter]; } // foreach JDK 1.5 for (String str: array) { } } @SuppressWarnings("unused") @Test public void testSet() { // 迭代遍历 Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); } // 推荐的遍历方法,迭代遍历的for 方法,或者相对while循环而言,能够避免变量名错误 Map<String, Object> voMap = new HashMap<String, Object>(); Object value = null; String columnName; for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); voMap.put(columnName, value)) { columnName = (String)iterator.next(); } // 迭代遍历的for方式;或者相对while循环而言,能够避免变量名错误 for (Iterator<String> iter = set.iterator(); iter.hasNext();) { String str = iter.next().toString(); } // foreach遍历:JDK1.5之后的方法 for (String str : set) { // 直接用了,注意它是无序的 } } @SuppressWarnings("unused") @Test public void testList() { long startTime = Calendar.getInstance().getTimeInMillis(); // 老款遍历的方法 for (int i = 0; i < list.size(); i++) { String str = list.get(i); } // 迭代遍历 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String str = iterator.next(); } // 迭代遍历的for方式,相对while循环而言,能够避免变量名错误 for (Iterator<String> iter = list.iterator(); iter.hasNext();) { String str = iter.next().toString(); } // foreach遍历:JDK1.5之后的方法 for (String str : list) { // 直接用了 } System.out.println("遍历list各种方式总耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); } @SuppressWarnings("unused") @Test public void testQueue() { long startTime = Calendar.getInstance().getTimeInMillis(); // 老款遍历的方法 for (int i = 0; i < queue.size(); i++) { String str = list.get(i); } // 迭代遍历 Iterator<String> iterator = queue.iterator(); while (iterator.hasNext()) { String str = iterator.next(); } // 迭代遍历的for方式;或者相对while循环而言,能够避免变量名错误 for (Iterator<String> iter = queue.iterator(); iter.hasNext();) { String str = iter.next().toString(); } // foreach遍历:JDK1.5之后的方法 for (String str : queue) { // 直接用了 } System.out.println("遍历queue各种方式总耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); } @SuppressWarnings("unused") @Test public void testMap() { // Entry 方式的迭代遍历,<Entry 方式遍历的时间短> long startTime = Calendar.getInstance().getTimeInMillis(); Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); String key = entry.getKey(); String value = entry.getValue(); } System.out.println("Map之Entry方式遍历耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); // Entry 方式的foreach 方法,这个是遍历Map 集合的首选,速度快,低碳环保无污染 for (Map.Entry<String, String> entry: map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); } // 不推荐使用的, 最好不使用的遍历方法 // Entry方式的foreach遍历,简单多了是不是,速度还贼快 for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); } // keySet方式的迭代遍历(keySet方式遍历耗时多) startTime = Calendar.getInstance().getTimeInMillis(); Iterator<String> iter2 = map.keySet().iterator(); while (iter2.hasNext()) { String key = (String)iter2.next(); String value = (String)map.get(key); } System.out.println("Map之keySet方式遍历耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); // keySet方式的foreach遍历,更简单,但速度慢 for(String key: map.keySet()) { String value = (String)map.get(key); } } }