集合的打印
必须使用 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()方法判断对象是否在集合中// trueSystem.out.println(appleList.contains(apple));// remove()方法删除集合中的对象appleList.remove(apple);// falseSystem.out.println(appleList.contains(apple));// indexOf()找到对象的下标号appleList.add(apple);// 0System.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);// 判断是否是子列表,不论顺序// trueSystem.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;}@Overridepublic 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》对你有帮助,请点赞、收藏,并留下你的观点哦!