集合的打印

必须使用 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 只能用来:

  1. 使用 iterator() 方法要求集合返回一个 Iterator。 Iterator 将准备好返回序列中的第一个元素。
  2. 使用next() 方法获得序列中的下一个元素。
  3. 使用 hasNext() 方法检查序列中是否还有元素。
  4. 使用 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

  1. ListIterator 是一个更强大的 Iterator 子类型,它只能由各种 List 类生成。
  2. Iterator 只能向前移动,而 ListIterator 可以双向移动
  3. 可以使用 set() 方法替换它访问过的最近一个元素。
  4. 通过调用 listIterator() 方法来生成指向 List 开头处的 ListIterator 。
  5. 可以通过调用 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的更多相关文章

  1. Map集合、散列表、红黑树介绍

    前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合: Collection总览 List集合就这么简单[源码剖析] 原本我是打算继续将Collection下的 ...

  2. python打印列表的下标和值的例子:

    python打印列表的下标和值的例子: In [1]: list01=[1,4,5] In [10]: def funct01(ll):   ....:     for index,value in ...

  3. 编写高质量代码改善C#程序的157个建议——建议30:使用LINQ取代集合中的比较器和迭代器

    建议30:使用LINQ取代集合中的比较器和迭代器 LINQ提供了类似于SQL的语法来实现遍历.筛选与投影集合的功能. static void Main(string[] args) { List< ...

  4. Redis底层探秘(四):整数集合及压缩列表

    整数集合 整数集合(intset)是集合键的底层实现之一,当一个集合只包含 整数值元素,并且这个集合的元素数量不多时,Redis就会使用郑书记和作为集合键的底层实现. 整数集合的实现 整数集合是red ...

  5. php实现从尾到头打印列表

    php实现从尾到头打印列表 一.总结 4.数组倒序:array_reverse() 5.函数肯定要return,而不是echo 二.php实现从尾到头打印列表 输入一个链表,从尾到头打印链表每个节点的 ...

  6. 数据绑定(五)使用集合对象作为列表控件的ItemsSource

    原文:数据绑定(五)使用集合对象作为列表控件的ItemsSource ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值,ItemsSource里存放的是一条一条 ...

  7. Redis 学习笔记(篇四):整数集合和压缩列表

    整数集合 Redis 中当一个集合(set)中只包含整数,并且元素不多时,底层使用整数集合实现,否则使用字典实现. 那么: 为什么会出现整数集合呢?都使用字典存储不行吗? 整数集合在 Redis 中的 ...

  8. Javase之集合体系之(1)集合顶层类Collection与其迭代器知识

    集合体系之集合顶层类Collection与其迭代器知识 集合的由来:Java是一门面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,就必须把多个对象进行存储,而要存 ...

  9. Redis数据结构—整数集合与压缩列表

    目录 Redis数据结构-整数集合与压缩列表 整数集合的实现 整数集合的升级 整数集合不支持降级 压缩列表的构成 压缩列表节点的构成 小结 Redis数据结构-整数集合与压缩列表 大家好,我是白泽.今 ...

随机推荐

  1. 【记录】如何造一个vite插件(2)

    上一篇已经把vite插件的基础结构搭建起来了,这一次就来聊聊继续完善开发环境. 完善开发环境 生成d.ts文件 先来修改一下lib/index.ts这个文件 export interface user ...

  2. ls仅列出当前目录下的所有目录

    ls -d */ -d仅列出目录本身,而不列出其中的内容 *通配符,所有的字符 /目录的标识

  3. GraphQL 概念入门

    GraphQL 概念入门 Restful is Great! But GraphQL is Better. -- My Humble Opinion. GraphQL will do to REST ...

  4. CentOS下配置Nginx实现动静分离实例

    测试环境: CentOS Linux release 7.6 PHP 7.2.32 两台服务器:192.168.1.109(Nginx),192.168.1.118(Apache) 1. 安装配置19 ...

  5. 原来ReadWriteLock也能开发高性能缓存,看完我也能和面试官好好聊聊了!

    大家好,我是冰河~~ 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在 ...

  6. P6106 [Ynoi2010] Self Adjusting Top Tree

    P6106 [Ynoi2010] Self Adjusting Top Tree 题意 给出平面直角坐标系上若干不与坐标轴平行的处于第一象限的互不相交的线段,多次询问平面中一个第一象限的矩形与这些线段 ...

  7. ifix重用性模块化开发纪实(以污水处理泵站为例)

    在经过多个自动化上位机的开发后,对上位机的重用开发和提高效率,减少重复工作有了一定的积累.故而产生了模块化建设上位机的思路.现从当下项目开始,研究出一套可重复利用的模块化系统. 1.点表整理 从PLC ...

  8. PHP 后台数组数据 传输给前台JS 使用

    一. PHP: $a = array('aaa','bbb'); $a= json_encode($a); JS: var a_json = {$a};   //此处a_json数组便可使用 二. H ...

  9. 比@EnableMongoAuditing功能强大的实现

    问题出现 以前通过@EnableMongoAuditing.@CreateDate.@LastModifiedDate进行实体类创建时间.修改时间的自动管理. 但为了实现多数据源的管理以及切换,自己覆 ...

  10. 开机时自动启动的AutoHotkey脚本

    ;;; 开机时自动启动的AutoHotkey脚本;; 此脚本修改时间 2019年06月18日20时48分;; 计时器创建代码段 ------------------------------------ ...