java集合的抽象是什么_Java集合-02集合抽象类

Collection简介

Collcetion定义(java version:1.8.0_111)

public interface Collection extends Iterable{

boolean add(E e);

boolean addAll(Collection extends E> c);

boolean remove(Object o);

void clear();

Iterator iterator();

boolean isEmpty();

int size();

boolean contains(Object o);

boolean containsAll(Collection> c);

boolean removeAll(Collection> c);

boolean retainAll(Collection> c);

Object[] toArray();

T[] toArray(T[] a);

}

Collection接口定义了集合的基本操作:

添加,删除,清空,遍历,是否为空,大小,是否包含某元素或者集合,变为数组等方法

List简介

List定义:

public interface List extends Collection{

boolean add(E e);

boolean addAll(Collection extends E> c);

boolean remove(Object o);

void clear();

Iterator iterator();

boolean isEmpty();

int size();

boolean contains(Object o);

boolean retainAll(Collection> c);

boolean removeAll(Collection> c);

boolean containsAll(Collection> c);

Object[] toArray();

T[] toArray(T[] a);

//在Collection接口基础上添加新的方法

E remove(int index);

boolean addAll(int index, Collection extends E> c);

E get(int index);

E set(int index, E element);

void add(int index, E element);

int indexOf(Object o);

int lastIndexOf(Object o);

ListIterator listIterator();

ListIterator listIterator(int index);

List subList(int fromIndex, int toIndex);

}

List继承Collection接口,是一个有序可重复的集合,它的每一个元素都有一个索引,第一个索引为0;

List提供了一个特殊的迭代器叫做ListIterator,允许在Iterator的基础上允许元素插入和替换以及双向存取

Set简介

Set定义:

public interface Set extends Collection{

boolean add(E e);

boolean addAll(Collection extends E> c);

boolean remove(Object o);

void clear();

Iterator iterator();

boolean isEmpty();

int size();

boolean contains(Object o);

boolean containsAll(Collection> c);

boolean removeAll(Collection> c);

boolean retainAll(Collection> c);

Object[] toArray();

T[] toArray(T[] a);

}

Set和Collection接口API没有什么区别,Set是一个不包含重复元素的集合,最多一个null元素

Queue简介

public interface Queue extends Collection{

boolean add(E e);

boolean offer(E e);

E remove();

E poll();

E element();

E peek();

}

Queue是在java5添加的,用于支持队列的常见操作,队列的一个特性为先进先出,即先添加的元素是最先被删除的元素。

拓展了Collection接口,add和offer方法添加元素,不同的是

add方法添加失败会抛出异常,故添加元素时候优先选择offer方法;

remove和poll方法为移除元素,同样的优先使用poll方法,如果为空队列时候,

remove方法会抛出异常,而poll方法会返回null;

element和peek方法遍历队列,优先使用peek,element方法在在空队列时使用抛出异常

AbstractCollection简介

public abstract class AbstractCollection implements Collection{

protected AbstractCollection() {

}

public abstract Iterator iterator();

public abstract int size();

public boolean isEmpty() {

return size() == 0;

}

public boolean contains(Object o) {

Iterator it = iterator();

if (o==null) {

while (it.hasNext())

if (it.next()==null)

return true;

} else {

while (it.hasNext())

if (o.equals(it.next()))

return true;

}

return false;

}

public Object[] toArray() {

// Estimate size of array; be prepared to see more or fewer elements

Object[] r = new Object[size()];

Iterator it = iterator();

for (int i = 0; i < r.length; i++) {

if (! it.hasNext()) // fewer elements than expected

return Arrays.copyOf(r, i);

r[i] = it.next();

}

return it.hasNext() ? finishToArray(r, it) : r;

}

public T[] toArray(T[] a) {

// Estimate size of array; be prepared to see more or fewer elements

int size = size();

T[] r = a.length >= size ? a :

(T[])java.lang.reflect.Array

.newInstance(a.getClass().getComponentType(), size);

Iterator it = iterator();

for (int i = 0; i < r.length; i++) {

if (! it.hasNext()) { // fewer elements than expected

if (a == r) {

r[i] = null; // null-terminate

} else if (a.length < i) {

return Arrays.copyOf(r, i);

} else {

System.arraycopy(r, 0, a, 0, i);

if (a.length > i) {

a[i] = null;

}

}

return a;

}

r[i] = (T)it.next();

}

// more elements than expected

return it.hasNext() ? finishToArray(r, it) : r;

}

public boolean add(E e) {

throw new UnsupportedOperationException();

}

public boolean remove(Object o) {

Iterator it = iterator();

if (o==null) {

while (it.hasNext()) {

if (it.next()==null) {

it.remove();

return true;

}

}

} else {

while (it.hasNext()) {

if (o.equals(it.next())) {

it.remove();

return true;

}

}

}

return false;

}

public boolean containsAll(Collection> c) {

for (Object e : c)

if (!contains(e))

return false;

return true;

}

public boolean addAll(Collection extends E> c) {

boolean modified = false;

for (E e : c)

if (add(e))

modified = true;

return modified;

}

public boolean removeAll(Collection> c) {

Objects.requireNonNull(c);

boolean modified = false;

Iterator> it = iterator();

while (it.hasNext()) {

if (c.contains(it.next())) {

it.remove();

modified = true;

}

}

return modified;

}

public boolean retainAll(Collection> c) {

Objects.requireNonNull(c);

boolean modified = false;

Iterator it = iterator();

while (it.hasNext()) {

if (!c.contains(it.next())) {

it.remove();

modified = true;

}

}

return modified;

}

public void clear() {

Iterator it = iterator();

while (it.hasNext()) {

it.next();

it.remove();

}

}

public String toString() {

Iterator it = iterator();

if (! it.hasNext())

return "[]";

StringBuilder sb = new StringBuilder();

sb.append('[');

for (;;) {

E e = it.next();

sb.append(e == this ? "(this Collection)" : e);

if (! it.hasNext())

return sb.append(']').toString();

sb.append(',').append(' ');

}

}

}

AbstractCollection是一个抽象类,实现了除iterator和size之外的函数,当然add之定义了个异常,

因为add方法针对不同的集合会做不同的处理,这样不管我们自己定义还是java本身的一些常用集合都可以

继承这个抽象类

AbstractList简介

AbstractList抽象类继承AbstractCollection类实现List接口,实现了List中除了size()和get(int index)之外的函数

相对于AbstractCollection,它实现了Iterator

AbstractSet简介

AbstractSet抽象类继承AbstractCollection类实现Set接口,相对于AbstractCollection没有太大的变化,

只是针对Set重写了equals和removeAll方法

Deque

Deque继承Queue,不过它是一个双向队列,在Queue上有了更加丰富的特性,支持从两个端点添加和删除元素

SortedSet

public interface SortedSet extends Set{

Comparator super E> comparator();//就是它,实现类重写这个方法,使得set变为有序

SortedSet subSet(E fromElement, E toElement);

SortedSet headSet(E toElement);

SortedSet tailSet(E fromElement);

E first();

E last();

E last();

}

SortedSet接口继承Set,在Set基础上添加了其他方法,使得Set变为有序集合

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值