java之List ArrayList LinkList

来源:互联网 发布:up卫士炒股软件 编辑:程序博客网 时间:2024/05/22 11:32
List(public interface)
A 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, Lists allow duplicate elements, as compared to Sets, where elements have to be unique.
------------------------------------------------------------------
ArrayList(public class)
Class Overview


ArrayList is an implementation(实现) of List, backed by an array. All optional operations including adding, removing, and replacing elements are supported.
是List的一个实现,基于Array。

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.


List ArrayList LinkList区别

1.ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构。

2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList要移动指针。

3.对于新增和删除操作add和remove,LinkList比较占优势,因为ArrayList要移动数据。

从上面三点可以看出:

ArrayList和LinkList是两个集合类,用于存储一系列的对象引用(references)。例如我们可以用ArrayList来存储一系列的String或者Integer。

而,List继承自Collection接口。List是一种有序集合,List中的元素可以根据索引(顺序号:元素在集合中处于的位置信息)进行取得/删除/插入操作。

总结如下:

List是一个接口,ArrayList、LinkList继承与这个接口并实现了它.

用的时候,可以这么用: List<String> list = new ArrayList<String>   等同于  ArrayList<String> list=new ArrayList<String>

Java提供的基础数据类型辅助类ArrayList LinkedList Set HashMap的简单介绍:


   在Java中提供了Collection和Map接口。其中List和Set继承了Collection接口;同时用Vector、 ArrayList、LinkedList三个类实现List接口,HashSet、TreeSet实现Set接口。直接有HashTable、 HashMap、TreeMap实现Map接口。

    Vector:基于Array的List,性能也就不可能超越Array,并且Vector是“sychronized”(注:同步的)的,这个也是Vector和ArrayList的唯一的区别。

    ArrayList:同Vector一样是一个基于Array的,但是不同的是ArrayList不是同步的。所以在性能上要比Vector优越一些。DevDiv提示大家ArrayList适用于顺序性的查找

    LinkedList:不同于前面两种List,它不是基于Array的,作为链表数据结构方式,所以不受Array性能的限制。当对 LinkedList做添加,删除动作的时候只要更改nextNode的相关信息就可以实现了所以它适合于进行频繁进行插入和删除操作。这就是 LinkedList的优势,当然对于元素的位置获取等方面就逊色很多。

    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的根本区别。

     HashSet:HashSet的存储方式是把HashMap中的Key作为Set的对应存储项,HashMap的key是不能有重复的。HashSet能快速定位一个元素,但是放到HashSet中的对象需要实现hashCode()方法0。

    TreeSet:将放入其中的元素按序存放,这就要求你放入其中的对象是可排序的。TreeSet不同于HashSet的根本是TreeSet是有序的。它是通过SortedMap来实现的。

    Set总结: 1. Set实现的基础是Map(HashMap); 2. Set中的元素是不能重复的,如果使用add(Object obj)方法添加已经存在的对象,则会覆盖前面的对象,不能包含两个元素e1、e2(e1.equals(e2))。

    Map:
    是一种把键对象和值对象进行关联的容器,Map有两种比较常用的实现: HashTable、HashMap和TreeMap。

    HashMap也用到了哈希码的算法,以便快速查找一个键,

    TreeMap则是对键按序存放,因此它有一些扩展的方法,比如firstKey(),lastKey()等。

    HashMap和Hashtable的区别。 HashMap允许空(null)键(key)或值(value),由于非线程安全,效率上可能高于Hashtable。 Hashtable不允许空(null)键(key)或值(value)。



原创粉丝点击