java集合框架部分相关接口与类的介绍

集合基础

接口

Iterable

//Implementing this interface allows an object to be the target of the "for-each loop" statement.
//Iterator其实是一个接口(迭代器)
Iterator<T> iterator();

default void forEach(Consumer<? super T> action) {//传一个实现了Consumer接口的子类实例
Objects.requireNonNull(action);
for (T t : this) {
   action.accept(t);
}
}
//以ArrayList为例,lambda表达式
objects.forEach(o -> System.out.println(o));
//匿名类
objects.forEach(new Consumer<Object>() {
      @Override
      public void accept(Object o) {
          System.out.println(o);
      }
  });
//方法引用
objects.forEach(System.out::println);

实现这个接口的集合可以使用foreach方法进行循环

Iterator

迭代器 替代了Enumeration
Iterator允许调用者在迭代期间从底层集合中删除元素,并具有明确定义的语义。

主要方法

boolean hasNext();
E next();
default void remove() {
        throw new UnsupportedOperationException("remove");
    }
default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }    

Consumer

Represents an operation that accepts a single input argument and returns no result. 
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
     Objects.requireNonNull(after);
     return (T t) -> { accept(t); after.accept(t); };
 }
  1. 只接受一个参数
  2. 没有返回值

Collection

The root interface in the collection hierarchy.

image-20201118180330028

  1. 是集合层次中的根接口
  2. jdk中并没有直接实现这个接口,而是由collection派生出特定子接口(例如set list),这个接口通常用于传递集合并操作他们,具有最大通用性
  3. 无序的集合如bag或者multisets应该直接实现这个接口
  4. 对于不直接实现这个接口而是实现其子接口的集合类,构造方法应该满足
    1. 有一个空的构造方法
    2. 有一个可以传入集合的构造方法
  5. 可以有序,可以无序,可以重复,可以不重复

List

image-20201118194007558

Lists that support this operation may place limitations on what
* elements may be added to this list.  In particular, some
* lists will refuse to add null elements, and others will impose
* restrictions on the type of elements that may be added. 
  1. 实现list接口的集合必须有序,是否为空由具体list决定
  2. 可以通过整形的索引来查找和访问集合中的元素
  3. 继承了collection接口

Set

image-20201121112011464image-20201121112046417

Cloneable

Serializable

RandomAccess

/**
 * Marker interface used by <tt>List</tt> implementations to indicate that
 * they support fast (generally constant time) random access.  The primary
 * purpose of this interface is to allow generic algorithms to alter their
 * behavior to provide good performance when applied to either random or
 * sequential access lists./
  1. 标记接口,内容为空,只是说明访问时可以采取随机访问

Map

/*
1. A map cannot contain duplicate keys;each key can map to at most one value.
2. The Map interface provides three collection views, which allow a map's contents to be viewed as 
		a set of keys,键
		collection of values,值
		or set of key-value mappings.键值对
3. the order of a map is defined as the order in which the iterators on the map's collection views return their elements. 
4. All general-purpose map implementation classes should provide two "standard" constructors: a void (no arguments) constructor which creates an empty map, and a constructor with a single argument of type Map,which creates a new map with the same key-value mappings as its argument.
*/
  1. 是否有序由其子类决定,如TreeMap有序,HashMap无序,是针对于值的view而言
  2. 对于不直接实现这个接口而是实现其子接口的map类,构造方法应该满足
    1. 有一个空的构造方法
    2. 有一个可以传入Map的构造方法

AbstractCollection

抽象类

AbstractList

抽象类

image-20201118170712314

AbstractMap

image-20201121162925807

1. This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
2. To implement an unmodifiable map, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the map's mappings.  Typically, the returned set will, in turn, be implemented atop AbstractSet  This set should not support the add or remove methods, and its iterator should not support the remove method.
3. To implement a modifiable map, the programmer must additionally override this class's put method (which otherwise throws an UnsupportedOperationException), and the iterator returned by entrySet().iterator() must additionally implement its remove method.
  1. 提供了Map接口的框架实现
  2. 对于不可修改的map,继承该类并实现entrySet方法
  3. 对于可修改的map,除了继承该类,还要额外重写put方法,iterator也要实现remove方法
posted @ 2020-12-07 17:41  茕祇  阅读(45)  评论(0编辑  收藏  举报