Collection是描述全部 序列容器的共性的根接口,它能够被认为是一个"附属接口",即由于要表示其余若干个接口的共性而出现的接口,另外,java.uitl.AbstaractCollection类提供了Collection的默认实现,使得你能够建立AbstractCollection的子类型,而其中没有没必要要的重复java
使用接口描述的一个理由是它可使咱们可以建立更通用的代码,经过针对接口而非具体实现来编写代码,咱们的代码能够应用于任何实现了Collection的类--这也就使得一个新类能够选择去实现Collection接口,以便咱们可使用它ui
package object; //: holding/InterfaceVsIterator.java import typeinfo.pets.*; import java.util.*; public class InterfaceVsIterator { public static void display(Iterator<Pet> it) { while(it.hasNext()) { Pet p = it.next(); System.out.print(p.id() + ":" + p + " "); } System.out.println(); } public static void display(Collection<Pet> pets) { for(Pet p : pets) System.out.print(p.id() + ":" + p + " "); System.out.println(); } public static void main(String[] args) { List<Pet> petList = Pets.arrayList(8); Set<Pet> petSet = new HashSet<Pet>(petList); Map<String,Pet> petMap = new LinkedHashMap<String,Pet>(); String[] names = ("Ralph, Eric, Robin, Lacey, " + "Britney, Sam, Spot, Fluffy").split(", "); for(int i = 0; i < names.length; i++) petMap.put(names[i], petList.get(i)); display(petList); display(petSet); display(petList.iterator()); display(petSet.iterator()); System.out.println(petMap); System.out.println(petMap.keySet()); display(petMap.values()); display(petMap.values().iterator()); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat {Ralph=Rat, Eric=Manx, Robin=Cymric, Lacey=Mutt, Britney=Pug, Sam=Cymric, Spot=Pug, Fluffy=Manx} [Ralph, Eric, Robin, Lacey, Britney, Sam, Spot, Fluffy] 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx *///:~
当你要实现一个不是Collection的外部类时,因为让它去实现Collection接口可能很是困难,所以使用Iterator就会变得很是吸引人,实现Collection就必须实现iterator(),而且只拿iterator()与继承AbstaractCollection相比花费的代价略微减少spa
package object; //: holding/CollectionSequence.java import typeinfo.pets.*; import java.util.*; public class CollectionSequence extends AbstractCollection<Pet> { private Pet[] pets = Pets.createArray(8); public int size() { return pets.length; } public Iterator<Pet> iterator() { return new Iterator<Pet>() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented //remove()方法时一个可选操做,能够不实现 throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { CollectionSequence c = new CollectionSequence(); InterfaceVsIterator.display(c);//ColectionSequence经过继承抽象Collection实现了功能 InterfaceVsIterator.display(c.iterator()); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx *///:~
若是你的类已经继承类其余的类,那么你就不能再继承AbstractCollection了,再这种状况下,要实现Collection就必须实现该接口中的全部方法,此时,继承并提供建立迭代器的能力就会显得容易的多了,生成Iterator是将队列与消费队列的方法链接再一块儿耦合度最小的方式,而且与实现Collection相比,它在序列类上所施加的约束也少的多code
package object; //: holding/NonCollectionSequence.java import typeinfo.pets.*; import java.util.*; class PetSequence { protected Pet[] pets = Pets.createArray(8); } public class NonCollectionSequence extends PetSequence { public Iterator<Pet> iterator() { return new Iterator<Pet>() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { NonCollectionSequence nc = new NonCollectionSequence(); InterfaceVsIterator.display(nc.iterator()); //NonCollectionSequence经过实现Iterator匿名类实现了Iterator } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx *///:~