Java基础

Java集合框架概述

2018-07-20  本文已影响13人  黄金矿工00七

集合框架使我们开发应用时不可缺少的工具,首先我们要明白,集合是什么?
集合代表了一组具有共同属性的对象,集合框架则定义了一套规范,用来表示、操作集合,使具体操作与实现细节解耦,为我们提供了囊括全部集合接口、实现和算法。

 * The root interface in the <i>collection hierarchy</i>.  A collection
 * represents a group of objects, known as its <i>elements</i>.  Some
 * collections allow duplicate elements and others do not.  Some are ordered
 * and others unordered.  The JDK does not provide any <i>direct</i>
 * implementations of this interface: it provides implementations of more
 * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
 * is typically used to pass collections around and manipulate them where
 * maximum generality is desired.
 *
 * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
 * duplicate elements) should implement this interface directly

集合框架的根接口,集合是一组对象的表示(对象叫做元素),一些集合实现允许重复元素,一些不允许,一些有序......JDK没有提供这个接口的任何直接实现,而是提供了更多细节性的接口(也就是具体化的接口,例如List),这个接口传递集合类给Collections(集合操作的工具类),用来为各种具体的集合提供最大化的统一操作方式。

  default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }
  default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
  default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);

Map并不是一个真正意义上的集合(are not true collections),但是这个接口提供了三种“集合视角”(collection views ),使得可以像操作集合一样操作它们,具体如下:
- key的集合: map's contents to be viewed as a set of keys
- value的集合:map’s contents to be viewed as a collection of value
- key-value映射(Entry)的集合:map’s contents to be viewed as a set of key-value mappings

上一篇下一篇

猜你喜欢

热点阅读