集合类-List

List接口简介

List中的元素是有序的、可以重复并且可以为null,一般推荐使用ArrayList实现类。

1. 父类

List接口继承自Collection接口,并新增了操作方法,如下表:

方法CollectionList
int size()存在存在
boolean isEmpty()存在存在
boolean contains(Object o)存在存在
Iterator iterator()存在存在
Object[] toArray()存在存在
T[] toArray(T[] a)存在存在
boolean add(E e)存在存在
boolean remove(Object o)存在存在
boolean containsAll(Collection c)存在存在
boolean addAll(Collection c)存在存在
boolean removeAll(Collection c)存在存在
boolean retainAll(Collection c)存在存在
void clear()存在存在
boolean equals(Object o)存在存在
int hashCode()存在存在
E get(int index)新增
E set(int index, E element)新增
void add(int index, E element)新增
E remove(int index)新增
int indexOf(Object o)新增
int lastIndexOf(Object o)新增
ListIterator listIterator()新增
ListIterator listIterator(int index)新增
List subList(int fromIndex, int toIndex)新增

2. 实现类
List下有ArrayList、Vector、LinkedList实现类,Vector下有Stack子类。
ArrayList
1、ArrayList是基于数组实现,其实内部是个动态数组。
2、ArrayList继承自AbstractList,实现了List(增删改查), RandomAccess(随机访问), Cloneable(克隆), java.io.Serializable(序列化)接口。

     class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable 

3、ArrayList的操作方法没有实现同步,不是线程安全的,同步可以使用Vector、java.util.concurrent.CopyOnWriteArrayList(推荐使用)类。


LinkedList
1、LinkedList基于链表实现。
2、LinkedList继承自AbstractSequentialList,实现了List(增删改查), Deque(双向队列), Cloneable(克隆), java.io.Serializable(序列化)接口。

     class LinkedList<E> extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

3、LinkedList基于链表,没实现随机访问接口,访问时比ArrayList慢;但是频繁插入或删除元素时使用LinkedList列表性能会更好。


Vector
Vector继承的类和实现的接口和ArrayList相同,不同点事Vector类中对于集合元素的操作方法都使用了synchronized关键字修饰,提供同步的方法。是线程安全的,但是性能较差,单线程推荐使用ArrayList。


Stack
1、Stack是一个后进先出(last in first out,LIFO)的堆栈。
2、继承Vector类,并增加了push,pop,peek,empty,search方法。


3. 与其他集合类型比较

List与Array
相同点:同一类型的一组对象;可以使用下标访问。
不同点:数组可以使用所有类型,List只能使用包装类型;数组容量固定不变,List长度可变;数组效率比List高。
固定长度时,使用数组,更省空间,更高效;变化长度使用List更好。

List与Set
相同点:继承自Collection接口,可以保存一组相同类型的数据。
不同点:元素顺序不同,List有序,Set无序;List可以有重复值,Set不能重复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值