從頭認識java-9.12 介面Collection與Iterator
這一章節我們來討論一下介面Collection與Iterator。
1.在Collection與Map的實作類別裡面,其實都實現了Collection與Iterator介面。
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();}}
從上面的代碼都可以看見,list和set都分別實現了Collection與Iterator,因此可以通過向上轉型來調用裡面的方法。
2.當我們有一個對象想複用類似showItem這種遍曆方法的時候,他必須實現Collection或Iterator,而且實現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}}
上面的執行個體代碼我們可以看見,實現起來是非常麻煩的一件事情,達不到代碼重用所應有的提供開發效率的效果。
我們知道有一個AbstractCollection實現了上面的介面,我們來繼承它看看。
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 = 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() {// 以後會展開實現}};}@Overridepublic int size() {return persons.length;}}
相對來說我們展現的例子就簡單了一些,但是需要注意,繼承了這個抽象類別其實裡面有很多方法是需要自己實現,只不過他裡面沒有給出來而已,就像我們的例子,必須自己初始化person的數組,不能使用外部add,因為在抽象類別裡面的實現,add是拋異常的。
Iterator:
相對來說Iterator就簡單優雅多了。
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 = 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() {// 以後會展開實現}};}}