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); };
}
- 只接受一个参数
- 没有返回值
Collection
The root interface in the collection hierarchy.
- 是集合层次中的根接口
- jdk中并没有直接实现这个接口,而是由collection派生出特定子接口(例如set list),这个接口通常用于传递集合并操作他们,具有最大通用性
- 无序的集合如
bag或者multisets应该直接实现这个接口 - 对于不直接实现这个接口而是实现其子接口的集合类,构造方法应该满足
- 有一个空的构造方法
- 有一个可以传入集合的构造方法
- 可以有序,可以无序,可以重复,可以不重复
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.
- 实现list接口的集合必须有序,是否为空由具体list决定
- 可以通过整形的索引来查找和访问集合中的元素
- 继承了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./
- 标记接口,内容为空,只是说明访问时可以采取随机访问
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.
*/
- 是否有序由其子类决定,如
TreeMap有序,HashMap无序,是针对于值的view而言 - 对于不直接实现这个接口而是实现其子接口的map类,构造方法应该满足
- 有一个空的构造方法
- 有一个可以传入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.
- 提供了Map接口的框架实现
- 对于不可修改的map,继承该类并实现entrySet方法
- 对于可修改的map,除了继承该类,还要额外重写put方法,iterator也要实现remove方法
java集合框架部分相关接口与类的介绍的更多相关文章
- java 集合框架 List相关接口
AbstractCollection 此类提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作. 还有两个抽象方法,具体的迭代器,具体的Collection 的大小 pu ...
- Java集合框架之四大接口、常用实现类
Java集合框架 <Java集合框架的四大接口> Collection:存储无序的.不唯一的数据:其下有List和Set两大接口. List:存储有序的.不唯一的数据: Set:存储无序的 ...
- Java集合框架之List接口浅析
Java集合框架之List接口浅析 一.List综述: 毫无疑问List接口位于java.util包下,继承自 Collection接口 存储元素的特点: 有序可重复(有序:即存进去是什么顺序,取出来 ...
- Java集合框架之Collection接口
Java是一门面向对象的语言,那么我们写程序的时候最经常操作的便是对象了,为此,Java提供了一些专门用来处理对象的类库,这些类库的集合我们称之为集合框架.Java集合工具包位于Java.util包下 ...
- Java集合框架之Map接口浅析
Java集合框架之Map接口浅析 一.Map接口综述: 1.1java.util.Map<k, v>简介 位于java.util包下的Map接口,是Java集合框架的重要成员,它是和Col ...
- Java集合框架之Set接口浅析
Java集合框架之Set接口浅析 一.java.util.Set接口综述: 这里只对Set接口做一简单综述,其具体实现类的分析,朋友们可关注我后续的博文 1.1Set接口简介 java.util.se ...
- java 集合框架(二)Iterable接口
Iterable接口是java 集合框架的顶级接口,实现此接口使集合对象可以通过迭代器遍历自身元素,我们可以看下它的成员方法 修饰符和返回值 方法名 描述 Iterator<T> iter ...
- Java集合框架中Map接口的使用
在我们常用的Java集合框架接口中,除了前面说过的Collection接口以及他的根接口List接口和Set接口的使用,Map接口也是一个经常使用的接口,和Collection接口不同,Map接口并不 ...
- Java集合框架中List接口的简单使用
Java集合框架可以简单的理解为一种放置对象的容器,和数学中的集合概念类似,Java中的集合可以存放一系列对象的引用,也可以看做是数组的提升,Java集合类是一种工具类,只有相同类型的对象引用才可以放 ...
随机推荐
- HDU 3341 Lost's revenge (AC自动机 + DP + 变进制/hash)题解
题意:给你些分数串,给你一个主串,主串每出现一个分数串加一分,要你重新排列主串,最多几分 思路:显然这里开$40^4$去状压内存不够.但是我们自己想想会发现根本不用开那么大,因为很多状态是废状压,不是 ...
- python阿里云api查询域名是否可以注册(CheckDomain)
import requests from fun import * from urllib import parse url ='http://domain.aliyuncs.com/?' acces ...
- CF 1477A. Nezzar and Board
传送门 思路: 从k = 2 * x - y ==> 2 * x = k + y ,可以看出x是k,y的中间值,则如果存在x1,x2,且x1 = x2 ± 1,则通过x1,x2可以得到所有整数, ...
- hihoCoder Challenge 1
#1034 : 毁灭者问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在 Warcraft III 之冰封王座中,毁灭者是不死族打三本后期时的一个魔法飞行单位. 毁 ...
- TypeScript & LeetCode
TypeScript & LeetCode TypeScript In Action TypeScript 复杂类型 编写复杂的 TypeScript 类型 // 方法「只可能」有两种类型签名 ...
- trao 模拟点击 & js auto click
trao 模拟点击 & js auto click 日历上选择某一天,在 scrollview 自动定位到选择的那一天 click 后获取 item 的 e.target.offsetLeft ...
- flask启动常见问题1:sqlalchemy.exc.ArgumentError: Mapper mapped class UserCode->data_system_user_email could not assemble any primary key columns for mapped table 'data_system_user_email'
我的描述:当我编辑好flask以后,ORM映射数据库完成,启动项目时,发生现象: 解决: 看字面的意思是主键导致的错误,于是我查看了data_system_user_email的键参数配置,发现表没有 ...
- hadoop环境搭建:高可用
目录 1.硬件配置 2.软件版本 3.准备工作 3.1.配置网络环境 3.2.安装JDK 3.3.安装ZOOKEEPER 4.安装Hadoop 5.启动 6.问题 7.配置文件 1.硬件配置 采用3台 ...
- Redis6.0.9主从搭建
所谓主从,大家都知道主是写数据,而从是进行数据的拷贝. 1:配置 主节点 127.0.0.1 6379 从节点 127.0.0.1 6378 先将单机版的配置文件赋值两份出来,原先的配置中主要改动有: ...
- Kubernetes: NGINX/PHP-FPM 502错误和优雅结束
我们有一个运行在Kubernetes上的PHP应用,每个POD由两个独立的容器组成 - Nginx和PHP-FPM. 在我们对应用进行缩容时,遇到了502错误,例如,当一个POD在结束中时,POD里面 ...





