集合的打印、列表List、迭代器Iterators
集合的打印

必须使用 Arrays.toString() 来生成数组的可打印形式。
但是打印集合无需任何帮助。
/**
* 集合的打印
* @author myf
*/
public class PrintingCollections {
public static void main(String[] args) {
Collection<Integer> collection = new ArrayList<>();
// Collections.addAll()接收一个Collection对象,以及一个数组或是逗号分隔的列表
Collections.addAll(collection, 1,2,3,4);
System.out.println(collection);
//[1, 2, 3, 4]
}
}
List包含ArrayList和LinkedList。
Set包含HashSet、TreeSet和LinkedHashSet。
Map包含HashMap、TreeMap和LinkedHashMap。
列表List

List承诺将元素保存在特定的序列中。List接口在Collection的基础上添加了许多方法,允许在List的中建插入和删除元素。
有两种类型的List:
- 基本的ArrayList,擅长随机访问元素,但在List中间插入和删除元素时速度较慢。
- LinkedList,它通过较低的代价在List中间进行插入和删除操作,提供了优化的顺序访问。linkedList对于随机访问来说相对较慢,但它具有比ArrayList更大的特征集。
/**
* @author myf
*/
public class ListFeatures {
public static void main(String[] args) {
List<Fruits> appleList = new ArrayList<>();
Fruits apple = new Fruits("apple");
appleList.add(apple);
// contains()方法判断对象是否在集合中
// true
System.out.println(appleList.contains(apple));
// remove()方法删除集合中的对象
appleList.remove(apple);
// false
System.out.println(appleList.contains(apple));
// indexOf()找到对象的下标号
appleList.add(apple);
// 0
System.out.println(appleList.indexOf(apple));
appleList.add(new Fruits("orange"));
appleList.add(new Fruits("banana"));
// subList从大列表中切片
List<Fruits> fruitsArrayList = appleList.subList(0,2);
// [Fruits{name='apple'}, Fruits{name='orange'}, Fruits{name='banana'}]
System.out.println(appleList);
// [Fruits{name='orange'}, Fruits{name='apple'}]
// fruitsArrayList.add(appleList.get(0));
System.out.println(fruitsArrayList);
// 判断是否是子列表,不论顺序
// true
System.out.println(appleList.containsAll(fruitsArrayList));
// “集合交集”操作
// appleList.retainAll(fruitsArrayList);
// System.out.println(appleList);
// [Fruits{name='apple'}, Fruits{name='orange'}]
fruitsArrayList.retainAll(appleList);
System.out.println(fruitsArrayList);
// 移除元素操作
appleList.removeAll(fruitsArrayList);
// [Fruits{name='banana'}]
System.out.println(appleList);
// 替换元素
appleList.set(0,new Fruits("moyifeng"));
// [Fruits{name='moyifeng'}]
System.out.println(appleList);
}
}
class Fruits {
String name;
public Fruits(String name) {
this.name = name;
}
@Override
public String toString() {
return "Fruits{" +
"name='" + name + '\'' +
'}';
}
}
迭代器Iterators

迭代器是一个对象,它在一个序列中移动并选择该序列中的每个对象,而客户端程序员不知道或不关心该序列的底层结构。另外,迭代器通常被称为轻量级对象(lightweight object):创建它的代价小。因此,经常可以看到一些对迭代器有些奇怪的约束。例如,Java 的 Iterator 只能单向移动。这个 Iterator 只能用来:
- 使用 iterator() 方法要求集合返回一个 Iterator。 Iterator 将准备好返回序列中的第一个元素。
- 使用next() 方法获得序列中的下一个元素。
- 使用 hasNext() 方法检查序列中是否还有元素。
- 使用 remove()方法将迭代器最近返回的那个元素删除。
/**
* @author myf
*/
public class SimpleIteration {
public static void main(String[] args) {
List<Fruits> fruitsList = new ArrayList<>();
fruitsList.add(new Fruits("test"));
fruitsList.add(new Fruits("apple"));
fruitsList.add(new Fruits("orange"));
fruitsList.add(new Fruits("banana"));
Iterator<Fruits> iterator = fruitsList.iterator();
iterator.next();
iterator.remove();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println(fruitsList.size());
}
}
ListIterator

- ListIterator 是一个更强大的 Iterator 子类型,它只能由各种 List 类生成。
- Iterator 只能向前移动,而 ListIterator 可以双向移动
- 可以使用 set() 方法替换它访问过的最近一个元素。
- 通过调用 listIterator() 方法来生成指向 List 开头处的 ListIterator 。
- 可以通过调用 listIterator(n) 创建一个一开始就指向列表索引号为 n 的元素处的 ListIterator 。
/**
* @author myf
*/
public class ListIteration {
public static void main(String[] args) {
List<Fruits> fruitsList = new ArrayList<>();
fruitsList.add(new Fruits("test"));
fruitsList.add(new Fruits("apple"));
fruitsList.add(new Fruits("orange"));
fruitsList.add(new Fruits("banana"));
ListIterator<Fruits> listIterator = fruitsList.listIterator();
while (listIterator.hasNext()){
System.out.print(listIterator.nextIndex());
System.out.println(listIterator.next());
}
ListIterator<Fruits> fruitsListIterator = fruitsList.listIterator(4);
while (fruitsListIterator.hasPrevious()){
System.out.print(fruitsListIterator.previousIndex());
System.out.println(fruitsListIterator.previous());
}
}
}
集合的打印、列表List、迭代器Iterators的更多相关文章
- Map集合、散列表、红黑树介绍
前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合: Collection总览 List集合就这么简单[源码剖析] 原本我是打算继续将Collection下的 ...
- python打印列表的下标和值的例子:
python打印列表的下标和值的例子: In [1]: list01=[1,4,5] In [10]: def funct01(ll): ....: for index,value in ...
- 编写高质量代码改善C#程序的157个建议——建议30:使用LINQ取代集合中的比较器和迭代器
建议30:使用LINQ取代集合中的比较器和迭代器 LINQ提供了类似于SQL的语法来实现遍历.筛选与投影集合的功能. static void Main(string[] args) { List< ...
- Redis底层探秘(四):整数集合及压缩列表
整数集合 整数集合(intset)是集合键的底层实现之一,当一个集合只包含 整数值元素,并且这个集合的元素数量不多时,Redis就会使用郑书记和作为集合键的底层实现. 整数集合的实现 整数集合是red ...
- php实现从尾到头打印列表
php实现从尾到头打印列表 一.总结 4.数组倒序:array_reverse() 5.函数肯定要return,而不是echo 二.php实现从尾到头打印列表 输入一个链表,从尾到头打印链表每个节点的 ...
- 数据绑定(五)使用集合对象作为列表控件的ItemsSource
原文:数据绑定(五)使用集合对象作为列表控件的ItemsSource ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值,ItemsSource里存放的是一条一条 ...
- Redis 学习笔记(篇四):整数集合和压缩列表
整数集合 Redis 中当一个集合(set)中只包含整数,并且元素不多时,底层使用整数集合实现,否则使用字典实现. 那么: 为什么会出现整数集合呢?都使用字典存储不行吗? 整数集合在 Redis 中的 ...
- Javase之集合体系之(1)集合顶层类Collection与其迭代器知识
集合体系之集合顶层类Collection与其迭代器知识 集合的由来:Java是一门面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,就必须把多个对象进行存储,而要存 ...
- Redis数据结构—整数集合与压缩列表
目录 Redis数据结构-整数集合与压缩列表 整数集合的实现 整数集合的升级 整数集合不支持降级 压缩列表的构成 压缩列表节点的构成 小结 Redis数据结构-整数集合与压缩列表 大家好,我是白泽.今 ...
随机推荐
- 基于SSM酒店管理系统mysql版本(前后台)
介绍:spring,springmvc,mybatis,mysql,eclipse 截图: 数据库表:CREATE TABLE `account` ( `id` int(11) NOT NULL AU ...
- 痞子衡嵌入式:i.MXRT1010, 1170型号上不一样的SNVS GPR寄存器读写控制设计
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1010, 1170型号上不一样的SNVS GPR寄存器读写控制设计. 痞子衡之前两篇文章 <在SBL项目实战中妙用i ...
- homeless靶机
仅供个人娱乐 靶机信息 下载地址:https://www.vulnhub.com/entry/homeless-1,215/ 一.主机扫描 二.信息收集 在网页源码和页面上,我们发现User-Agen ...
- 面试官:展开说说,Spring中Bean对象是如何通过注解注入的?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 章节目录(手写Spring,让你了解更多) [x] 第 01 章:开篇介绍,我要带你撸 Spr ...
- Python - 解包的各种骚操作
为什么要讲解包 因为我觉得解包是 Python 的一大特性,大大提升了编程的效率,而且适用性很广 啥是解包 个人通俗理解:解开包袱,拿出东西 正确理解:将元素从可迭代对象中一个个取出来 python ...
- Spring Cloud分区发布实践(6)--灰度服务-根据Header选择实例区域
此文是一个完整的例子, 包含可运行起来的源码. 此例子包含以下部分: 网关层实现自定义LoadBalancer, 根据Header选取实例 服务中的Feign使用拦截器, 读取Header Feign ...
- 自学linux——2.认识目录及常用指(命)令
认识目录及常用指(命)令 1.备份: 快照(还原精灵):短期备份 频繁备份 可关可开.可能会影响系统的操作. 备份时:虚拟机--快照 还原时:虚拟机--快照--快照管理器--相应位置--转到 克隆 ...
- Prometheus MySQL监控+grafana展示
前言 最近爱上了研究各种杂七杂八的技术. Prometheus是现如今最火的监控软件之一.做为一个运维DBA,不会这个可就OUT了. 本篇博客,演示一下prometheus之通过mysql expor ...
- Java面向对象14——接口
接口 package oop.demon01.demon09; //抽象思维~Java //interface 定义的关键字 , 接口都需要有实现类 public interface Use ...
- SpringBoot 如何进行参数校验,老鸟们都这么玩的!
大家好,我是飘渺. 前几天写了一篇 SpringBoot如何统一后端返回格式?老鸟们都是这样玩的! 阅读效果还不错,而且被很多号主都转载过,今天我们继续第二篇,来聊聊在SprinBoot中如何集成参数 ...