根据不同博客整理,还请原作者见谅...
本人一般不原创博客,怕有误人子弟之嫌,此篇只是整理之果,仅供参考....
在Android开发中我们经常需要对数据进行分类和操作,对于轻量级的数据存储我们可能不需要动用SQLite或效率以及类库不完善的XML,由于 SharedPreferences不具备数据枚举方法,如果仅仅是一个String或Int数组可以通过一个标记分割设计外,我们还是主要来看看 Android或者说Java提供的基础数据类型辅助类ArrayList LinkedList Set HashMap的介绍。
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
|-HashSet
|-TreeSet
Map
├Hashtable
├HashMap
└WeakHashMap
1: Collection接口
public class Collections extends Object
Collections
contains static methods which operate on Collection
classes.
Collection是最基本的集合接口,一个Collection代表一组 Object,即Collection的元素(Elements)。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。 Java SDK不提供直接继承自Collection的类,Java SDK提供的类都是继承自Collection的“子接口”如List和Set。所有实现Collection接口的类都必须提供两个标准的构造函数:无参数的构造函数用于创建一个空的Collection,有一个 Collection参数的构造函数用于创建一个新的Collection,这个新的Collection与传入的Collection有相同的元素。后 一个构造函数允许用户复制一个Collection。如何遍历Collection中的每一个元素?不论Collection的实际类型如何,它都支持一个iterator()的方法,该方法返回一个迭代子,使用该迭代子即可逐一访问Collection中每一个元素。典型的用法如下:
Iterator it = collection.iterator(); // 获得一个迭代子
while(it.hasNext()) {
Object obj = it.next(); // 得到下一个元素
}
或者:
for (Iterator it =collection.iterator(); it.hasNext();) { Object obj = it.next(); // 得到下一个元素 }
由Collection接口派生的两个接口是List和Set。
1.1: List接口
public interface List implements Collection<E>
List
is a collection which maintains an ordering for its elements. Every element in the List
has an index. Each element can thus be accessed by its index, with the first index being zero. Normally, List
s allow duplicate elements, as compared to Sets, where elements have to be unique. List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。和下面要提到的Set不同,List允许有相同的元素。除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个 ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素, 还能向前或向后遍历。
实现List接口的常用类有LinkedList,ArrayList,Vector和Stack。
List总结
1. 所有的List中只能容纳单个不同类型的对象组成的表,而不是Key-Value键值对。例如:[ tom,1,c ];
2. 所有的List中可以有相同的元素,例如Vector中可以有 [ tom,koo,too,koo ];
3. 所有的List中可以有null元素,例如[ tom,null,1 ];
4. 基于Array的List(Vector,ArrayList)适合查询,而LinkedList(链表)适合添加,删除操作。
虽然Set同List都实现了Collection接口,但是他们的实现方式却大不一样。List基本上都是以Array为基础。但是Set则是在 HashMap的基础上来实现的,这个就是Set和List的根本区别。
1.1.1:LinkedList类
public class LinkedList extends AbstractSequentialList<E> implements Serializable Cloneable Deque<E> List<E> Queue<E>
LinkedList is an implementation of List
, backed by a doubly-linked list. All optional operations including adding, removing, and replacing elements are supported.
All elements are permitted, including null.
This class is primarily useful if you need queue-like behavior. It may also be useful as a list if you expect your lists to contain zero or one element, but still require the ability to scale to slightly larger numbers of elements. In general, though, you should probably use ArrayList
if you don't need the queue-like behavior.
LinkedList:不同于前面两种List,它不是基于Array的,作为链表数据结构方式,所以不受Array性能的限制。当对 LinkedList做添加,删除动作的时候只要更改nextNode的相关信息就可以实现了所以它适合于进行频繁进行插入和删除操作。这就是 LinkedList的优势,当然对于元素的位置获取等方面就逊色很多。
1.1.2:ArrayList类
public class ArrayList extends AbstractList<E> implements Serializable Cloneable RandomAccess
ArrayList is an implementation of List
, backed by an array. All optional operations including adding, removing, and replacing elements are supported.
All elements are permitted, including null.
This class is a good choice as your default List
implementation. Vector
synchronizes all operations, but not necessarily in a way that's meaningful to your application: synchronizing each call to get
, for example, is not equivalent to synchronizing the list and iterating over it (which is probably what you intended). CopyOnWriteArrayList
is intended for the special case of very high concurrency, frequent traversals, and very rare mutations.
ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。
size,isEmpty,get,set方法运行时间为常数。但是add方法开销为分摊的常数,添加n个元素需要O(n)的时间。其他的方法运行时间为线性。
每个ArrayList实例都有一个容量(Capacity),即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加,但是增长算法并 没有定义。当需要插入大量元素时,在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。和LinkedList一样,ArrayList也是非同步的(unsynchronized)。
ArrayList:同Vector一样是一个基于Array的,但是不同的是ArrayList不是同步的。所以在性能上要比Vector优越一些。 Android123提示大家适用于顺序性的查找
1.1.3:Vector类
public class Vector extends AbstractList<E> implements Serializable Cloneable List<E> RandomAccess
Vector is an implementation of List
, backed by an array and synchronized. All optional operations including adding, removing, and replacing elements are supported.
All elements are permitted, including null.
This class is equivalent to ArrayList
with synchronized operations. This has a performance cost, and the synchronization is not necessarily meaningful to your application: synchronizing each call to get
, for example, is not equivalent to synchronizing on the list and iterating over it (which is probably what you intended). If you do need very highly concurrent access, you should also consider CopyOnWriteArrayList
.
Vector基于Array的List,性能也就不可能超越Array,并且Vector是“sychronized”的,这个也是Vector和 ArrayList的唯一的区别。
1.1.3.1:Stack 类
public class Stack extends Vector<E>
Stack
is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables users to pop to and push from the stack, including null objects. There is no limit to the size of the stack.
Stack继承自Vector,实现一个后进先出的堆栈。Stack提供5个额外的方法使得 Vector得以被当作堆栈使用。基本的push和pop方法,还有peek方法得到栈顶的元素,empty方法测试堆栈是否为空,search方法检测 一个元素在堆栈中的位置。Stack刚创建后是空栈。
1.2:Set接口
Set是一种不包含重复的元素的Collection,即任意的两个元素e1和e2都有e1.equals(e2)=false,Set最多有一个null元素。
很明显,Set的构造函数有一个约束条件,传入的Collection参数不能包含重复的元素。
请注意:必须小心操作可变对象(Mutable Object)。如果一个Set中的可变元素改变了自身状态导致Object.equals(Object)=true将导致一些问题。
Set总结:
1. Set实现的基础是Map(HashMap);
2. Set中的元素是不能重复的,如果使用add(Object obj)方法添加已经存在的对象,则会覆盖前面的对象,不能包含两个元素e1、e2(e1.equals(e2))。
3。Set是数学中定义的集合,所以元素无序, 且不能重复添加。java程序中Set集合用的不多,
1.2.1: HashSet
public class HashSet extends AbstractSet<E> implements Serializable Cloneable Set<E>
HashSet is an implementation of a Set. All optional operations (adding and removing) are supported. The elements can be any objects.
1.2.2:TreeSet
public class TreeSet extends AbstractSet<E> implements Serializable Cloneable NavigableSet<E>
TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are supported. The elements can be any objects which are comparable to each other either using their natural order or a specified Comparator.
2:Map接口
public interface Map
A Map
is a data structure consisting of a set of keys and values in which each key is mapped to a single value. The class of the objects used as keys is declared when the Map
is declared, as is the class of the corresponding values.
A Map
provides helper methods to iterate through all of the keys contained in it, as well as various methods to access and update the key/value pairs.
Map是一种把键对象(key)和值对象(value)进行关联的容器,关键字key是唯一不重复的。Map是一个有序的集合,所以查询起来速度很快
Map有两种比较常用的实现: HashTable、HashMap和TreeMap。
TreeMap则是对键按序存放,因此它有一些扩展的方法,比如firstKey(),lastKey()等。
HashMap和Hashtable的区别。 HashMap允许空(null)键(key)或值(value),由于非线程安全,效率上可能高于Hashtable。 Hashtable不允许空(null)键(key)或值(value)。
2.1:Hashtable类
public class Hashtable extends Dictionary<K, V> implements Serializable Cloneable Map<K, V>
Hashtable is a synchronized implementation of Map
. All optional operations are supported.
Neither keys nor values can be null. (Use HashMap
or LinkedHashMap
if you need null keys or values.)
2.2:HashMap类
public class HashMap extends AbstractMap<K, V> implements Serializable Cloneable
HashMap is an implementation of Map
. All optional operations are supported.
All elements are permitted as keys or values, including null.
Note that the iteration order for HashMap is non-deterministic. If you want deterministic iteration, use LinkedHashMap
.
Note: the implementation of HashMap
is not synchronized. If one thread of several threads accessing an instance modifies the map structurally, access to the map needs to be synchronized. A structural modification is an operation that adds or removes an entry. Changes in the value of an entry are not structural changes.
The Iterator
created by calling the iterator
method may throw a ConcurrentModificationException
if the map is structurally changed while an iterator is used to iterate over the elements. Only the remove
method that is provided by the iterator allows for removal of elements during iteration. It is not possible to guarantee that this mechanism works in all cases of unsynchronized concurrent modification. It should only be used for debugging purposes.
HashMap也用到了哈希码的算法,以便快速查找一个键,
2.3:WeakHashMap类
public class WeakHashMap extends AbstractMap<K, V> implements Map<K, V>
WeakHashMap is an implementation of Map with keys which are WeakReferences. A key/value mapping is removed when the key is no longer referenced. All optional operations (adding and removing) are supported. Keys and values can be any objects. Note that the garbage collector acts similar to a second thread on this collection, possibly removing keys.
WeakHashMap是一种改进的HashMap,它对key实行“弱引用”,如果一个key不再被外部所引用,那么该key可以被GC回收。