发布:2023/12/7 15:41:17作者:大数据 来源:大数据 浏览次数:503
1 2 3 4 5 6 7 8 9 10 |
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span> <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">E</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span> <span class="token keyword">boolean</span> <span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">E</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Iterable
只是返回了Iterator
接口的一个实例,这里很是奇怪,为什么不把两个接口合二为一,直接在Iterable
里面定义hasNext()
,next()
等方法呢?
原因是实现了Iterable
的类可以在实现多个Iterator
内部类,例如LinkedList
中的ListItr
和DescendingIterator
两个内部类,就分别实现了双向遍历和逆序遍历。通过返回不同的Iterator
实现不同的遍历方式,这样更加灵活。如果把两个接口合并,就没法返回不同的Iterator
实现类了。
上个代码,直观地展示一下实现了Iterable
的类如何通过返回不同的Iterator
从而实现不同的遍历方式。MutilIterator实现了三种迭代器,分别是默认的前向迭代器,反向迭代器和随机迭代器。主函数中分别调用了三种迭代器进行遍历。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MutilIterator</span> <span class="token keyword">implements</span> <span class="token class-name">Iterable</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> words <span class="token operator">=</span> <span class="token string">"May I get offers this summer."</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//默认的迭代器,前向遍历</span> <span class="token keyword">public</span> Iterator<span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> iterator() </span><span class="token punctuation">{</span> <span class="token comment">//匿名内部类</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Iterator</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text">() </span><span class="token punctuation">{</span> <span class="token keyword">private</span> int index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token builtin">boolean</span> <span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> index <span class="token operator"><</span> words<span class="token punctuation">.</span>length<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> words<span class="token punctuation">[</span>index<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Not implemented</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">UnsupportedOperationException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token plain-text">; } //反向迭代器 public Iterable</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> reverseIterator() </span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Iterable</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text">() </span><span class="token punctuation">{</span> @Override <span class="token keyword">public</span> Iterator<span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> iterator() </span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Iterator</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text">() </span><span class="token punctuation">{</span> <span class="token keyword">private</span> int index <span class="token operator">=</span> words<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token builtin">boolean</span> <span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> index <span class="token operator">></span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> words<span class="token punctuation">[</span>index<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Not implemented</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">UnsupportedOperationException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token plain-text">; } }; } //随机迭代器,注意这里不是创建一个新的Iterator,而是返回了一个打乱的List中的迭代器 public Iterable</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> randomized() </span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Iterable</span><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text">() </span><span class="token punctuation">{</span> <span class="token keyword">public</span> Iterator<span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> iterator() </span><span class="token punctuation">{</span> List<span class="token tag"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token plain-text"> shuffled = new ArrayList</span><span class="token tag"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token plain-text">(Arrays.asList(words)); Collections.shuffle(shuffled, new Random(47)); return shuffled.iterator(); } }; } public static void main(String[] args) </span><span class="token punctuation">{</span> MutilIterator mi <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MutilIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//默认的迭代器</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String String <span class="token punctuation">:</span> mi<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>String <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//反向迭代器</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String String <span class="token punctuation">:</span> mi<span class="token punctuation">.</span><span class="token function">reverseIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>String <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//随机迭代器</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String String <span class="token punctuation">:</span> mi<span class="token punctuation">.</span><span class="token function">randomized</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>String <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token plain-text">/*Output: May I get offers this summer. summer. this offers get I May I this offers summer. May get */// </span> |
Java容器中,所有的Collection子类
会实现Iteratable
接口以实现foreach
功能,Iteratable
接口的实现又依赖于实现了Iterator
的内部类(参照LinkedList
中listIterator()
和descendingIterator()
的JDK源码)。有的容器类会有多个实现Iterator接口的内部类,通过返回不同的迭代器实现不同的迭代方式。
作者:IT_Matters
链接:https://www.jianshu.com/p/cf82ab7e51ef
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4