Java 容器 & 泛型:1、认识容器

Writer:BYSocket(泥沙砖瓦浆木匠)java

微博:BYSocketweb

豆瓣:BYSocket数组

容器是Java语言学习中重要的一部分。泥瓦匠个人感受是刚开始挺难学的,但等你熟悉它,接触多了,也就“瓜熟蒂落”地知道了。Java的容器类主要由两个接口派生而出:Collection和Map安全

 

1、Collection vs Collections

首先,Collection 和 Collections 是两个不一样的概念。之因此放在一块儿,是为了更好的比较。Collection是容器层次结构中根接口。而Collections是一个提供一些处理容器类静态方法的类。数据结构

CollectionVsCollections

JDK不提供Collection接口的具体实现,而是提供了更加具体的子接口(如Set和List)实现框架

那Collection接口存在有何做用呢?存在便是道理。异步

缘由在于:全部容器的实现类(如ArrayList实现了List接口,HashSet实现了Set接口)提供了两个‘标准’的构造函数来实现:一、一个无参的构造方法(void)二、一个带有Collection类型单参数构造方法,用于建立一个具备其参数相同元素新的Collection及其实现类等。实际上:由于全部通用的容器类听从Collection接口,用第二种构造方法是容许容器之间相互的复制。socket

 

2、Collection的类层次结构

下面的图是关于Collection的类的层次结构。函数

java-collection-hierarchy

 

Set性能

一个不包括重复元素(包括可变对象)的Collection,是一种无序的集合。Set不包含满 a.equals(b) 的元素对a和b,而且最多有一个null。实现Set的接口有:EnumSet、HashSet、TreeSet等。下图是Set的JDK源码UML图。

Set

 

List

一个有序的Collection(也称序列),元素能够重复。确切的讲,列表一般容许知足 e1.equals(e2) 的元素对 e1  e2,而且若是列表自己容许 null 元素的话,一般它们容许多个 null 元素。实现List的有:ArrayList、LinkedList、Vector、Stack等。下图是List的JDK源码UML图。

list

 

Queue

一种队列则是双端队列,支持在头、尾两端插入和移除元素,主要包括:ArrayDeque、LinkedBlockingDeque、LinkedList。另外一种是阻塞式队列,队列满了之后再插入元素则会抛出异常,主要包括ArrayBlockQueue、PriorityBlockingQueue、LinkedBlockingQueue。虽然接口并未定义阻塞方法,可是实现类扩展了此接口。下图是Queue的JDK源码UML图。

queue

 

3、Map的类的层次结构

下面的图是Map的层次结构图MapClassHierarchy-600x354

Map:

是一个键值对的集合。也就是说,一个映射不能包含重复的键,每一个键最多映射到一个值。该接口取代了Dictionary抽象类。实现map的有:HashMap、TreeMap、HashTable、Properties、EnumMap。下图是Map的JDK源码UML图。

map

 

4、容器接口的小结

collection-summary

 

5、代码样例

对HashMap,HashSet,LinkedList,ArrayList,TreeMap,TreeSet的例子以下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
 
@SuppressWarnings("unchecked")
public class CollectionAll
{
     
     public static void main(String[] args)
     {
         printLists();
         
         printSets();
         
         printMaps();
     }
 
     private static void printLists()
     {
         List< String > a1 = new ArrayList< String >();
         a1.add("List");
         a1.add("Set");
         a1.add("Queue");
         a1.add("Map");
         System.out.println("ArrayList Elements:");
         System.out.print("\t" + a1 + "\n");
         
         List< String > l1 = new LinkedList< String >();
         l1.add("List");
         l1.add("Set");
         l1.add("Queue");
         l1.add("Map");
         System.out.println("LinkedList Elements:");
         System.out.print("\t" + l1 + "\n");
     }
     @SuppressWarnings("rawtypes")
     private static void printSets()
     {
         Set h1 = new HashSet< String >();
         h1.add("List");
         h1.add("Set");
         h1.add("Queue");
         h1.add("Map");
         System.out.println("HashSet Elements:");
         System.out.print("\t" + h1 + "\n");
         
         Set t1 = new TreeSet< String >();
         t1.add("List");
         t1.add("Set");
         t1.add("Queue");
         t1.add("Map");
         System.out.println("TreeSet Elements:");
         System.out.print("\t" + t1 + "\n");
     }
     
     private static void printMaps()
     {
         Map< String , String> h1 = new HashMap< String , String>();
         h1.put("List", "ArrayList");
         h1.put("Set", "HashSet");
         h1.put("Queue", "PriorityQueue");
         h1.put("Map", "HashMap");
         System.out.println("HashMap Elements:");
         System.out.print("\t" + h1 + "\n");
         
         Map< String , String> t1 = new TreeMap< String ,String>();
         t1.put("List", "ArrayList");
         t1.put("Set", "HashSet");
         t1.put("Queue", "PriorityQueue");
         t1.put("Map", "HashMap");
         System.out.println("TreeMap Elements:");
         System.out.print("\t" + t1 + "\n");
         
     }
}

控制台打印以下:

?

1
2
3
4
5
6
7
8
9
10
11
12
ArrayList Elements:
     [List, Set, Queue, Map]
LinkedList Elements:
     [List, Set, Queue, Map]
HashSet Elements:
     [Map, Queue, Set, List]
TreeSet Elements:
     [List, Map, Queue, Set]
HashMap Elements:
     {Map=HashMap, Queue=PriorityQueue, Set=HashSet, List=ArrayList}
TreeMap Elements:
     {List=ArrayList, Map=HashMap, Queue=PriorityQueue, Set=HashSet}

 

6、总结

异同点

      出处:http://blog.csdn.net/softwave/article/details/4166598

Vector和ArrayList

      1,vector是线程同步的,因此它也是线程安全的,而arraylist是线程异步的,是不安全的。若是不考虑到线程的安全因素,通常用arraylist效率比较高。
      2,若是集合中的元素的数目大于目前集合数组的长度时,vector增加率为目前数组长度的100%,而arraylist增加率为目前数组长度的50%.如过在集合中使用数据量比较大的数据,用vector有必定的优点。
      3,若是查找一个指定位置的数据,vector和arraylist使用的时间是相同的,都是0(1),这个时候使用vector和arraylist均可以。而若是移动一个指定位置的数据花费的时间为0(n-i)n为总长度,这个时候就应该考虑到使用linklist,由于它移动一个指定位置的数据所花费的时间为0(1),而查询一个指定位置的数据时花费的时间为0(i)。

      ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都容许直接序号索引元素,可是插入数据要设计到数组元素移动等内存操做,因此索引数据快插入数据慢,Vector因为使用了synchronized方法(线程安全)因此性能上比ArrayList要差,LinkedList使用双向链表实现存储,按序号索引数据须要进行向前或向后遍历,可是插入数据时只须要记录本项的先后项便可,因此插入数度较快!

Aarraylist和Linkedlist

      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
      2.对于随机访问get和set,ArrayList以为优于LinkedList,由于LinkedList要移动指针。
      3.对于新增和删除操做add和remove,LinedList比较占优点,由于ArrayList要移动数据。
      这一点要看实际状况的。若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList。但如果批量随机的插入删除数据,LinkedList的速度大大优于ArrayList. 由于ArrayList每插入一条数据,要移动插入点及以后的全部数据。

HashMap与TreeMap

      一、HashMap经过hashcode对其内容进行快速查找,而TreeMap中全部的元素都保持着某种固定的顺序,若是你须要获得一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不固定的)。HashMap中元素的排列顺序是不固定的)。

      二、  HashMap经过hashcode对其内容进行快速查找,而TreeMap中全部的元素都保持着某种固定的顺序,若是你须要获得一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不固定的)。集合框架”提供两种常规的Map实现:HashMap和TreeMap (TreeMap实现SortedMap接口)。

      三、在Map 中插入、删除和定位元素,HashMap 是最好的选择。但若是您要按天然顺序或自定义顺序遍历键,那么TreeMap会更好。使用HashMap要求添加的键类明肯定义了hashCode()和 equals()的实现。 这个TreeMap没有调优选项,由于该树总处于平衡状态。

hashtable与hashmap

      一、历史缘由:Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现 。

      二、同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步的 。

      三、值:只有HashMap可让你将空值做为一个表的条目的key或value 。

collection

相关文章
相关标签/搜索