Java Iterator 迭代器操作合集
8.8. Iterator 迭代器
Iterator接口包含四个方法:hasNext,next,remove,forEachRemaining
8.8.1. List 迭代
package cn.netkiller.test; import java.util.Iterator; import java.util.List; public class Test { public static void main(String[] args) { Test test = new Test(); // ArrayList<String> arrayList = new ArrayList<String>(); List<String> list = List.of("Neo", "Tom", "Jerry"); System.out.printf("%s for %s\n", "=".repeat(10), "=".repeat(10)); for (String name : list) { System.out.println(name); } System.out.printf("%s forEach %s\n", "=".repeat(10), "=".repeat(10)); list.forEach(obj -> { System.out.println(obj); }); Iterator<String> iterator = list.iterator(); System.out.printf("%s while %s\n", "=".repeat(10), "=".repeat(10)); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.printf("%s forEachRemaining %s\n", "=".repeat(10), "=".repeat(10)); iterator.forEachRemaining(System.out::println); } }
8.8.2. 处理剩余结果
iterator.forEachRemaining(System.out::println);
8.8.3. Map 迭代
package cn.netkiller.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Test { public sta