集合框架知识系列02 集合顶层接口

首先,下面是Collection、Map和、Set接口的相关架构图

  1. Collection接口

clipboard.png

  1. Map接口

clipboard.png
图片来源:http://www.cnblogs.com/skywan...

一、Collection相关接口和类

和Collection相关的接口主要有Collection、List和Set接口,其他接口会在介绍三个接口中穿插讲解。

1、Collection接口

  • Collection是一个抽象出来的接口,定义如下:
public interface Collection<E> extends Iterable<E> {}

其中包括了集合的基本操作,包括:删除、添加、遍历、大小等。
Collection中定义方法如下:

int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
//1.8新增
default boolean removeIf(Predicate<? super E> filter){}
default Spliterator<E> spliterator(){}
default Stream<E> stream(){}
default Stream<E> parallelStream() {}
  • AbstractCollection抽象类继承自Collection,实现了除iterator()和size()的所有方法。定义如下:
public abstract class AbstractCollection<E> implements Collection<E> {}

2、List接口

  • List接口继承自Collection,List中的元素的允许重复的。定义如下:
public interface List<E> extends Collection<E> {}

和Collection不重合、List特有的方法如下:

boolean addAll(int index, Collection<? extends E> c);
default void replaceAll(UnaryOperator<E> operator) {}
default void sort(Comparator<? super E> c) {}
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<E> listIterator();
ListIterator<E> listIterator(int index);
List<E> subList(int fromIndex, int toIndex);
  • AbstractList抽象类继承了AbstractCollection,并且实现了List接口,定义如下:
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}

3、Set接口

  • Set接口继承自Collection,Set是数学中定义的集合,元素不允许重复。定义如下:
public interface Set<E> extends Collection<E> {}

Set接口和Collection中方法一致,具体见Collection接口方法。

  • AbstractSet抽象类继承了AbstractCollection,并且实现了Set接口,定义如下:
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}

二、Map相关接口和类

Map是一种键值对的映射,没有继承Collection接口,具体定义如下:

public interface Map<K,V> {}

1、Map接口

Map接口中定义了添加、删除、遍历等相关方法,具体方法如下:

int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
V get(Object key);
V put(K key, V value);
V remove(Object key);
void putAll(Map<? extends K, ? extends V> m);
void clear();
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();
interface Entry<K,V> {}
boolean equals(Object o);
int hashCode();
//1.8新增
default V getOrDefault(Object key, V defaultValue) {}
default void forEach(BiConsumer<? super K, ? super V> action) {}
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {}
default V putIfAbsent(K key, V value) {}
default boolean remove(Object key, Object value) {}
default boolean replace(K key, V oldValue, V newValue) {}
default V replace(K key, V value) {}
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {}
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {}
default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {}
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction){}

2、AbstractMap类

AbstractMap抽象类实现了Map接口,实现Map中定义的方法,定义如下:

public abstract class AbstractMap<K,V> implements Map<K,V> {}

3、SortedMap和NavigableMap接口

SortedMap表示一个有序的键值映射,排序的方式有两种:自然排序和指定比较强排序。插入有序的SortedMap的所有元素都必须实现Comparable接口,具体方法如下:

Comparator<? super K> comparator();
SortedMap<K,V> subMap(K fromKey, K toKey);
SortedMap<K,V> headMap(K toKey);
SortedMap<K,V> tailMap(K fromKey);
K firstKey();
K lastKey();
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();

NavigableMap是SortedMap接口的扩展,有针对给定搜索目标返回最接近匹配项的导航方法。具体方法如下:

Map.Entry<K,V> lowerEntry(K key);
K lowerKey(K key);
Map.Entry<K,V> floorEntry(K key);
K floorKey(K key);
Map.Entry<K,V> ceilingEntry(K key);
K ceilingKey(K key);
Map.Entry<K,V> higherEntry(K key);
K higherKey(K key);
Map.Entry<K,V> firstEntry();
Map.Entry<K,V> lastEntry();
Map.Entry<K,V> pollFirstEntry();
Map.Entry<K,V> pollLastEntry();
NavigableMap<K,V> descendingMap();
NavigableSet<K> navigableKeySet();
NavigableSet<K> descendingKeySet();
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey,   boolean toInclusive);
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
SortedMap<K,V> subMap(K fromKey, K toKey);
SortedMap<K,V> headMap(K toKey);
SortedMap<K,V> tailMap(K fromKey);

Iterator相关接口

Iterator的工作是遍历并选择序列中的对象,它提供了一种访问一个容器(container)对象中的各个元素,而又不必暴露该对象内部细节的方法。通过迭代器,开发人员不需要了解容器底层的结构,就可以实现对容器的遍历。由于创建迭代器的代价小,因此迭代器通常被称为轻量级的容器。

1、Iterator接口

Iterator接口的定义如下:

public interface Iterator<E> {}

Iterator中定义的方法如下:

boolean hasNext();
E next();
//1.8新增
default void remove() {}
default void forEachRemaining(Consumer<? super E> action) {}

2、ListIterator接口

支持在迭代期间向List中添加或删除元素,并且可以在List中双向滚动。定义如下:

public interface ListIterator<E> extends Iterator<E> {}

ListIterator中定义的方法如下:

boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E e);
void add(E e);

本节主要总结了集合相关的顶层接口,下一节将分析每一类集合实现类。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值