zoukankan      html  css  js  c++  java
  • 设计模式-17-迭代器

    简介:提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。

    目的:

    总结:使用Iterator避免二次迭代

    组成:

    1,举例

    package com.design.q.iterator.standard;
    
    import java.util.Iterator;
    
    @SuppressWarnings("unchecked")
    public class ArrayList<E> implements Iterable<E>{
        
        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<E> iterator() {
            return new Ite();
        }
        
        
        private class Ite implements Iterator<E>{
            
            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 LinkedList<E> implements Iterable<E>{
    
        private Entry<E> header = new Entry<E>(null, null, null);
        private int size;
        
        public LinkedList() {
            header.next = header.previous = header;
        }
    
        /**
         * 集合新增元素
         * @param e
         */
        public void add(E e){
            Entry<E> newEntry = new Entry<E>(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> 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>{
            E value;
            Entry<E> previous;
            Entry<E> next;
            public Entry(E value, Entry<E> previous, Entry<E> next){
                super();
                this.value = value;
                this.previous = previous;
                this.next = next;
            }
        }
    
        @Override
        public Iterator<E> iterator() {
            return new Ite();
        }
        
        private class Ite implements Iterator<E>{
    
            Entry<E> 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 HashSet<E> implements Iterable<E>{
    
        private static final Object NULL = new Object();
        private Map<E, Object> 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<E> 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迭代
             */
            ArrayList<Integer> arrayList = new ArrayList<>();
            for(int i = 0; i < 10; i++){
                arrayList.add(i);
            }
            System.out.println("ArrayList 大小=" + arrayList.size());
            Iterator<Integer> arrayListIterator = arrayList.iterator();
            while(arrayListIterator.hasNext()){
                System.out.print(arrayListIterator.next() + " ");
            }
    //        for(Integer i : arrayList){
    //            System.out.print(i + " ");
    //        }
            
            /**
             * LinkedList迭代
             */
            LinkedList<Integer> linkedList = new LinkedList<>();
            for(int i = 0; i < 10; i ++){
                linkedList.add(i);
            }
            System.out.println("
    LinkedList 大小=" + linkedList.size());
            Iterator<Integer> linkedListIterator = linkedList.iterator();
            while(linkedListIterator.hasNext()){
                System.out.print(linkedListIterator.next() + " ");
            }
    //        for(Integer i : linkedList){
    //            System.out.print(i + " ");
    //        }
            
            /**
             * HashSet迭代
             */
            HashSet<Integer> hashSet = new HashSet<>();
            for(int i = 0; i < 10; i ++){
                hashSet.add(i);
            }
            System.out.println("
    HashSet 大小=" + hashSet.size());
            Iterator<Integer> hashSetIterator = hashSet.iterator();
            while(hashSetIterator.hasNext()){
                System.out.print(hashSetIterator.next() + " ");
            }
    //        for(Integer i : hashSet){
    //            System.out.print(i + " ");
    //        }
        }
    }

    3,Result

    ArrayList 大小=10
    0 1 2 3 4 5 6 7 8 9 
    LinkedList 大小=10
    0 1 2 3 4 5 6 7 8 9 
    HashSet 大小=10
    0 1 2 3 4 5 6 7 8 9 
  • 相关阅读:
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
    mybatis自定义枚举转换类
    javaweb分布式事务
    javaweb的负载均衡,tomcat集群和session共享
    分布式并发锁处理
    FindBugs规则整理
    SpringMVC中文乱码,字符过滤器配置
    mybatis快速入门
    黑盒测试常用的测试方法
    问题及解决方案小技巧
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/7246410.html
Copyright © 2011-2022 走看看