简介:提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
目的:
总结:使用Iterator避免二次迭代
组成:
1,举例
package com.design.q.iterator.standard;import java.util.Iterator;@SuppressWarnings("unchecked")public class ArrayListimplements Iterable { private E[] array = (E[]) new Object[10]; //集合 private static final int INCREMENT = 10; //集合扩展大小 private int size = 0; //集合实时大小 /** * 集合元素添加 * @param e */ public void add(E e){ if(size < array.length){ array[size ++] = e; }else{ //集合大小不足时进行扩容 E[] copy = (E[]) new Object[array.length + INCREMENT]; System.arraycopy(array, 0, copy, 0, size); copy[size ++] = e; array = copy; } } /** * 集合数组化 * @return */ public Object[] toArray(){ Object[] copy = new Object[size]; System.arraycopy(array, 0, copy, 0, size); return copy; } /** * 返回集合大小 * @return */ public int size(){ return size; } @Override public Iterator iterator() { return new Ite(); } private class Ite implements Iterator { int index = 0; @Override public boolean hasNext() { return index != size(); } @Override public E next() { return array[index ++]; } public void remove(){ } } }
LinkedList
package com.design.q.iterator.standard;import java.util.Iterator;public class LinkedListimplements Iterable { private Entry header = new Entry (null, null, null); private int size; public LinkedList() { header.next = header.previous = header; } /** * 集合新增元素 * @param e */ public void add(E e){ Entry newEntry = new Entry (e, header, header.next); newEntry.previous.next = newEntry; newEntry.next.previous = newEntry; size ++; } /** * 集合数组化 * @return */ public Object[] toArray(){ Object[] result = new Object[size]; int i = size - 1; for(Entry e = header.next; e != header; e = e.next){ result[i --] = e.value; } return result; } /** * 返回集合大小 * @return */ public int size(){ return size; } private static class Entry { E value; Entry previous; Entry next; public Entry(E value, Entry previous, Entry next){ super(); this.value = value; this.previous = previous; this.next = next; } } @Override public Iterator iterator() { return new Ite(); } private class Ite implements Iterator { Entry index = header; @Override public boolean hasNext() { return index.previous != header; } @Override public E next() { E e = index.previous.value; index = index.previous; return e; } @Override public void remove() { // TODO Auto-generated method stub } }}
HashSet
package com.design.q.iterator.standard;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HashSetimplements Iterable { private static final Object NULL = new Object(); private Map map = new HashMap<>(); /** * 集合元素新增 * @param e */ public void add(E e){ map.put(e, NULL); } /** * 集合数组化 * @return */ public Object[] toArray(){ return map.keySet().toArray(); } /** * 集合大小 * @return */ public int size(){ return map.size(); } @Override public Iterator iterator() { return map.keySet().iterator(); }}
2,Main
package com.design.q.iterator.standard;import java.util.Iterator;/** * 17-迭代器模式 * * 使用Iterator避免二次迭代 */public class MainTest { public static void main(String[] args) { /** * ArrayList迭代 */ ArrayListarrayList = new ArrayList<>(); for(int i = 0; i < 10; i++){ arrayList.add(i); } System.out.println("ArrayList 大小=" + arrayList.size()); Iterator arrayListIterator = arrayList.iterator(); while(arrayListIterator.hasNext()){ System.out.print(arrayListIterator.next() + " "); }// for(Integer i : arrayList){// System.out.print(i + " ");// } /** * LinkedList迭代 */ LinkedList linkedList = new LinkedList<>(); for(int i = 0; i < 10; i ++){ linkedList.add(i); } System.out.println("\nLinkedList 大小=" + linkedList.size()); Iterator linkedListIterator = linkedList.iterator(); while(linkedListIterator.hasNext()){ System.out.print(linkedListIterator.next() + " "); }// for(Integer i : linkedList){// System.out.print(i + " ");// } /** * HashSet迭代 */ HashSet hashSet = new HashSet<>(); for(int i = 0; i < 10; i ++){ hashSet.add(i); } System.out.println("\nHashSet 大小=" + hashSet.size()); Iterator hashSetIterator = hashSet.iterator(); while(hashSetIterator.hasNext()){ System.out.print(hashSetIterator.next() + " "); }// for(Integer i : hashSet){// System.out.print(i + " ");// } }}
3,Result
ArrayList 大小=100 1 2 3 4 5 6 7 8 9 LinkedList 大小=100 1 2 3 4 5 6 7 8 9 HashSet 大小=100 1 2 3 4 5 6 7 8 9