集合基础

接口

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.

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

List

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

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

抽象类

AbstractMap

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方法

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

  1. java 集合框架 List相关接口

    AbstractCollection 此类提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作. 还有两个抽象方法,具体的迭代器,具体的Collection 的大小 pu ...

  2. Java集合框架之四大接口、常用实现类

    Java集合框架 <Java集合框架的四大接口> Collection:存储无序的.不唯一的数据:其下有List和Set两大接口. List:存储有序的.不唯一的数据: Set:存储无序的 ...

  3. Java集合框架之List接口浅析

    Java集合框架之List接口浅析 一.List综述: 毫无疑问List接口位于java.util包下,继承自 Collection接口 存储元素的特点: 有序可重复(有序:即存进去是什么顺序,取出来 ...

  4. Java集合框架之Collection接口

    Java是一门面向对象的语言,那么我们写程序的时候最经常操作的便是对象了,为此,Java提供了一些专门用来处理对象的类库,这些类库的集合我们称之为集合框架.Java集合工具包位于Java.util包下 ...

  5. Java集合框架之Map接口浅析

    Java集合框架之Map接口浅析 一.Map接口综述: 1.1java.util.Map<k, v>简介 位于java.util包下的Map接口,是Java集合框架的重要成员,它是和Col ...

  6. Java集合框架之Set接口浅析

    Java集合框架之Set接口浅析 一.java.util.Set接口综述: 这里只对Set接口做一简单综述,其具体实现类的分析,朋友们可关注我后续的博文 1.1Set接口简介 java.util.se ...

  7. java 集合框架(二)Iterable接口

    Iterable接口是java 集合框架的顶级接口,实现此接口使集合对象可以通过迭代器遍历自身元素,我们可以看下它的成员方法 修饰符和返回值 方法名 描述 Iterator<T> iter ...

  8. Java集合框架中Map接口的使用

    在我们常用的Java集合框架接口中,除了前面说过的Collection接口以及他的根接口List接口和Set接口的使用,Map接口也是一个经常使用的接口,和Collection接口不同,Map接口并不 ...

  9. Java集合框架中List接口的简单使用

    Java集合框架可以简单的理解为一种放置对象的容器,和数学中的集合概念类似,Java中的集合可以存放一系列对象的引用,也可以看做是数组的提升,Java集合类是一种工具类,只有相同类型的对象引用才可以放 ...

随机推荐

  1. 3.使用nginx-ingress

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-06-25 13:54:15 星期二 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  2. 蓝桥杯——试题 算法训练 Sereja and Squares

    Java 代码 ```` import java.util.Scanner; public class Main { private static long num = 0; private stat ...

  3. Windows 10 Emoji shortcuts

    Windows 10 Emoji shortcuts Windows 10 Emoji 快捷方式 https://support.microsoft.com/en-us/windows/windows ...

  4. CSS Grid & Flex poster PDF 海报定制

    CSS Grid & Flex poster PDF 海报定制 CSS 手工实现 导出 SVG / PNG 导出 PDF 打印,定制海报 refs https://css-tricks.com ...

  5. ituring 挂了

    ituring 挂了 图灵社区 挂了 运行时错误 "/"应用程序中的服务器错误. 运行时错误 说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁止远程查看应用程序错 ...

  6. Open Collective

    Open Collective Open Collective is an online funding platform for open and transparent communities. ...

  7. 钉钉 & URL Scheme & Universal Link & Deep Link

    钉钉 & URL Scheme & Universal Link & Deep Link DD link https://www.cnblogs.com/xgqfrms/p/1 ...

  8. CSS transition & shorthand property order

    CSS transition & shorthand property order shorthand property https://developer.mozilla.org/en-US ...

  9. redux & connect

    redux & connect import React, { Component, // useState, // useEffect, } from 'react'; import { b ...

  10. 软件工程中的CI&CD

    wiki 在软件工程中,CI/CD或CICD通常是指持续集成以及持续交付或持续部署的组合实践 持续集成 在软件工程中,持续集成(CI)是每天将所有开发人员的工作副本合并到共享主线中的一种做法.[1] ...