自己实现的数据结构,迭代器在源码中的一些应用,java.util.Iterator接口,/*** An iterator over a collection.  {@code Iterator} takes the place of* {@link Enumeration} in the Java Collections Framework.  Iterators* differ from enumerations in two ways:** <ul>*      <li> Iterators allow the caller to remove elements from the*           underlying collection during the iteration with well-defined*           semantics.*      <li> Method names have been improved.* </ul>** <p>This interface is a member of the* <a href="{@docRoot}/../technotes/guides/collections/index.html">* Java Collections Framework</a>.** @param <E> the type of elements returned by this iterator** @author  Josh Bloch* @see Collection* @see ListIterator* @see Iterable* @since 1.2*/
public interface Iterator<E>你们肯定非常熟悉他,里面有一个hasNext(),/*** Returns {@code true} if the iteration has more elements.* (In other words, returns {@code true} if {@link #next} would* return an element rather than throwing an exception.)** @return {@code true} if the iteration has more elements*/
boolean hasNext();是否有下一个元素,这个和我们的isLastCourse比较相近,当然对于我们现在这个业务场景,我们也可以实现一个hasNext方法的一个迭代器,那你们可以自己来实现一下,使用hasNext的一个CourseIterator,这个Iterator是一个很标准的迭代器模式,那这个接口的实现类会有很多,我们看一下ArrayList迭代器的一个实现,/*** An optimized version of AbstractList.Itr*/
private class Itr implements Iterator<E> {int cursor;       // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint expectedModCount = modCount;public boolean hasNext() {return cursor != size;}@SuppressWarnings("unchecked")public E next() {checkForComodification();int i = cursor;if (i >= size)throw new NoSuchElementException();Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length)throw new ConcurrentModificationException();cursor = i + 1;return (E) elementData[lastRet = i];}public void remove() {if (lastRet < 0)throw new IllegalStateException();checkForComodification();try {ArrayList.this.remove(lastRet);cursor = lastRet;lastRet = -1;expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}}@Override@SuppressWarnings("unchecked")public void forEachRemaining(Consumer<? super E> consumer) {Objects.requireNonNull(consumer);final int size = ArrayList.this.size;int i = cursor;if (i >= size) {return;}final Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length) {throw new ConcurrentModificationException();}while (i != size && modCount == expectedModCount) {consumer.accept((E) elementData[i++]);}// update once at end of iteration to reduce heap write trafficcursor = i;lastRet = i - 1;checkForComodification();}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}
}通过Itr来实现了Iterator接口,在里面进行了各种实现,public boolean hasNext() {return cursor != size;
}hasNext也是用size进行的判断,然后获取下一个元素,@SuppressWarnings("unchecked")
public E next() {checkForComodification();int i = cursor;if (i >= size)throw new NoSuchElementException();Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length)throw new ConcurrentModificationException();cursor = i + 1;return (E) elementData[lastRet = i];
}还有removepublic void remove() {if (lastRet < 0)throw new IllegalStateException();checkForComodification();try {ArrayList.this.remove(lastRet);cursor = lastRet;lastRet = -1;expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}
}那再往下看,对于Itr呢,进行了一个子类扩展,比如扩展了哪些呢/*** An optimized version of AbstractList.ListItr*/private class ListItr extends Itr implements ListIterator<E> {ListItr(int index) {super();cursor = index;}public boolean hasPrevious() {return cursor != 0;}public int nextIndex() {return cursor;}public int previousIndex() {return cursor - 1;}@SuppressWarnings("unchecked")public E previous() {checkForComodification();int i = cursor - 1;if (i < 0)throw new NoSuchElementException();Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length)throw new ConcurrentModificationException();cursor = i;return (E) elementData[lastRet = i];}public void set(E e) {if (lastRet < 0)throw new IllegalStateException();checkForComodification();try {ArrayList.this.set(lastRet, e);} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}}public void add(E e) {checkForComodification();try {int i = cursor;ArrayList.this.add(i, e);cursor = i + 1;lastRet = -1;expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}}}比如是否有前面的元素,下一个索引,前面的索引,下边也是一样的,前面的元素,那对于迭代器在源码中的应用,你们可以看一下,包括这里面的实现类有很多,比如netty这个包,Spring,mybatis,所以想看迭代器模式,在各个框架中的应用,通过接口找他的实现,全部可以找到,非常的方便,那这些就是在JDK中的一些使用,那我们再看一个在mybatis中的一个使用,我们打开这个类,DefaultCursor这么一个类/*** This is the default implementation of a MyBatis Cursor.* This implementation is not thread safe.** @author Guillaume Darmont / guillaume@dropinocean.com*/
public class DefaultCursor<T> implements Cursor<T>我们可以看到他实现了Cursor这个接口,Cursor就是游标,这个就是Mybatis的默认游标,很简单我们看一下,默认持有一个Cursor的游标器private final CursorIterator cursorIterator = new CursorIterator();我们可以看到这里创建了一个Cursor游标迭代器,然后通过iterator()这个方法@Override
public Iterator<T> iterator() {if (iteratorRetrieved) {throw new IllegalStateException("Cannot open more than one iterator on a Cursor");}iteratorRetrieved = true;return cursorIterator;
}返回了适当的迭代器接口对象,正是JDK中的Iterator所以迭代器模式,在源码和开源框架中的应用非常的广泛,你们可以通过他的接口的实现,来具体的有方向的研究,那迭代器在日常工作当中,几乎不会来写,除非我们要定义自己的数据结构的时候,我们可能要定制一个自己实现的数据结构,对应的迭代器

迭代器模式源码解析(jdk+mybatis)相关推荐

  1. 组合模式源码解析(jdk+mybatis)

    我们先看一下java.awt.container这么一个类,public class Container extends Component 我们可以看到这个类继承Component,awt这个包下边 ...

  2. 代理模式源码解析(jdk+spring+mybatis)

    首先是java.lang.reflect,也就是我们刚刚使用的Proxy这个类,这里面coding的时候,也就是debug的时候,这个就是代理的一个典型应用,还有proxyFactoryBean,这个 ...

  3. 装饰者模式源码解析(spring-session mybatis jdk servlet)

    那在JDK中体现最明显的,就是JAVA IO方面的一些类,那在JAVA IO中,我们为了增加缓存,我们使用BufferedReader,那现在我们来看一下,那因为增加缓存的功能,类有很多,子类也就需要 ...

  4. 桥接模式源码解析(jdk)

    现在我们来看一下桥接模式在源码中的一些应用,首先我们说一下,我们先看一个接口,首先我们看一下Driver的实现类,我们看到com.mysql.jdbc实现了Driver,添加了ORACLE的驱动,或者 ...

  5. 享元模式源码解析(jdk+tomcat)

    首先我们看一下Integer这个类,在使用它的时候非常非常的频繁,那我们看一下Integer有一个方法,叫valueOfpublic final class Integer extends Numbe ...

  6. 美团Leaf源码——号段模式源码解析

    前言 分布式ID生成策略基本要求就是全局不重复,最好还能递增,长度较短,性能高,可用性强.关于相关的实现方案有很多,本文着重使用美团开源的分布式ID生成解决方案--Leaf. 关于Leaf,美团官方的 ...

  7. 【MyBatis源码解析】MyBatis一二级缓存

    MyBatis缓存 我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相 ...

  8. MyBatis3源码解析(8)MyBatis与Spring的结合

    简介 在上几篇文章中,解析了MyBatis的核心原理部分,我们大致对其有了一定的了解,接下来我们看看在日常的开发中MyBatis是如何与Spring框架结合的 源码解析 在我们的日常开发中,使用Spr ...

  9. Spark内核(上)——附:两种Yarn模式源码解析

    文章目录 一.Spark内核概述 1.1 Spark核心组件回顾 1.1.1 Driver 1.1.2 Executor 1.2 Spark通用运行流程概述 二.Spark通信架构概述 2.1 Spa ...

最新文章

  1. Hql中使用in参数
  2. 菜鸟小编对云计算的一点猜想
  3. 玩转oracle 11g(17):命令学习5
  4. python生产教程_python入门教程12-09 (python语法入门之生产者消费者模型)
  5. 景安mysql主机_景安虚拟主机使用教程
  6. linux文件IO的操作
  7. HDU3783 ZOJ【文本处理】
  8. 使用pscp在Linux、Windows间互传文件
  9. OD教程(汇编基础)
  10. 最新Linux系统安装腾讯QQ教程(非Wine)
  11. 猿创征文|工具百宝箱-数据库连接工具-接口调试与测试工具-抓包工具
  12. ping 端口是否开放(Mac、Linux、Windows系统)
  13. leetcode 第1题【两数之和】C语言
  14. Opencv3.4中使用SURF等算法使用错误的解决方法
  15. 第十五届全国大学生智能车全国总决赛获奖信息-西部赛区
  16. MATLAB画路径图(带结点,不同颜色的路径)
  17. 学渣之路:一个月拯救我英语四级
  18. 32. Pandas借助Python爬虫读取HTML网页表格存储到Excel文件
  19. ASP.NET MVC3实践
  20. java 围棋_Java.awt实现一个简单的围棋

热门文章

  1. 病毒与木马大多作成 动态库形式的原因
  2. myeclipse 重新关联项目和svn
  3. [Erlang-0003][OTP] Efficiency Guide User's Guide - Common Caveats
  4. SQL语句实现两个数据库表直接操作
  5. 大话设计模式—状态模式
  6. 【HNOI2019】部分题简要题解
  7. 【特征选择】基础知识
  8. Netty使用Marshalling传输信息
  9. Property #39;sqlSessionFactory#39; or #39;sqlSessionTemplate#39; are required
  10. 坚持己见还是随波逐流