Java 中的List之Vector再认知

目录

代码

方法

LearnVector.java 

 TravesingVector.java(遍历Vector的四种方式)


Java代码写了不少,但是就是碎片化的,现在系统的整理一下;

代码

方法

字段摘要
     protected  int capacityIncrement
               向量的大小大于其容量时,容量自动增加的量。
     protected  int elementCount
               Vector 对象中的有效组件数。
     protected  Object[] elementData
               存储向量组件的数组缓冲区。 

从类 java.util.AbstractList 继承的字段
     modCount 

构造方法摘要
     Vector()
               构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。
     Vector(Collection<? extends E> c)
               构造一个包含指定 collection 中的元素的向量,这些元素按其 collection 的迭代器返回元素的顺序排列。
     Vector(int initialCapacity)
               使用指定的初始容量和等于零的容量增量构造一个空向量。
     Vector(int initialCapacity, int capacityIncrement)
               使用指定的初始容量和容量增量构造一个空的向量。 

方法摘要
      boolean  add(E e)
               将指定元素添加到此向量的末尾。
      void     add(int index, E element)
               在此向量的指定位置插入指定的元素。
      boolean  addAll(Collection<? extends E> c)
               将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。
      boolean  addAll(int index, Collection<? extends E> c)
               在指定位置将指定 Collection 中的所有元素插入到此向量中。
      void     addElement(E obj)
               将指定的组件添加到此向量的末尾,将其大小增加 1。
      int capacity()
               返回此向量的当前容量。
      void     clear()
               从此向量中移除所有元素。
      Object   clone()
               返回向量的一个副本。
      boolean  contains(Object o)
               如果此向量包含指定的元素,则返回 true。
      boolean  containsAll(Collection<?> c)
               如果此向量包含指定 Collection 中的所有元素,则返回 true。
      void     copyInto(Object[] anArray)
               将此向量的组件复制到指定的数组中。
      E   elementAt(int index)
               返回指定索引处的组件。
      Enumeration<E>     elements()
               返回此向量的组件的枚举。
      void     ensureCapacity(int minCapacity)
               增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。
      boolean  equals(Object o)
               比较指定对象与此向量的相等性。
      E   firstElement()
               返回此向量的第一个组件(位于索引 0) 处的项)。
      E   get(int index)
               返回向量中指定位置的元素。
      int hashCode()
               返回此向量的哈希码值。
      int indexOf(Object o)
               返回此向量中第一次出现的指定元素的索引,如果此向量不包含该元素,则返回 -1。
      int indexOf(Object o, int index)
               返回此向量中第一次出现的指定元素的索引,从 index 处正向搜索,如果未找到该元素,则返回 -1。
      void     insertElementAt(E obj, int index)
               将指定对象作为此向量中的组件插入到指定的 index 处。
      boolean  isEmpty()
               测试此向量是否不包含组件。
      E   lastElement()
               返回此向量的最后一个组件。
      int lastIndexOf(Object o)
               返回此向量中最后一次出现的指定元素的索引;如果此向量不包含该元素,则返回 -1。
      int lastIndexOf(Object o, int index)
               返回此向量中最后一次出现的指定元素的索引,从 index 处逆向搜索,如果未找到该元素,则返回 -1。
      E   remove(int index)
               移除此向量中指定位置的元素。
      boolean  remove(Object o)
               移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。
      boolean  removeAll(Collection<?> c)
               从此向量中移除包含在指定 Collection 中的所有元素。
      void     removeAllElements()
               从此向量中移除全部组件,并将其大小设置为零。
      boolean  removeElement(Object obj)
               从此向量中移除变量的第一个(索引最小的)匹配项。
      void     removeElementAt(int index)
               删除指定索引处的组件。
     protected  void     removeRange(int fromIndex, int toIndex)
               从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。
      boolean  retainAll(Collection<?> c)
               在此向量中仅保留包含在指定 Collection 中的元素。
      E   set(int index, E element)
               用指定的元素替换此向量中指定位置处的元素。
      void     setElementAt(E obj, int index)
               将此向量指定 index 处的组件设置为指定的对象。
      void     setSize(int newSize)
               设置此向量的大小。
      int size()
               返回此向量中的组件数。
      List<E>  subList(int fromIndex, int toIndex)
               返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
      Object[] toArray()
               返回一个数组,包含此向量中以恰当顺序存放的所有元素。
     <T> T[]   toArray(T[] a)
               返回一个数组,包含此向量中以恰当顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。
      String   toString()
               返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。
      void     trimToSize()
               对此向量的容量进行微调,使其等于向量的当前大小。 

继承方法
     从类 java.util.AbstractList 继承的方法
          iterator, listIterator, listIterator 

     从类 java.lang.Object 继承的方法
          finalize, getClass, notify, notifyAll, wait, wait, wait 

     从接口 java.util.List 继承的方法
          iterator, listIterator, listIterator

LearnVector.java 

package javacollection.list;


import java.util.*;

/**
 * @ClassName LearnVector
 * @Author zhangqx02
 * @Date 2019/9/19 19:17
 * @Description
 */

public class LearnVector {
    public static void main(String[] args){
        vectorMethod();
        
    }

    private static void vectorMethod(){

        //  构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。
        Vector vector = new Vector();

        vector.add("Hadoop");
        vector.add("Spark");

        String[] language = {"Scala","Java","C++"};

        //将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。
        vector.addAll(Arrays.asList(language));

        System.out.println("vector: "+ vector.toString());

        //   返回此向量的当前容量。
        int capacity = vector.capacity();
        System.out.println("capacity: "+ capacity);

        //  返回此向量中的组件数。
        int size = vector.size();
        System.out.println("size: "+ size);

        vector.trimToSize();
        // 对此向量的容量进行微调,使其等于向量的当前大小。
        int trimCapacity = vector.capacity();
        System.out.println("trimCapacity: "+ trimCapacity);

        // 在此向量的指定位置插入指定的元素。
        vector.add(0,"Hive");
        System.out.println("vector: "+ vector.toString());

        //  将此向量的组件复制到指定的数组中。
        String[] strArray = new  String[10];
        vector.copyInto(strArray);
        int index = 0;
        while (strArray[index] != null){
            System.out.println("strArray: "+ strArray[index]);
            index++;
        }

        // 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
        List list = vector.subList(2, 4);
        System.out.println("list: "+ list.toString());

        //  在此向量中仅保留包含在指定 Collection 中的元素。
        String[] retain = {"Hadoop","Java","Scala"};
        vector.retainAll(Arrays.asList(retain));
        System.out.println("vector: "+vector.toString());

        // 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
        Object[] objArr = vector.toArray();
        for (Object obj:objArr) {
            System.out.println("toArray: "+ obj);
        }

        // 返回一个数组,包含此向量中以恰当顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。
        String[] strArr = (String[]) vector.toArray(new String[0]);
        for (String string:strArr) {
            System.out.println("strArr: "+ string);

        }

        System.out.println();

    }
   

}

 TravesingVector.java(遍历Vector的四种方式)

package javacollection.list;


import java.util.*;

/**
 * @ClassName TravesingVector
 * @Author zhangqx02
 * @Date 2019/9/19 19:17
 * @Description
 */

public class TravesingVector{
    public static void main(String[] args){
        Vector vector = new Vector();
        for(int i =0 ;i <10000000;i++ ){
            vector.add(i);
        }
        iteratorThroughEnumeration(vector);
        iteratorThroughRandomAccess(vector);
        iteratorThroughFor2(vector);
        iteratorThroughIterator(vector);
    }

    private static void vectorMethod(){

        //  构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。
        Vector vector = new Vector();

        vector.add("Hadoop");
        vector.add("Spark");

        String[] language = {"Scala","Java","C++"};

        //将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。
        vector.addAll(Arrays.asList(language));

        System.out.println("vector: "+ vector.toString());

        //   返回此向量的当前容量。
        int capacity = vector.capacity();
        System.out.println("capacity: "+ capacity);

        //  返回此向量中的组件数。
        int size = vector.size();
        System.out.println("size: "+ size);

        vector.trimToSize();
        // 对此向量的容量进行微调,使其等于向量的当前大小。
        int trimCapacity = vector.capacity();
        System.out.println("trimCapacity: "+ trimCapacity);

        // 在此向量的指定位置插入指定的元素。
        vector.add(0,"Hive");
        System.out.println("vector: "+ vector.toString());

        //  将此向量的组件复制到指定的数组中。
        String[] strArray = new  String[10];
        vector.copyInto(strArray);
        int index = 0;
        while (strArray[index] != null){
            System.out.println("strArray: "+ strArray[index]);
            index++;
        }

        // 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
        List list = vector.subList(2, 4);
        System.out.println("list: "+ list.toString());

        //  在此向量中仅保留包含在指定 Collection 中的元素。
        String[] retain = {"Hadoop","Java","Scala"};
        vector.retainAll(Arrays.asList(retain));
        System.out.println("vector: "+vector.toString());

        // 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
        Object[] objArr = vector.toArray();
        for (Object obj:objArr) {
            System.out.println("toArray: "+ obj);
        }

        // 返回一个数组,包含此向量中以恰当顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。
        String[] strArr = (String[]) vector.toArray(new String[0]);
        for (String string:strArr) {
            System.out.println("strArr: "+ string);

        }

        System.out.println();

    }
    private static void isRandomAccessSupported(List list) {
        if (list instanceof RandomAccess) {
            System.out.println("RandomAccess implemented!");
        } else {
            System.out.println("RandomAccess not implemented!");
        }

    }

    public static void iteratorThroughRandomAccess(List list) {

        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        for (int i=0; i<list.size(); i++) {
            list.get(i);
        }
        endTime = System.currentTimeMillis();
        long interval = endTime - startTime;
        System.out.println("iteratorThroughRandomAccess:" + interval+" ms");
    }

    public static void iteratorThroughIterator(List list) {

        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        for(Iterator iter = list.iterator(); iter.hasNext(); ) {
            iter.next();
        }
        endTime = System.currentTimeMillis();
        long interval = endTime - startTime;
        System.out.println("iteratorThroughIterator:" + interval+" ms");
    }


    public static void iteratorThroughFor2(List list) {

        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        for(Object obj:list)
            ;
        endTime = System.currentTimeMillis();
        long interval = endTime - startTime;
        System.out.println("iteratorThroughFor2:" + interval+" ms");
    }

    public static void iteratorThroughEnumeration(Vector vec) {

        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        for(Enumeration enu = vec.elements(); enu.hasMoreElements(); ) {
            enu.nextElement();
        }
        endTime = System.currentTimeMillis();
        long interval = endTime - startTime;
        System.out.println("iteratorThroughEnumeration:" + interval+" ms");
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: JavaVectorList都是集合类,但它们有一些区别: 1. Vector是线程安全的,而List不是。这意味着在多线程环境下,Vector可以保证数据的一致性,而List可能会出现数据不一致的情况。 2. Vector是基于数组实现的,而List可以基于数组或链表实现。这意味着在插入或删除元素时,Vector可能需要重新分配内存空间,而List则可以通过修改指针来实现。 3. Vector支持使用枚举器(Enumeration)来遍历集合,而List则支持使用迭代器(Iterator)来遍历集合。 4. Vector的方法都是同步的,而List的方法不是。这意味着在多线程环境下,Vector的方法可能会出现阻塞,而List则不会。 总的来说,Vector适合在多线程环境下使用,而List适合在单线程环境下使用。如果需要在多线程环境下使用List,可以考虑使用Collections.synchronizedList方法来创建一个同步的List。 ### 回答2: JavaVectorList都是容器类,可以用来存储对象并对它们进行一些操作。它们最主要的区别在于它们的底层实现方式和线程安全性。 1、底层实现 VectorJava早期版本就存在的一个类,它使用了基于数组实现的方式来存储对象。在向Vector对象添加或删除元素时,它会重新分配内存空间,并把已有元素复制到新的数组。这种实现方式虽然能够保证线程安全,但是性能较差,特别是在大量操作时会导致性能下降。 List是一个接口,它定义了一些有序的集合操作,如增加、删除、查询等等。而List的实现类有很多,比如ArrayList、LinkedList等。这些实现类底层的数据结构不同,ArrayList使用数组来实现,而LinkedList使用双向链表来实现。 2、线程安全性 因为Vector是早期版本就存在的容器类,在当时Java还没有ConcurrentHashMap、ConcurrentSkipListMap等并发容器类时,就需要考虑线程安全性问题。因此,Vector是线程安全的,但是这会带来一些性能上的损失。 而List则被设计为非线程安全的,如果需要在多线程环境使用,可以考虑使用Collections工具类提供的synchronizedList方法或者使用CopyOnWriteArrayList等并发容器类来保证线程安全。 除了上述两个方面,VectorList还有一些细节上的区别,比如Vector可以设置增长因子(默认情况下为原来的两倍),而ArrayList不支持,不过这些细节对于开发者来说并不是很重要,选取哪个容器类主要还是根据实际需求和场景来决定。 ### 回答3: JavaVectorList都是容器类,用来存储一组对象,但是它们有一些区别: 1. 底层实现不同:Vector是线程安全的,而List不是线程安全的;Vector是通过数组实现的,而List是通过链表实现的。 2. 初始容量不同:对于Vector来说,需要指定初始容量,以便在容器元素超过该容量时,自动增加内部存储器。而List不需要指定初始容量,可以动态增加或缩减内部存储器。 3. 确定容量的方法不同:Vector有一个increment方法,它用来指定容器在重新分配存储空间时,增加的存储空间大小。而List没有这个方法。 4. 迭代器的行为不同:Vector使用基于枚举方法的迭代器,而List使用基于游标或指针的迭代器,效率相对较高。 5. 性能方面的差异:由于Vector是线程安全的,它的性能相对较差;List因为不是线程安全的,所以它的性能相对较好。 总而言之,VectorList都有自己的特点和优劣,可以根据具体的需求来选择使用哪一个。如果需要线程安全,且不在意性能,则可以使用Vector;如果对性能有较高要求,则可以使用List
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值