java collection class summary

source: http://www.cnblogs.com/leeplogs/p/5891861.html


1. Collections and Arrays

An array (which can store basic data types ) is a container used to store objects, but the length of the array is fixed , which is not suitable for use when the number of objects is unknown.

Collections (which can only store objects , and the types of objects can be different) are of variable length and can be used in most cases.

2. Hierarchical relationship

As shown in the figure: In the figure, the solid line border is the implementation class, the polyline border is the abstract class, and the dotted border is the interface

The Collection interface is the root interface of the collection class, and there is no direct implementation class for this interface provided in Java. But let it be inherited to produce two interfaces, namely Set and List . A Set cannot contain duplicate elements. List is an ordered collection that can contain repeated elements, providing access by index .

Map is another interface in the Java.util package. It has nothing to do with the Collection interface and is independent of each other, but they are all part of the collection class. Map contains key-value pairs. Map cannot contain duplicate keys, but can contain the same value .

Iterator , all collection classes, implement the Iterator interface, which is an interface for traversing elements in a collection. It mainly includes the following three methods:
1. HasNext() Whether there is a next element.
2.next() returns the next element.
3.remove() removes the current element.

3. Introduction to several important interfaces and classes

1. List (ordered and repeatable)
The objects stored in the List are ordered and repeatable. The List focuses on the index. It has a series of methods related to the index, and the query speed is fast. Because when inserting or deleting data into the list set, it will be accompanied by the movement of the following data, so the speed of inserting and deleting data is slow.

2. Set (unordered, non-repeatable)
The objects stored in the Set are disordered and cannot be repeated. The objects in the set are not sorted in a specific way, but simply add the objects to the set.

3. Map (key-value pairs, unique keys, and non-unique values)
Map sets store key-value pairs, keys cannot be repeated, and values ​​can be repeated. The value is obtained according to the key. When traversing the map collection, the set collection of the key is obtained first, and the set collection is traversed to obtain the corresponding value.

The comparison is as follows:

 

 

Is it in order

Whether to allow element repetition

Collection

 

 

List

Yes

Yes

Set

AbstractSet

no

no

 

HashSet

 

TreeSet

yes (with binary sort tree)

Map

AbstractMap

no

Use key-value to map and store data, the key must be unique, and the value can be repeated

 

HashMap

 

TreeMap

yes (with binary sort tree)

 

4. Traversal

 The following four common output methods are provided in the class set:

1) Iterator : Iterative output is the most used output method.

2) ListIterator : It is a sub-interface of Iterator, which is specially used to output the contents of List.

3) foreach output: a new function provided after JDK1.5, which can output an array or a collection.

4) for loop

The code example is as follows:

 for的形式:forint i=0;i<arr.size();i++{...}

 foreach的形式: forint iarr{...}

 iterator的形式:
Iterator it = arr.iterator();
while(it.hasNext()){ object o =it.next(); ...}

五、ArrayList和LinkedList

ArrayList和LinkedList在用法上没有区别,但是在功能上还是有区别的。LinkedList经常用在增删操作较多而查询操作很少的情况下,ArrayList则相反。

六、Map集合

实现类:HashMap、Hashtable、LinkedHashMap和TreeMap

HashMap 

HashMap是最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。因为键对象不可以重复,所以HashMap最多只允许一条记录的键为Null,允许多条记录的值为Null,是非同步的

Hashtable

Hashtable与HashMap类似,是HashMap的线程安全版,它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢,它继承自Dictionary类,不同的是它不允许记录的键或者值为null,同时效率较低。

ConcurrentHashMap

线程安全,并且锁分离。ConcurrentHashMap内部使用段(Segment)来表示这些不同的部分,每个段其实就是一个小的hash table,它们有自己的锁。只要多个修改操作发生在不同的段上,它们就可以并发进行。

LinkedHashMap

LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的,在遍历的时候会比HashMap慢,有HashMap的全部特性。

TreeMap

TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序(自然顺序),也可以指定排序的比较器,当用Iterator遍历TreeMap时,得到的记录是排过序的。不允许key值为空,非同步的;

map的遍历

第一种:KeySet()
将Map中所有的键存入到set集合中。因为set具备迭代器。所有可以迭代方式取出所有的键,再根据get方法。获取每一个键对应的值。 keySet():迭代后只能通过get()取key 。
取到的结果会乱序,是因为取得数据行主键的时候,使用了HashMap.keySet()方法,而这个方法返回的Set结果,里面的数据是乱序排放的。
典型用法如下:
Map map = new HashMap();
map.put("key1","lisi1");
map.put("key2","lisi2");
map.put("key3","lisi3");
map.put("key4","lisi4");  
//先获取map集合的所有键的set集合,keyset()
Iterator it = map.keySet().iterator();
 //获取迭代器
while(it.hasNext()){
Object key = it.next();
System.out.println(map.get(key));
}

第二种:entrySet()
Set<Map.Entry<K,V>> entrySet() //返回此映射中包含的映射关系的 Set 视图。(一个关系就是一个键-值对),就是把(key-value)作为一个整体一对一对地存放到Set集合当中的。Map.Entry表示映射关系。entrySet():迭代后可以e.getKey(),e.getValue()两种方法来取key和value。返回的是Entry接口。
典型用法如下:
Map map = new HashMap();
map.put("key1","lisi1");
map.put("key2","lisi2");
map.put("key3","lisi3");
map.put("key4","lisi4");
//将map集合中的映射关系取出,存入到set集合
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Entry e =(Entry) it.next();
System.out.println("键"+e.getKey () + "的值为" + e.getValue());
}
推荐使用第二种方式,即entrySet()方法,效率较高
对于keySet其实是遍历了2次,一次是转为iterator,一次就是从HashMap中取出key所对于的value。而entryset只是遍历了第一次,它把key和value都放到了entry中,所以快了。两种遍历的遍历时间相差还是很明显的。

七、主要实现类区别小结

Vector和ArrayList
1,vector是线程同步的,所以它也是线程安全的,而arraylist是线程异步的,是不安全的。如果不考虑到线程的安全因素,一般用arraylist效率比较高。
2,如果集合中的元素的数目大于目前集合数组的长度时,vector增长率为目前数组长度的100%,而arraylist增长率为目前数组长度的50%。如果在集合中使用数据量比较大的数据,用vector有一定的优势。
3,如果查找一个指定位置的数据,vector和arraylist使用的时间是相同的,如果频繁的访问数据,这个时候使用vector和arraylist都可以。而如果移动一个指定位置会导致后面的元素都发生移动,这个时候就应该考虑到使用linklist,因为它移动一个指定位置的数据时其它元素不移动。
ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要涉及到数组元素移动等内存操作,所以索引数据快,插入数据慢,Vector由于使用了synchronized方法(线程安全)所以性能上比ArrayList要差,LinkedList使用双向链表实现存储,按序号索引数据需要进行向前或向后遍历,但是插入数据时只需要记录本项的前后项即可,所以插入数度较快。

arraylist和linkedlist
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。 这一点要看实际情况的。若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList。但若是批量随机的插入删除数据,LinkedList的速度大大优于ArrayList. 因为ArrayList每插入一条数据,要移动插入点及之后的所有数据。

HashMap与TreeMap
1、 HashMap通过hashcode对其内容进行快速查找,而TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不固定的)。
2、在Map 中插入、删除和定位元素,HashMap是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。使用HashMap要求添加的键类明确定义了hashCode()和 equals()的实现。
两个map中的元素一样,但顺序不一样,导致hashCode()不一样。
同样做测试:
在HashMap中,同样的值的map,顺序不同,equals时,false;
而在treeMap中,同样的值的map,顺序不同,equals时,true,说明,treeMap在equals()时是整理了顺序了的。

HashTable与HashMap
1、同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步的。
2、HashMap允许存在一个为null的key,多个为null的value 。
3、hashtable的key和value都不允许为null。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324459205&siteId=291194637