The Collection Interfaces in Java

开发中几乎离不开集合,虽然平时用到的就那么几个,对它的了解还是知之甚少,遂有了整理一下的想法,但看到Java中的集合非常的庞杂,因此,只对接口做了一些简单的梳理,下面步入正题:

谓之集合,指可以盛放多个对象,也可以把它称之为容器,集合类和数组不同,数组元素既可以是基本类型的值,也可以是对象;而集合里只能保存对象。一般大家把Collections分成三个体系:SetListMap,但从下面的图来看,也可以分为两个体系:CollectionMap,如果掌握了每个集合的特点,怎么分也就看个人喜好了。(该静下心来好好看看下图)

先来看Collection这一支:

Interface java.util.Iterable

此为集合根接口,如果有一个集合为Iterable,那么可以通过它获取一个Iterator,并且利用它作为目标来迭代。

public interface Iterable<T> {
   Iterator<T> iterator();
}

Interface java.util.Collection

该接口中定义的方法可以操作SetListQueue集合,当使用System.outprintln方法来输出集合对象时,将输出[ele1,ele2...]的形式,这显然是因为Collection的实现类重写了toString()方法,所有的Collection都重写了toString()方法,该方法可以一次性数据集合中的元素。至于遍历的方式就不做介绍了。

public interface Collection<E> extends Iterable<E> {
   Iterator<E> iterator();
   int size();
   boolean isEmpty();
   boolean contains(Object o);
   boolean add(E e);
   boolean remove(Object element);
   boolean containsAll(Collection<?> c);
   boolean addAll(Collection<? extends E> c);
   boolean removeAll(Collection<?> c);
   boolean retainAll(Collection<?> c);
   void clear();
   Object[] toArray();
   <T> T[] toArray(T[] a);
}

Interface java.util.Set

Set类似一个罐子,一旦把对象丢进”Set集合,集合里多个对象之间没有明显的顺序,Set集合与Collection基本完全一样,他没有提供额外的方法,实际上Set就是Collection,知识行为不同。Set集合不容许包含相同的元素,如果试图把两个相同元素加入同一个Set集合中,则添加操作失败,add方法返回false,且新元素不会被加入。

Set判断两个对象相同不是使用==运算符,而是根据equals方法。如果两个对象用equals方法比较返回trueSet就不会接受这两个对象;反之亦然。

public interface Set<E> extends Collection<E> {
}

Interface java.util.SortedSet

SortedSet进一步保证了Set内的元素按照升序排列,排序的规则是按照自然排序进行,如果需要自定义排序方法,那么就需要借助comparator接口了。每个方法的用法请详见API

public interface SortedSet<E> extends Set<E> {
   Comparator<? super E> comparator();
   E first();
   E last();
   SortedSet<E> subSet(E fromElement, E toElement);
   SortedSet<E> headSet(E toElement);
   SortedSet<E> tailSet(E fromElement);
}

Interface java.util.List

List作为Collection接口的子接口,可以使用Collection接口的全部方法,而且由于List是有序集合,因此List集合里增加了一些根据索引操作集合元素的方法,List中允许重复元素。

List额外提供了一个listIterator()方法,它继承自Iterator接口,拥有向前迭代的功能,还可以通过add方法向List集合中添加元素。

public interface List<E> extends Collection<E> {
   E get(int index);
   E set(int index, E e);
   void add(int index, E e);
   E remove(int index);
   boolean addAll(int index, Collection<? extends E> c);
   int indexOf(Object o);
   int lastIndexOf(Object o);
   ListIterator<E> listIterator();
   ListIterator<E> listIterator(int index);
   List<E> subList(int from, int to);
}

Interface java.util.Queue

Queue用于模拟队列数据结构,通常指FIFO的容器,访问元素操作会返回队列头部的元素,通常,队列不允许随机访问队列的元素。

public interface Queue<E> extends Collection<E> {
   boolean offer(E o);  // adds if possible
   E poll(); // retrieve and remove head (return null if empty)
   E remove(); // retrieve and remove head (throw if empty)
   E peek(); // retrieve but do not remove head (return null if empty)
   E element(); // retrieve but do not remove head (throw if empty)
}

Interface java.util.concurrent.BlockingQueue

此接口用到的不多,首先它是一个队列,那和上面介绍的队列有何区别呢?如果BlockingQueue为空,从BlockingQueue取元素将会被阻断进入等待状态,知道BlockingQueue放入元素才会被唤醒,同样,如果BlockingQueue是满的,任何试图放入元素的操作也会被阻断进入等待状态,知道BlockingQueue有空间才会被唤醒。

public interface BlockingQueue<E> extends Queue<E> {
   boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException;
   void put(E o) throws InterruptedException;
   boolean add(E e);
   E poll(long timeout, TimeUnit unit) throws InterruptedException;
   E take() throws InterruptedException;
   int drainTo(Collection<? super E> c);
   int drainTo(Collection<? super E> c, int maxElements);
   int remainingCapacity();
}

再来看Map这一支:

Interface java.util.Map

Map中把Key放在一起看,组成了一个Set集合,是不能重复的,Map确实包含一个keySet()方法,用于返回所有的key组成的set集合;如果把Map所有的value放在一起看,他非常类似于List,元素键可以重复,因此,Map也被称作字典。

Map提供了大量的实现类,EntryMap内部类,因此通过entrySet()方法可以取出包含key-valueMap.Entry对象。

public interface Map<K,V> {
   boolean containsKey(Object key);
   boolean containsValue(Object value);
   V put(K key, V value);
   V get(Object key);
   V remove(Object key);
   void putAll(Map<? extends K, ? extends V> t);
   void clear();
   public Set<K> keySet();
   public Collection<V> values();
   Set<Map.Entry<K, V>> entrySet();
   public interface Entry<K,V> {
       K getKey();
       V getValue();
       V setValue(V value);
   }
}

Interface java.util.SortedMap

正如Set接口派生出SortedSet子接口,SortedSet有一个TreeSet实现类,Map接口也派生了一个SortedMap子接口,SortedMap也有一个TreeMap实现类。与TreeSet类似的是,TreeMap也是基于红黑树对TreeMap所有key进行排序,从而保证TreeMap中所有key-value处于有序状态。

排序方法分自然排序和定制排序,如果自然排序可以必须实现Comparable接口,且所有的key是同一个类对象,定制排序则需要借助Compatator对象。

public interface SortedMap<K, V> extends Map<K, V> {
   Comparator<? super E> comparator();
   K firstKey();
   K lastKey();
   SortedMap<K,V> subMap(K fromKey, K toKey);
   SortedMap<K,V> headMap(K toKey);
   SortedMap<K,V> tailMap(K fromKey);
}

Interface java.util.concurrent.ConcurrentMap

对于很多并发应用程序来说是一个非常有用的接口,对并发有所了解的童鞋可进一步了解。本人只能写到这里了。

public interface ConcurrentMap<K, V> extends Map<K, V> {
   V putIfAbsent(K key, V value);
   boolean remove(Object key, Object value);
   boolean replace(K key, V oldValue, V newValue);
   V replace(K key, V value);
}

  至于实现类,在用到时可以百度或API查询即可,没必要一一列举,这里没有谈到线程安全和泛型,以后有时间再做梳理。(为后面Bean的注入做个铺垫)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值