Java:集合,Array、Collection(List/Set/Queue)、Map的遍历,比如:ArrayList,LinkedList,HashSet,HashMap
2013-03-08 20:39
1111 查看
1. 概述
1.1 Set接口
存入Set的每个元素都必须是唯一的,Set接口不保证维护元素的次序;HashSet类: 为快速查找设计的Set,存入HashSet的对象必须定义hashCode(),它不保证集合的迭代顺序;
LinkedHashSet类: 具有HashSet的查询速度,且内部使用链表维护元素的顺序(插入的次序)。
1.2 List接口
List按对象进入的顺序保存对象,不做排序等操作;ArrayList类:由数组实现的List,允许对元素进行快速随机访问,但是向List中间插入与移除元素的速度很慢;
LinkedList类: 对顺序访问进行了优化,向List中间插入与删除的开销并不大,随机访问则相对较慢。
1.3 Queue接口
Queue用于模拟队列这种数据结构,实现“FIFO”等数据结构。通常,队列不允许随机访问队列中的元素。ArrayDeque类:为Queue子接口Deque的实现类,数组方式实现。
LinkedList类:是List接口的实现类,同时它也实现了Deque接口(Queue子接口)。因此它也可以当做一个双端队列来用,也可以当作“栈”来使用。
1.4 Map接口
添加、删除操作put/remove/putAll/clear查询操作get/containsKey/containsValue/size/isEmpty
视图操作keySet/values/entrySet
Map.Entry接口(Map的entrySet()方法返回一个实现Map.Entry接口的对象集合) getKey/getValue/setValue
下面是一段代码用来遍历各种数组、集合、映射的。更多内容请参考:
Java:集合,Collection接口框架图
Java:集合,Map接口框架图
Java:concurrent包下面的Collection接口框架图( CopyOnWriteArraySet, CopyOnWriteArrayList,ConcurrentLinkedQueue,BlockingQueue)
Java:concurrent包下面的Map接口框架图(ConcurrentMap接口、ConcurrentHashMap实现类)
2. 示范代码
package com.clzhang.sample.collections; import java.util.*; import org.junit.Test; import org.junit.Before; import org.junit.After; /** * 测试遍历Array/Set/List/Queue/Map的类 * @author acer * */ public class CollectionMapTest { 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 i = 0; i < TOTAL_COUNT; i++) { String key = "user" + i; String value = "pass" + i; array[i] = 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 i = 0; i < array.length; i++) { String str = array[i]; } // foreach遍历:JDK1.5之后的方法 for (String str : array) { // 直接用了 } System.out.println("遍历array各种方式总耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); } @SuppressWarnings("unused") @Test public void testSet() { long startTime = Calendar.getInstance().getTimeInMillis(); // 迭代遍历 Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); } // 迭代遍历的for方式;或者相对while循环而言,能够避免变量名错误 for (Iterator<String> iter = set.iterator(); iter.hasNext();) { String str = iter.next().toString(); } // foreach遍历:JDK1.5之后的方法 for (String str : set) { // 直接用了,注意它是无序的 } System.out.println("遍历set各种方式总耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); } @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() { long startTime = 0; // Entry方式的迭代遍历(Entry方式遍历时间短) startTime = Calendar.getInstance().getTimeInMillis(); Iterator<Map.Entry<String, String>> iter1 = map.entrySet().iterator(); while(iter1.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) iter1.next(); String key = (String)entry.getKey(); String value = (String)entry.getValue(); } System.out.println("Map之Entry方式遍历耗时:" + (Calendar.getInstance().getTimeInMillis() - startTime)); // 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); } } }
输出:
遍历set各种方式总耗时:31
遍历list各种方式总耗时:47
遍历queue各种方式总耗时:47
遍历array各种方式总耗时:0
Map之Entry方式遍历耗时:15
Map之keySet方式遍历耗时:31
3. 总结
1.遍历数组速度最快;2.Map在Entry方式与keySet方式遍历时,耗时有着非常大的区别,原因应该是keySet方式遍历时再次调用map.get(key)方法造成的,当数据量大时,需要特别考虑。
相关文章推荐
- HashMap和HashSet的使用,区别。集合,Array、Collection(List/Set/Queue)、Map
- HashMap和HashSet的使用,区别。集合,Array、Collection(List/Set/Queue)、Map
- java中Collection/Collections;List/Set/Map;ArrayList/Vector/LinkedList;HashSet/HashMap/TreeSet/TreeMap
- Lesson_for_java_day13--java中的集合——Collection、List、ArrayList、LinkedList、Set、HashSet、TreeSet
- Java核心API -- 6(Collection集合List、Set、ArrayList、HashSet)
- List、Map、Set的理解(LinkedList和ArrayList、Vector和ArrayList、HashMap和HashTable和HashSet区别与使用)
- 集合类(常见的集合类:Collection、List、Set、ArrayList、linkedList、Vector、HashSet、TreeSet)
- Java对集合的遍历 List ArryList HashMap LinkedMap JSON 数组等
- Java:concurrent包下面的Collection接口框架图( CopyOnWriteArraySet, CopyOnWriteArrayList,ConcurrentLinkedQueue,BlockingQueue)
- JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
- Java集合之ArrayList,LinkedList,HashMap,HashSet
- java collections 集合基础学习 ArrayList,LinkedList, HashSet, TreeSet, HashMap, Stack,Queue,PriorityQueue
- java中集合 list Map arraylist linkedlist hashmap
- 浅析Java中的集合包(ArrayList,LinkedList,Vector, Stack,HashSet,TreeSet,HashMap,TreeMap)
- List、Map、Set的理解(LinkedList和ArrayList、Vector和ArrayList、HashMap和HashTable和HashSet区别与使用)
- Java集合:List、Set和Map的区别,ArrayList和LinkedList有何区别..........
- java集合类,List和Set比较,各自的子类比较(ArrayList,Vector,LinkedList;HashSet,TreeSet),Map集合比较
- Map集合,List集合,Array数组,Iterator迭代,Collection集合,Set集合的多种遍历方式
- java中集合 list Map arraylist linkedlist hashmap ——学java,零基础不怕,不只要理论,更要实践+项目,<a href=http://www.bjweixin.com>太原维信科技提供 </a>
- java中集合 list Map arraylist linkedlist hashmap —...