Java-9.12 Interface Collection and Iterator
In this section, we will discuss Interface Collection and Iterator.
1. In the Collection and Map implementation classes, the Collection and Iterator interfaces are actually implemented.
package com.ray.ch09;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;public class Test {public static void showItems(Collection
collection) {for (Integer item : collection) {System.out.print(item + );}}public static void showItems(Iterator
iterator) {while (iterator.hasNext()) {Integer item = iterator.next();System.out.print(item + );}}public static void main(String[] args) {ArrayList
list = new ArrayList
();HashSet
set = new HashSet
();for (int i = 0; i < 10; i++) {list.add(i);set.add(i);}showItems(list);System.out.println();showItems(set);System.out.println();showItems(list.iterator());System.out.println();showItems(set.iterator());System.out.println();}}
From the code above, we can see that both list and set implement Collection and Iterator respectively, so we can call the methods in the above through the upward transformation.
2. When an object wants to reuse the Traversal method similar to showItem, it must implement Collection or Iterator, and it is easier to implement Iterator.
Collection:
package com.ray.ch09;import java.util.Collection;import java.util.Iterator;public class Test {}class Person implements Collection
{@Overridepublic int size() {// TODO Auto-generated method stubreturn 0;}@Overridepublic boolean isEmpty() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean contains(Object o) {// TODO Auto-generated method stubreturn false;}@Overridepublic Iterator
iterator() {// TODO Auto-generated method stubreturn null;}@Overridepublic Object[] toArray() {// TODO Auto-generated method stubreturn null;}@Overridepublic
T[] toArray(T[] a) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean add(Person e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean remove(Object o) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean containsAll(Collection
c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean addAll(Collection
c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean removeAll(Collection
c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean retainAll(Collection
c) {// TODO Auto-generated method stubreturn false;}@Overridepublic void clear() {// TODO Auto-generated method stub}}
As we can see from the instance code above, implementation is very troublesome and cannot achieve the effect of code reuse to provide development efficiency.
We know that there is a AbstractCollection that implements the above interface. Let's inherit it.
Package com. ray. ch09; import java. util. AbstractCollection; import java. util. Collection; import java. util. Iterator; public class Test {public static void showItems (Collection
Collection) {for (Person person: collection) {System. out. print (person. getId () +) ;}} public static void showItems (Iterator
Iterator) {while (iterator. hasNext () {Person person = iterator. next (); System. out. print (person. getId () +) ;}} public static void main (String [] args) {PersonList personList = new PersonList (); showItems (personList); System. out. println (); showItems (personList. iterator () ;}} class Person {private int id = 0; public int getId () {return id;} public void setId (int id) {this. id = id ;}} class PersonList extends AbstractCollection
{Private Person [] persons = new Person [10]; public PersonList () {for (int I = 0; I <persons. length; I ++) {Person person Person = new person (); Person. setId (I); persons [I] = person ;}@overridepublic Iterator
Iterator () {return new Iterator
() {Private int index = 0; @ Overridepublic boolean hasNext () {return index <persons. length ;}@ Overridepublic Person next () {return persons [index ++] ;}@ Overridepublic void remove () {// future implementation }};} @ Overridepublic int size () {return persons. length ;}}
The example we present is a little simpler, but you must note that there are many methods in inheriting this abstract class that need to be implemented by yourself, but they are not provided in it, just like in our example, you must initialize the array of persons by yourself, and do not use external add. Because of the implementation in the abstract class, the add operation throws an exception.
Iterator:
Iterator is much simpler and more elegant.
Package com. ray. ch09; import java. util. Iterator; public class Test {public static void showItems (Iterator
Iterator) {while (iterator. hasNext () {Person person = iterator. next (); System. out. print (person. getId () +) ;}} public static void main (String [] args) {PersonList personList = new PersonList (); showItems (personList. iterator () ;}} class Person {private int id = 0; public int getId () {return id;} public void setId (int id) {this. id = id ;}} class PersonList {private Person [] persons = new Person [10]; public PersonList () {for (int I = 0; I <persons. length; I ++) {Person person Person = new person (); Person. setId (I); persons [I] = person;} public Iterator
Iterator () {return new Iterator
() {Private int index = 0; @ Overridepublic boolean hasNext () {return index <persons. length ;}@ Overridepublic Person next () {return persons [index ++] ;}@ Overridepublic void remove () {// future implementation }};}}