数据结构:List/Set/数组杂谈

一,List概述

List官方说明:An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each

element is inserted.  The user can access elements by their integer index (position in the list), and search for elements in the
list.List是有序集合,也是继承了collection接口。list的一个重要特点,是可以下标进行访问,这一点有别于Queue和Deque。
    // Query Operations
    boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator();
    Object[] toArray();
    <T> T[] toArray(T[] a);
    // Modification Operations
    boolean add(E e);
    boolean remove(Object o);
    // Bulk Modification Operations
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean addAll(int index, Collection<? extends E> c);
    boolean removeAll(Collection<?> c);
    boolean retainAll(Collection<?> c);
    void clear();
    // Comparison and hashing
    boolean equals(Object o);
    int hashCode();
    // Positional Access Operations
    E get(int index);
    E set(int index, E element);
    void add(int index, E element);
    E remove(int index);
    // Search Operations
    int indexOf(Object o);
    int lastIndexOf(Object o);
    // List Iterators
    ListIterator<E> listIterator();
    ListIterator<E> listIterator(int index);
    // View
    List<E> subList(int fromIndex, int toIndex);

二,list在底层实现上,是线性表?是链表?还是哈希表?


    Set、List、Map虽然都可以统称为集合,但是如果更准确的描述,Set是集,List是集合,Map是映射,这三个接口都是继承了Collection接口。
下面是继承关系:
Collection <-- List <-- Vector
Collection <-- List <-- ArrayList
Collection <-- List <-- LinkedList
Collection <-- Set  <-- HashSet
Collection <-- Set  <-- HashSet    <-- LinkedHashSet
Collection <-- Set  <-- SortedSet  <-- TreeSet
关于List的总结:
    * 所有的List只能容纳单个不同类型的对象组成的表,而不是Key-Value键值对。例如[yangcq,1,c];
    * 所有的List中,可以存在相同元素,例如[yangcq,yangcq,yang];
    * 所有的List中,可以存在null元素,例如[yangcq,null,yang];
    * 基于Array的List,Vector、ArrayList,查询速度较快;LinkedList(链表)增删速度较快。
关于Set的总结:
    * Set实现的基础是Map(HashMap);
    * Set中的元素是不能重复的,因为HashMap的key是不能重复的。
    * Set是无序的;

通常情况下,在选择使用哪种集合时,我们通常要考虑下面几个因素:
    * API;
    * 是否支持多线程;
    * 数据增长性;
    * 使用模式;
下面我们来具体分析,以Vector和ArrayList为例:
API:从API的角度来看,Vector和ArrayList很相似,区别很小;
是否支持多线程:Vector支持多线程,是线程安全的。Array不是线程安全的。凡事有利有弊,同步需要额外的系统开销,所以Vector的性能
不如ArrayList。
数据增长性:从内部实现机制来看,Vector和ArrayList都是使用数组Array来存放集合中的对象。当增加的元素超过了内部数组的长度,就需要
进行扩容,Vector默认自动扩容100%,而ArrayList自动扩容50%。所以如果需要保存大量数据的时候,使用Vector具有更好的性能。因为每次扩
容,都需要额外的系统开销,Vector扩容的次数要少于ArrayList。
使用模式:在Vector和ArrayList中,从一个指定的位置查找元素或者在集合的末尾增加、删除一个元素,所花费的时间时一样的,时间复杂度
都是O(1)。但是,如果在集合的其他位置增加、删除元素,那么时间复杂度就是O(n),n表示集合中元素的个数。因为插入的元素前面或者后面
的所有元素都需要位移。也就是说增加、删除元素,Vector和ArrayList的性能都很低,我们应该选择LinkedList。

补充:Constructs an empty vector so that its internal data array has size {@code 10} and its standard capacity increment is
zero.从Vector的官方说明可以看出,如果不指定长度,vector的默认初始长度为10。
public Vector() {
    this(10);
}
因此:下面2种写法是等效的。
Vector<String> vector = new Vector<String>(10);
Vector<String> vector = new Vector<String>();

ArrayList的默认初始长度也是10,这一点和Vector一致。HashMap的初始值和最大值是多少呢,有兴趣的猿友可以查看一下源代码。


三,数组的用法

package com.ebank.action;
import java.util.ArrayList;
import java.util.Vector;
/**
 *
 * @author yangcq
 * @description 数组(Java界效率最高的数据结构)
 *
 */
public class ArrayTest {

    public static void main(String[] args) {
        
        // Vector 和 ArrayList都是以数组作为存储对象的集合。也就是说,Vector 和 ArrayList都是在底层
        // 都是以数组为结构来实现的。
        
        // Vector的默认初始长度为10
        Vector<String> vector = new Vector<String>(10);
        ArrayList<String> arrayList = new ArrayList<String>(10);
        String[] strArray = {"aaaaa","bbbbb"};  // 字符串数组
        Double[] douArray = {111.00,12.21};     // 双精度整形数组
        Long[] longArray = {1250l,5895l};       // 长整形数组
        
        // Vector是同步的,所以性能低于ArrayList
        vector.add("aaaa");
        // ArrayList不是同步的,所以性能优于Vector
        arrayList.add("bbbb");
        // array的性能优于Vector 和 ArrayList
        // 在Java界,功能越强大,性能越低。有时候,简单的,反而是最好的选择。
        
        // 数组是一个简单的线性序列,基于Native,所以,历来号称Java中效率最高的数据结构。
        // 但是,由于数组初始化后大小规定,索引不能超过下标,不能灵活扩展,因此使用并不多。
        System.out.println(strArray[0]);
        System.out.println(douArray[0]);
        System.out.println(longArray[0]);
    }
}

总之,大牛建议,用Array代替Vector和ArrayList。尤其是对执行效率要求高的程序。因为使用Array避免了同步、额外的方法调用、不必要的

重新分配空间等系统开销。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值