cache = new ArrayList<>();
@Override
public void init(AnalysisContext analysisContext) {}
@Override
public void put(String value) {
- cache.put(index++, value);
+ cache.add(value);
}
@Override
diff --git a/src/main/java/com/alibaba/excel/util/IntUtils.java b/src/main/java/com/alibaba/excel/util/IntUtils.java
new file mode 100644
index 00000000..0237e452
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/util/IntUtils.java
@@ -0,0 +1,30 @@
+package com.alibaba.excel.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Int utils
+ *
+ * @author Jiaju Zhuang
+ **/
+public class IntUtils {
+ private IntUtils() {}
+ /**
+ * Returns the {@code int} nearest in value to {@code value}.
+ *
+ * @param value any {@code long} value
+ * @return the same value cast to {@code int} if it is in the range of the {@code int} type,
+ * {@link Integer#MAX_VALUE} if it is too large, or {@link Integer#MIN_VALUE} if it is too
+ * small
+ */
+ public static int saturatedCast(long value) {
+ if (value > Integer.MAX_VALUE) {
+ return Integer.MAX_VALUE;
+ }
+ if (value < Integer.MIN_VALUE) {
+ return Integer.MIN_VALUE;
+ }
+ return (int) value;
+ }
+}
diff --git a/src/main/java/com/alibaba/excel/util/ListUtils.java b/src/main/java/com/alibaba/excel/util/ListUtils.java
new file mode 100644
index 00000000..6b4d7499
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/util/ListUtils.java
@@ -0,0 +1,63 @@
+package com.alibaba.excel.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * List utils
+ *
+ * @author Jiaju Zhuang
+ **/
+public class ListUtils {
+ private ListUtils() {}
+
+ /**
+ * Creates an {@code ArrayList} instance backed by an array with the specified initial size;
+ * simply delegates to {@link ArrayList#ArrayList(int)}.
+ *
+ * Note for Java 7 and later: this method is now unnecessary and should be treated as
+ * deprecated. Instead, use {@code new }{@link ArrayList#ArrayList(int) ArrayList}{@code <>(int)}
+ * directly, taking advantage of the new "diamond" syntax.
+ * (Unlike here, there is no risk of overload ambiguity, since the {@code ArrayList} constructors
+ * very wisely did not accept varargs.)
+ *
+ * @param initialArraySize the exact size of the initial backing array for the returned array list
+ * ({@code ArrayList} documentation calls this value the "capacity")
+ * @return a new, empty {@code ArrayList} which is guaranteed not to resize itself unless its size
+ * reaches {@code initialArraySize + 1}
+ * @throws IllegalArgumentException if {@code initialArraySize} is negative
+ */
+ public static ArrayList newArrayListWithCapacity(int initialArraySize) {
+ checkNonnegative(initialArraySize, "initialArraySize");
+ return new ArrayList<>(initialArraySize);
+ }
+
+ /**
+ * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, plus an
+ * unspecified amount of padding; you almost certainly mean to call {@link
+ * #newArrayListWithCapacity} (see that method for further advice on usage).
+ *
+ * Note: This method will soon be deprecated. Even in the rare case that you do want
+ * some amount of padding, it's best if you choose your desired amount explicitly.
+ *
+ * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list
+ * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of
+ * elements
+ * @throws IllegalArgumentException if {@code estimatedSize} is negative
+ */
+ public static ArrayList newArrayListWithExpectedSize(int estimatedSize) {
+ return new ArrayList<>(computeArrayListCapacity(estimatedSize));
+ }
+
+ static int computeArrayListCapacity(int arraySize) {
+ checkNonnegative(arraySize, "arraySize");
+ return IntUtils.saturatedCast(5L + arraySize + (arraySize / 10));
+ }
+
+ static int checkNonnegative(int value, String name) {
+ if (value < 0) {
+ throw new IllegalArgumentException(name + " cannot be negative but was: " + value);
+ }
+ return value;
+ }
+}
diff --git a/update.md b/update.md
index adfc42c2..2a7bfa83 100644
--- a/update.md
+++ b/update.md
@@ -5,6 +5,7 @@
* 升级ehcache 到 3.8.1
* 支持非驼峰的字段读写
* 修复`CellData`可能不返回行列号 [Issue #1832](https://github.com/alibaba/easyexcel/issues/1832)
+* 优化读取性能
# 2.2.8
* 兼容07在特殊的excel的情况下,读取数据异常