使用Iterator遍历Sheet(POI)验证及解释结果有序性
test.xlsx:
Code:
package poi; import static org.junit.Assert.*; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(value=Parameterized.class)
public class TestIterator {
public TestIterator(Integer times) {
} @Parameters
public static Collection<Integer[]> init(){
int times=200;
Integer[][] parameters=new Integer[times][1];
for (int i = 0; i < times; i++) {
parameters[i][0]=i;
}
return Arrays.asList(parameters);
} @Test
public void testItertor() throws IOException{
List<Integer> expected=new ArrayList<Integer>();//rowNums.need to change according to demand
for (int i = 0; i <=1 ; i++) {
expected.add(i);
}
expected.add(3);
expected.add(4);
expected.add(6); List<Integer> actual=new ArrayList<Integer>(); String filePath="/poi/test.xlsx";
InputStream is=this.getClass().getResourceAsStream(filePath);
Workbook wb=new XSSFWorkbook(is);
Sheet sheet=wb.getSheetAt(0);
for (Row row : sheet) {
actual.add(row.getRowNum());
} assertEquals(expected, actual);
}
}
结果:通过验证。
源码解析:
org.apache.poi.xssf.usermodel.XSSFSheet
XSSFRow的存放数据结构:
private TreeMap<Integer, XSSFRow> _rows;
/**
* @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
* be the third row if say for instance the second row is undefined.
* Call getRowNum() on each row if you care which one it is.
*/
@SuppressWarnings("unchecked")
public Iterator<Row> rowIterator() {
return (Iterator<Row>)(Iterator<? extends Row>) _rows.values().iterator();
} /**
* Alias for {@link #rowIterator()} to
* allow foreach loops
*/
public Iterator<Row> iterator() {
return rowIterator();
}
使用Iterator遍历Sheet(POI)验证及解释结果有序性的更多相关文章
- ES6的 Iterator 遍历器到底是什么?
这节课要讲的是ES6中的Iterator. for...of为啥不遍历Object对象 第十三节我们讲了简单又实用的for...of,我们可以使用它来遍历数组,字符串,Set和Map结构,但是有没有发 ...
- 关于for循环和Iterator遍历ArrayList的性能问题
今日看到@DriveMan的一篇博客,题为<ArrayList集合实现RandomAccess接口有何作用?为何LinkedList集合却没实现这接口?>,文中提到对于实现了RandomA ...
- [转]使用Enumeration和Iterator遍历集合类
原文地址:http://www.cnblogs.com/xwdreamer/archive/2012/05/30/2526268.html 前言 在数据库连接池分析的代码实例中,看到其中使用Enume ...
- Java SE之For增强与Iterator遍历器提取数据(附Map.Entry)
增强for循环: 1.操作数组 2.操作List集合 3.操作Map集合 1.map.values()法 2.map.keySet()法 [传统方法] 3.Map.Entry法 ...
- 疑问:Iterator 遍历器和数据集合各种遍历方法的区别
https://es6.ruanyifeng.com/#docs/iterator Iterator(遍历器)的概念 Iterator 接口主要供for...of消费 Iterator 的遍历过程是: ...
- 8.2.2 使用Java8增强的Iterator遍历集合元素
8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...
- java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);
java.util.Collection接口 Java的集合主要由两个接口派生出来,一个是Collection一个是Map,本章只记录Collection常用集合 集合只能存储引用类型数据,不能存储基 ...
- Iterator遍历 (遍历集合)
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...
- Iterator遍历器 调用Symbol.Iterator属性,遍历器对象。
Iterator实现原理 创建一个指针对象,指向当前数据结构的起始位置.也就是说,遍历器对象本质上,就是一个指针对象. 第一次调用指针对象的next方法,可以将指针指向数据结构的第一个成员. 第二次调 ...
随机推荐
- Android动态加载jar、apk的实现
前段时间到阿里巴巴参加支付宝技术分享沙龙,看到支付宝在Android使用插件化的技术,挺好奇的.正好这几天看到了农民伯伯的相关文章,因此简单整理了下,有什么错误希望大神指正. 核心类 1.1 ...
- libgdx, mouse 关节
鼠标与body的交互就靠这个mouse 关节了. 在使用中:主要分成3步: 步1:mouseDown : 这个时期,调用world->QueryAABB.它有一个回调接口,并依据鼠标指针指定一个 ...
- hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...
- C++中rand()函数的用法
C++中rand()函数的用法 2011-12-30 11:03:59| 分类: C / C++|举报|字号 订阅 一.C++中不能使用random()函数 random函数不是ANSI C标准 ...
- BZOJ 1537: [POI2005]Aut- The Bus(dp + BIT)
对y坐标离散化, 然后按x坐标排序, dp. 一个点(x, y), 设到达这个点接到的最多乘客数为t, 那么t可以用来更新y'>=y的所有点.用树状数组维护最大值. -------------- ...
- DHTMLX系列组件的学习笔记
<link rel="stylesheet" type="text/css" href="codebase/skins/dhtmlxeditor ...
- stack around the variable “ ” was corrupted
用scanf格式控制不当经常发生此错误. 如 short int a=10; scanf("%d",&a); 应该是%hd; 一般是越界引起的. 参看:http://bl ...
- 7款Linux下阅读PDF的阅读器。
5款Linux下阅读PDF的阅读器.1. Mupdf:link 2. Adobe Reader:link 3. Foxit Reader:link 4.Evince:link 5. Okular:li ...
- Windows Azure 安全最佳实践 - 第 6 部分:Azure 服务如何扩展应用程序安全性
多种Windows Azure服务可以帮助您将应用程序安全性扩展到云. 有三种服务可提供多个提供程序之间的身份标识映射.内部部署数据中心间的连接和相互发送消息的应用程序功能(无论应用程序位于何处). ...
- 基于visual Studio2013解决C语言竞赛题之0809链表排序
题目