outputStreamList = new ArrayList<>();
+ try {
+ //加载PDF文件
+ PdfDocument pdfDocument = new PdfDocument();
+ pdfDocument.loadFromStream(inputStream);
+ BufferedImage image;
+ //保存PDF的每一页到图片
+ for (int i = 0; i < pdfDocument.getPages().getCount(); i++) {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ image = pdfDocument.saveAsImage(i);
+ ImageIO.write(image, "PNG", byteArrayOutputStream);
+ outputStreamList.add(byteArrayOutputStream);
+ }
+ pdfDocument.close();
+ } catch (IOException e) {
+ log.error(e.getMessage());
+ }
+ return outputStreamList;
+ }
+
+
+}
diff --git a/src/main/java/com/lin/pdfutil/PicToPdfUtil.java b/src/main/java/com/lin/pdfutil/PicToPdfUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..9d6969142f0cb07f4e6b5834d3d9d68f2bd00a2f
--- /dev/null
+++ b/src/main/java/com/lin/pdfutil/PicToPdfUtil.java
@@ -0,0 +1,172 @@
+package com.lin.pdfutil;
+
+import com.itextpdf.text.*;
+import com.itextpdf.text.pdf.PdfWriter;
+
+import com.spire.pdf.PdfDocument;
+import com.spire.pdf.PdfPageBase;
+import com.spire.pdf.graphics.PdfImage;
+
+import lombok.extern.slf4j.Slf4j;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+
+
+/**
+ * 图片转pdf工具
+ *
+ * @author linqiankun
+ */
+@Slf4j
+public class PicToPdfUtil {
+
+ /**
+ * 将pdf转换为图片
+ *
+ * @param inputStream 图片输入流
+ * @return pdf 输出流
+ */
+ public static ByteArrayOutputStream picToPdf1(InputStream inputStream) {
+
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ //创建pdf文档
+ PdfDocument document = new PdfDocument();
+ //添加页面
+ PdfPageBase pdfPageBase = document.getPages().add();
+ //加载图片
+ PdfImage pdfImage = PdfImage.fromStream(inputStream);
+
+ //绘制图片到pdf并设置其在pdf文件中的位置和大小
+ double width = getImgWidth(inputStream) / pdfPageBase.getActualBounds(true).getWidth();
+ double height = getImgHeight(inputStream) / pdfPageBase.getActualBounds(true).getHeight();
+
+ double fitRate = Math.max(width, height);
+ double fitWidth = getImgWidth(inputStream) / fitRate * 0.707f;
+ double fitHeight = getImgHeight(inputStream) / fitRate * 1.0f;
+
+ pdfPageBase.getCanvas().drawImage(pdfImage, 0, 0, fitWidth, fitHeight);
+
+ //保存并关闭
+ document.saveToStream(outputStream);
+ document.close();
+
+ return outputStream;
+ }
+
+ /**
+ * @param inputStream 图片文件
+ * @return 图片的宽度
+ */
+ private static double getImgHeight(InputStream inputStream) {
+ BufferedImage src = null;
+ int ret = -1;
+ try {
+ src = javax.imageio.ImageIO.read(inputStream);
+ ret = src.getWidth(null);
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ }
+ return ret;
+ }
+
+ /**
+ * @param inputStream 图片文件
+ * @return 图片的高度
+ */
+ private static double getImgWidth(InputStream inputStream) {
+ BufferedImage src = null;
+ int ret = -1;
+ try {
+ src = javax.imageio.ImageIO.read(inputStream);
+ ret = src.getHeight(null);
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ }
+ return ret;
+ }
+
+
+ /**
+ * 图片转pdf,位置不准确
+ *
+ * @param imgFilePath 图片文件路径
+ * @param pdfFilePath pdf文件路径
+ * @return 转换成功
+ * @throws IOException 文件读取异常
+ */
+ public static boolean imgToPdf2(String imgFilePath, String pdfFilePath) throws IOException {
+ File file = new File(imgFilePath);
+ if (file.exists()) {
+ Document document = new Document();
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(pdfFilePath);
+ PdfWriter.getInstance(document, fos);
+ // 添加PDF文档的某些信息,比如作者,主题等等
+ document.addAuthor("linqiankun");
+ document.addSubject("linqiankun");
+ // 设置文档的大小
+ document.setPageSize(PageSize.A4);
+ // 打开文档
+ document.open();
+ // 写入一段文字
+ // document.add(new Paragraph("JUST TEST ..."));
+ // 读取一个图片
+ Image image = Image.getInstance(imgFilePath);
+ float imageHeight = image.getScaledHeight();
+ float imageWidth = image.getScaledWidth();
+ int i = 0;
+ while (imageHeight > 500 || imageWidth > 500) {
+ image.scalePercent(100 - i);
+ i++;
+ imageHeight = image.getScaledHeight();
+ imageWidth = image.getScaledWidth();
+ System.out.println("imageHeight->" + imageHeight);
+ System.out.println("imageWidth->" + imageWidth);
+ }
+
+ image.setAlignment(Image.ALIGN_CENTER);
+ // 设置图片的绝对位置
+ // image.setAbsolutePosition(0, 0);
+ // image.scaleAbsolute(500, 400);
+ // 插入一个图片
+ document.add(image);
+ } catch (DocumentException | IOException de) {
+ log.error(de.getMessage());
+ }
+ document.close();
+ if (fos != null) {
+ fos.flush();
+ fos.close();
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * 图片转pdf
+ *
+ * @param imagePath 图片路径
+ * @param pdfPath pdf路径
+ * @throws DocumentException 文本异常
+ * @throws IOException 流异常
+ */
+ public static void imageToPdf3(String imagePath, String pdfPath) throws IOException, DocumentException {
+ BufferedImage img = ImageIO.read(new File(imagePath));
+ FileOutputStream fos = new FileOutputStream(pdfPath);
+ Document doc = new Document(null, 0, 0, 0, 0);
+ doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
+ Image image = Image.getInstance(imagePath);
+ PdfWriter.getInstance(doc, fos);
+ doc.open();
+ doc.add(image);
+ doc.close();
+ }
+
+
+}
diff --git a/src/main/java/com/lin/pdfutil/WordToPdfUtil.java b/src/main/java/com/lin/pdfutil/WordToPdfUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..d260192c385b07ed5cc37f45b7f999108eb792b7
--- /dev/null
+++ b/src/main/java/com/lin/pdfutil/WordToPdfUtil.java
@@ -0,0 +1,10 @@
+package com.lin.pdfutil;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * @author linqiankun
+ */
+@Slf4j
+public class WordToPdfUtil {
+}
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index 2bbfa36e6f0d666eb880e0aecdd32c843c1e7f2f..684f15b2f67f63ac6e4480e1965fa4f780f028a8 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 冒泡排序
* O(n)~O(n^2)~O(n^2)
* 稳定
+ *
+ * @author linqiankun
*/
public class BubbleSort {
@@ -14,15 +16,15 @@ public class BubbleSort {
/**
* 冒泡排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void bubbleSorted(int[] array, StatusCode statusCode) {
+ public static void bubbleSorted(int[] array, SortStatusCode sortStatusCode) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int falg = SortUtil.compare(array[i], array[j]);
//可修改排序方向
- if (falg == statusCode.getCode()) {
+ if (falg == sortStatusCode.getCode()) {
SortUtil.swap(array, i, j);
}
}
@@ -33,22 +35,23 @@ public class BubbleSort {
/**
* 鸡尾酒排序
*
- * @param array
- * @param n
+ * @param array 需要排序的数组
+ * @param n 数组大小
*/
public static void CocktailSort(int[] array, int n) {
- int left = 0; // 初始化边界
+ // 初始化边界
+ int left = 0;
int right = n - 1;
while (left < right) {
- for (int i = left; i < right; i++) // 前半轮,将最大元素放到后面
- {
+ // 前半轮,将最大元素放到后面
+ for (int i = left; i < right; i++) {
if (array[i] > array[i + 1]) {
SortUtil.swap(array, i, i + 1);
}
}
right--;
- for (int i = right; i > left; i--) // 后半轮,将最小元素放到前面
- {
+ // 后半轮,将最小元素放到前面
+ for (int i = right; i > left; i--) {
if (array[i - 1] > array[i]) {
SortUtil.swap(array, i - 1, i);
}
diff --git a/src/main/java/com/lin/sort/BucketSort.java b/src/main/java/com/lin/sort/BucketSort.java
index 9dab05fe57dbc920f34946ba014ba0bb56183bcc..c966feb57986a2d2d7f8de6e7612a124cb5a95b2 100644
--- a/src/main/java/com/lin/sort/BucketSort.java
+++ b/src/main/java/com/lin/sort/BucketSort.java
@@ -1,18 +1,22 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import java.util.Arrays;
+/**
+ * 桶排序
+ *
+ * @author linqiankun
+ */
public class BucketSort {
/**
* 桶排序
*
- * @param sourceArray
- * @return
- * @throws Exception
+ * @param sourceArray 需要排序的数组
+ * @return 排序后的数组
*/
public static int[] bucketSorted(int[] sourceArray) {
// 对 arr 进行拷贝,不改变参数内容
@@ -24,10 +28,9 @@ public class BucketSort {
/**
* 桶排序
*
- * @param arr
- * @param bucketSize
- * @return
- * @throws Exception
+ * @param arr 需要排序的数组
+ * @param bucketSize 桶大小
+ * @return 排序后的数组
*/
private static int[] bucketSort(int[] arr, int bucketSize) {
if (arr.length == 0) {
@@ -48,9 +51,9 @@ public class BucketSort {
int[][] buckets = new int[bucketCount][0];
// 利用映射函数将数据分配到各个桶中
- for (int i = 0; i < arr.length; i++) {
- int index = (int) Math.floor((arr[i] - minValue) / bucketSize);
- buckets[index] = arrAppend(buckets[index], arr[i]);
+ for (int item : arr) {
+ int index = (int) Math.floor((item - minValue) / bucketSize);
+ buckets[index] = arrAppend(buckets[index], item);
}
int arrIndex = 0;
@@ -59,10 +62,8 @@ public class BucketSort {
continue;
}
// 对每个桶进行排序,这里使用了插入排序
-
- InsertionSort.insertionSorted(bucket, StatusCode.MIX_SORT);
-
-// bucket = insertSort.sort(bucket);
+ InsertionSort.insertionSorted(bucket, SortStatusCode.MIX_SORT);
+ // bucket = insertSort.sort(bucket);
for (int value : bucket) {
arr[arrIndex++] = value;
}
@@ -74,8 +75,9 @@ public class BucketSort {
/**
* 自动扩容,并保存数据
*
- * @param arr
- * @param value
+ * @param arr 桶
+ * @param value 值
+ * @return 桶
*/
private static int[] arrAppend(int[] arr, int value) {
arr = Arrays.copyOf(arr, arr.length + 1);
diff --git a/src/main/java/com/lin/sort/CountingSort.java b/src/main/java/com/lin/sort/CountingSort.java
index f000a3a4f4f9d8031f78879a583809c43b63fa9d..9cf65895a1ee088215a8bccee3b45a67b9b0cc49 100644
--- a/src/main/java/com/lin/sort/CountingSort.java
+++ b/src/main/java/com/lin/sort/CountingSort.java
@@ -4,6 +4,8 @@ import java.util.Arrays;
/**
* 计数排序
+ *
+ * @author linqiankun
*/
public class CountingSort {
@@ -11,7 +13,7 @@ public class CountingSort {
/**
* 计数排序
*
- * @param sourceArray
+ * @param sourceArray 需要排序的数组
* @return 返回排序后的数组
*/
public static int[] countingsorted(int[] sourceArray) {
@@ -26,9 +28,9 @@ public class CountingSort {
/**
* 统计排序
*
- * @param arr
- * @param maxValue
- * @return
+ * @param arr 排序的数组
+ * @param maxValue 最大值
+ * @return 排序后的数组
*/
private static int[] sort(int[] arr, int maxValue) {
int bucketLen = maxValue + 1;
@@ -51,8 +53,8 @@ public class CountingSort {
/**
* 获取最大值
*
- * @param arr
- * @return
+ * @param arr 需要排序的数组
+ * @return 最大值
*/
private static int getMaxValue(int[] arr) {
int maxValue = arr[0];
diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java
index 6ab5f7f907cd86e490642c715394e94b697eb9e7..caa6de89a60a515caea15c45b74c2bf2d8b7df25 100644
--- a/src/main/java/com/lin/sort/HeapSort.java
+++ b/src/main/java/com/lin/sort/HeapSort.java
@@ -1,69 +1,87 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 堆排序
* O(nlogn)~O(nlogn)~O(nlogn)
* 不稳定
+ *
+ * @author linqiankun
*/
public class HeapSort {
/**
* 堆排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void heapSorted(int[] array, StatusCode statusCode) {
- int heap_size = buildHeap(array, array.length, statusCode); // 建立一个最大堆
- while (heap_size > 1) { // 堆(无序区)元素个数大于1,未完成排序
+ public static void heapSorted(int[] array, SortStatusCode sortStatusCode) {
+ // 建立一个最大堆
+ int heapSize = buildHeap(array, array.length, sortStatusCode);
+ // 堆(无序区)元素个数大于1,未完成排序
+ while (heapSize > 1) {
// 将堆顶元素与堆的最后一个元素互换,并从堆中去掉最后一个元素
// 此处交换操作很有可能把后面元素的稳定性打乱,所以堆排序是不稳定的排序算法
- SortUtil.swap(array, 0, --heap_size);
- heapify(array, 0, heap_size, statusCode); // 从新的堆顶元素开始向下进行堆调整,时间复杂度O(logn)
+ SortUtil.swap(array, 0, --heapSize);
+ // 从新的堆顶元素开始向下进行堆调整,时间复杂度O(logn)
+ heapify(array, 0, heapSize, sortStatusCode);
}
}
/**
* 堆结构调整
+ * 从A[i]向下进行堆调整
*
- * @param array
- * @param i
+ * @param array 堆数组
+ * @param i 当前节点索引
* @param size
+ * @param sortStatusCode 排序方式
*/
- private static void heapify(int[] array, int i, int size, StatusCode statusCode) // 从A[i]向下进行堆调整
- {
- int left_child = 2 * i + 1; // 左孩子索引
- int right_child = 2 * i + 2; // 右孩子索引
- int max = i; // 选出当前结点与其左右孩子三者之中的最大值
- if (left_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[left_child]))
-// if (left_child < size && array[left_child] > array[max])
- max = left_child;
- if (right_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[right_child]))
-// if (right_child < size && array[right_child] > array[max])
- max = right_child;
+ private static void heapify(int[] array, int i, int size, SortStatusCode sortStatusCode) {
+ // 左孩子索引
+ int leftChild = 2 * i + 1;
+ // 右孩子索引
+ int rightChild = 2 * i + 2;
+ // 选出当前结点与其左右孩子三者之中的最大值
+ int max = i;
+ if (leftChild < size && sortStatusCode.getCode() == SortUtil.compare(array[max], array[leftChild]))
+ // if (leftChild < size && array[leftChild] > array[max])
+ {
+ max = leftChild;
+ }
+ if (rightChild < size && sortStatusCode.getCode() == SortUtil.compare(array[max], array[rightChild]))
+ // if (rightChild < size && array[rightChild] > array[max])
+ {
+ max = rightChild;
+ }
if (max != i) {
- SortUtil.swap(array, i, max); // 把当前结点和它的最大(直接)子节点进行交换
- heapify(array, max, size, statusCode); // 递归调用,继续从当前结点向下进行堆调整
+ // 把当前结点和它的最大(直接)子节点进行交换
+ SortUtil.swap(array, i, max);
+ // 递归调用,继续从当前结点向下进行堆调整
+ heapify(array, max, size, sortStatusCode);
}
}
/**
* 构造堆
+ * 建堆,时间复杂度O(n)
*
- * @param array
- * @param n
+ * @param array 堆数组
+ * @param n 当前节点
+ * @param sortStatusCode 排序方式
* @return
*/
- private static int buildHeap(int[] array, int n, StatusCode statusCode) // 建堆,时间复杂度O(n)
- {
- int heap_size = n;
- for (int i = heap_size / 2 - 1; i >= 0; i--) // 从每一个非叶结点开始向下进行堆调整
- heapify(array, i, heap_size, statusCode);
- return heap_size;
+ private static int buildHeap(int[] array, int n, SortStatusCode sortStatusCode) {
+ int heapSize = n;
+ // 从每一个非叶结点开始向下进行堆调整
+ for (int i = heapSize / 2 - 1; i >= 0; i--) {
+ heapify(array, i, heapSize, sortStatusCode);
+ }
+ return heapSize;
}
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
index f19c696bf14a8d15a0fc80c1b4d34d19fe5ff3ed..78767d5f8abdacd4978f068763ad8ac359b08625 100644
--- a/src/main/java/com/lin/sort/InsertionSort.java
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 插入排序
* O(n)~O(n^2)~O(n^2)
* 稳定
+ *
+ * @author linqiankun
*/
public class InsertionSort {
@@ -14,16 +16,16 @@ public class InsertionSort {
/**
* 插入排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void insertionSorted(int[] array, StatusCode statusCode) {
+ public static void insertionSorted(int[] array, SortStatusCode sortStatusCode) {
//默认第0个是排好序的,i以前的排好序
for (int i = 1; i < array.length; i++) {
int get = array[i];
int j = i - 1;
//从当前位值倒序往前查找
- while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) {
+ while (j >= 0 && sortStatusCode.getCode() == SortUtil.compare(get, array[j])) {
array[j + 1] = array[j];
j--;
}
@@ -36,28 +38,28 @@ public class InsertionSort {
/**
* 二分插入排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void insertionSortedDichotomy(int[] array, StatusCode statusCode) {
+ public static void insertionSortedDichotomy(int[] array, SortStatusCode sortStatusCode) {
for (int i = 1; i < array.length; i++) {
int get = array[i];
int left = 0;
int right = i - 1;
//二分查找数据所处的位置,也是在之前的队列中
- while (left <= right)
- {
+ while (left <= right) {
int mid = (left + right) / 2;
- if (statusCode.getCode() == SortUtil.compare(get,array[mid]))
-// if (array[mid] > get)
+ if (sortStatusCode.getCode() == SortUtil.compare(get, array[mid]))
+ // if (array[mid] > get)
+ {
right = mid - 1;
- else
+ } else {
left = mid + 1;
+ }
}
//拿走的是位置i的数据,left之后的往后移动
- for (int j = i - 1; j >= left; j--)
- {
- array[j + 1] = array[j];
+ if (i - left >= 0) {
+ System.arraycopy(array, left, array, left + 1, i - left);
}
array[left] = get;
}
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
index 5df23b9f949973600d3a134daa4693c69ff8d39e..389abc3c3e8da6ca1aa2898888c75969cf773ba3 100644
--- a/src/main/java/com/lin/sort/MergeSort.java
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 归并排序
* O(nlogn)~O(nlogn)~O(nlogn)
* 稳定
+ *
+ * @author linqiankun
*/
public class MergeSort {
@@ -14,17 +16,17 @@ public class MergeSort {
/**
* 非递归归并排序(错误)
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void mergeSortedIteration(int array[], StatusCode statusCode) {
+ public static void mergeSortedIteration(int[] array, SortStatusCode sortStatusCode) {
int left, mid, right;
for (int i = 1; i < array.length; i *= 2) {
left = 0;
while (left + i < array.length) {
mid = left + i - 1;
right = mid + 1 < array.length ? mid + 1 : array.length - 1;
- merge(array, statusCode, left, mid, right);
+ merge(array, sortStatusCode, left, mid, right);
left = mid + 1;
}
}
@@ -34,33 +36,34 @@ public class MergeSort {
/**
* 递归使用归并排序
*
- * @param array
- * @param statusCode
- * @param left
- * @param right
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
+ * @param left 左边界
+ * @param right 右边界
*/
- public static void mergeSortedRecursion(int[] array, StatusCode statusCode, int left, int right) {
- if (left == right)
+ public static void mergeSortedRecursion(int[] array, SortStatusCode sortStatusCode, int left, int right) {
+ if (left == right) {
return;
+ }
int mid = (left + right) / 2;
//前半段数组归并
- mergeSortedRecursion(array, statusCode, left, mid);
+ mergeSortedRecursion(array, sortStatusCode, left, mid);
//后半段数组归并
- mergeSortedRecursion(array, statusCode, mid + 1, right);
+ mergeSortedRecursion(array, sortStatusCode, mid + 1, right);
//归并合并
- merge(array, statusCode, left, mid, right);
+ merge(array, sortStatusCode, left, mid, right);
}
/**
* 数组合并
*
- * @param array
- * @param statusCode
- * @param left
- * @param mid
- * @param right
+ * @param array 需要合并的数组
+ * @param sortStatusCode 排序方式
+ * @param left 左边界
+ * @param mid 中间位置
+ * @param right 有边界
*/
- private static void merge(int[] array, StatusCode statusCode, int left, int mid, int right) {
+ private static void merge(int[] array, SortStatusCode sortStatusCode, int left, int mid, int right) {
int len = right - left + 1;
int[] temp = new int[len];
int index = 0;
@@ -70,7 +73,7 @@ public class MergeSort {
int j = mid + 1;
//两组数据归并操作
while (i <= mid && j <= right) {
- temp[index++] = statusCode.getCode() == SortUtil.compare(array[i], array[j]) ? array[i++] : array[j++];
+ temp[index++] = sortStatusCode.getCode() == SortUtil.compare(array[i], array[j]) ? array[i++] : array[j++];
}
//上面的循环无法将两个子数组的数据全部循环到
while (i <= mid) {
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
index f4f76c4e3f82b72927e0890ef89c9d6d09fa599d..19a1d722acac4cc30226e9332abf0f8820ef8735 100644
--- a/src/main/java/com/lin/sort/QuickSort.java
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 快速排序
* O(nlogn)~O(nlogn)~O(n^2)
* 不稳定
+ *
+ * @author linqiankun
*/
public class QuickSort {
@@ -14,38 +16,51 @@ public class QuickSort {
/**
* 分治块
*
- * @param array
- * @param left
- * @param right
- * @param statusCode
- * @return
+ * @param array 分治数组
+ * @param left 左边界
+ * @param right 右边界
+ * @param sortStatusCode 排序方式
+ * @return 基准的索引
*/
- private static int partition(int[] array, int left, int right, StatusCode statusCode) // 划分函数
+ private static int partition(int[] array, int left, int right, SortStatusCode sortStatusCode) // 划分函数
{
- int pivot = array[right]; // 这里每次都选择最后一个元素作为基准
- int tail = left - 1; // tail为小于基准的子数组最后一个元素的索引
- for (int i = left; i < right; i++) // 遍历基准以外的其他元素
- {
- if (SortUtil.compare(pivot, array[i]) == 0 || !(statusCode.getCode() == SortUtil.compare(pivot, array[i]))) //会破环稳定性
-// if (array[i] <= pivot) // 把小于等于基准的元素放到前一个子数组末尾
+ // 这里每次都选择最后一个元素作为基准
+ int pivot = array[right];
+ // tail为小于基准的子数组最后一个元素的索引
+ int tail = left - 1;
+ // 遍历基准以外的其他元素
+ for (int i = left; i < right; i++) {
+ // 把小于等于基准的元素放到前一个子数组末尾
+ //会破环稳定性
+ if (SortUtil.compare(pivot, array[i]) == 0 || (sortStatusCode.getCode() != SortUtil.compare(pivot, array[i])))
+ // if (array[i] <= pivot)
{
SortUtil.swap(array, ++tail, i);
}
- }
- SortUtil.swap(array, tail + 1, right); // 最后把基准放到前一个子数组的后边,剩下的子数组既是大于基准的子数组
+ } // 最后把基准放到前一个子数组的后边,剩下的子数组既是大于基准的子数组
// 该操作很有可能把后面元素的稳定性打乱,所以快速排序是不稳定的排序算法
- return tail + 1; // 返回基准的索引
+ SortUtil.swap(array, tail + 1, right);
+ // 返回基准的索引
+ return tail + 1;
}
+
/**
* 快速排序
+ *
+ * @param array 排序数组
+ * @param left 左边界
+ * @param right 右边界
+ * @param sortStatusCode 排序方式
*/
- public static void quickSorted(int[] array, int left, int right, StatusCode statusCode) {
- if (left >= right)
+ public static void quickSorted(int[] array, int left, int right, SortStatusCode sortStatusCode) {
+ if (left >= right) {
return;
- int pivot_index = partition(array, left, right, statusCode); // 基准的索引
- quickSorted(array, left, pivot_index - 1, statusCode);
- quickSorted(array, pivot_index + 1, right, statusCode);
+ }
+ // 基准的索引
+ int pivotIndex = partition(array, left, right, sortStatusCode);
+ quickSorted(array, left, pivotIndex - 1, sortStatusCode);
+ quickSorted(array, pivotIndex + 1, right, sortStatusCode);
}
diff --git a/src/main/java/com/lin/sort/RadixSort.java b/src/main/java/com/lin/sort/RadixSort.java
index 6c66253f38846e2914347e58d43f9949e2812eca..9b4bf181501fca08f3ffcb548656f9b04b3523af 100644
--- a/src/main/java/com/lin/sort/RadixSort.java
+++ b/src/main/java/com/lin/sort/RadixSort.java
@@ -4,6 +4,8 @@ import java.util.Arrays;
/**
* 基数排序
+ *
+ * @author linqiankun
*/
public class RadixSort {
@@ -11,8 +13,8 @@ public class RadixSort {
/**
* 基数排序
*
- * @param sourceArray
- * @return
+ * @param sourceArray 需要排序的数组
+ * @return 排序后的数组
*/
public static int[] radixSorted(int[] sourceArray) {
// 对 arr 进行拷贝,不改变参数内容
@@ -25,7 +27,7 @@ public class RadixSort {
/**
* 获取最高位数
*
- * @param arr
+ * @param arr 需要排序的数组
* @return
*/
private static int getMaxDigit(int[] arr) {
@@ -36,8 +38,8 @@ public class RadixSort {
/**
* 获取最大值
*
- * @param arr
- * @return
+ * @param arr 排序数组
+ * @return 最大值
*/
private static int getMaxValue(int[] arr) {
int maxValue = arr[0];
@@ -49,7 +51,7 @@ public class RadixSort {
return maxValue;
}
- protected static int getNumLenght(long num) {
+ private static int getNumLenght(long num) {
if (num == 0) {
return 1;
}
@@ -63,9 +65,9 @@ public class RadixSort {
/**
* 基数排序
*
- * @param arr
+ * @param arr 排序数组
* @param maxDigit
- * @return
+ * @return 排序后数组
*/
private static int[] radixSort(int[] arr, int maxDigit) {
int mod = 10;
@@ -75,9 +77,9 @@ public class RadixSort {
// 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10)
int[][] counter = new int[mod * 2][0];
- for (int j = 0; j < arr.length; j++) {
- int bucket = ((arr[j] % mod) / dev) + mod;
- counter[bucket] = arrayAppend(counter[bucket], arr[j]);
+ for (int item : arr) {
+ int bucket = ((item % mod) / dev) + mod;
+ counter[bucket] = arrayAppend(counter[bucket], item);
}
int pos = 0;
@@ -94,8 +96,8 @@ public class RadixSort {
/**
* 自动扩容,并保存数据
*
- * @param arr
- * @param value
+ * @param arr 数组
+ * @param value 扩容后数组
*/
private static int[] arrayAppend(int[] arr, int value) {
arr = Arrays.copyOf(arr, arr.length + 1);
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index 3b6e7d3db5ac2c5d49bb47a5122dc585c63b5879..af7ee00f941dbd6644faf7c8fd75ef5a3b34809f 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 选择排序
* O(n^2)~O(n^2)~O(n^2)
* 不稳定
+ *
+ * @author linqiankun
*/
public class SelectionSort {
@@ -14,17 +16,17 @@ public class SelectionSort {
/**
* 选择排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void selectionSorted(int[] array, StatusCode statusCode) {
+ public static void selectionSorted(int[] array, SortStatusCode sortStatusCode) {
//确定最小的位置
for (int i = 0; i < array.length; i++) {
int min = i;
//确定最小的位置存放的数据
for (int j = i + 1; j < array.length; j++) {
//用标志为查出为排序队列中最小/大的
- if (statusCode.getCode() == SortUtil.compare(array[j], array[min])) {
+ if (sortStatusCode.getCode() == SortUtil.compare(array[j], array[min])) {
min = j;
}
}
diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java
index 9bc773920f250d02b42ea2a516efc51a1bd078f7..76ffd6ddebe281c2a0827db0fd3bd282317c862b 100644
--- a/src/main/java/com/lin/sort/ShellSort.java
+++ b/src/main/java/com/lin/sort/ShellSort.java
@@ -1,12 +1,14 @@
package com.lin.sort;
-import com.lin.enums.StatusCode;
+import com.lin.enums.SortStatusCode;
import com.lin.sort.util.SortUtil;
/**
* 希尔排序
* O(n^1.3)~O(nlogn)~O(n^2)~O(n^2)
* 不稳定
+ *
+ * @author linqiankun
*/
public class ShellSort {
@@ -14,10 +16,10 @@ public class ShellSort {
/**
* 希尔排序
*
- * @param array
- * @param statusCode
+ * @param array 需要排序的数组
+ * @param sortStatusCode 排序方式
*/
- public static void shellSorted(int[] array, StatusCode statusCode) {
+ public static void shellSorted(int[] array, SortStatusCode sortStatusCode) {
int shell = 0;
//寻找最大的希尔值
while (shell <= array.length) {
@@ -29,7 +31,7 @@ public class ShellSort {
for (int i = shell; i < array.length; i++) {
int j = i - shell;
int get = array[i];
- while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) {
+ while (j >= 0 && sortStatusCode.getCode() == SortUtil.compare(get, array[j])) {
array[j + shell] = array[j];
j = j - shell;
}
diff --git a/src/main/java/com/lin/sort/util/SortUtil.java b/src/main/java/com/lin/sort/util/SortUtil.java
index 331e81f7af337e46802c2723ffef5e6fe3abb77b..e65c31e77af2080b6addab5d369c2a1c16fb1eb1 100644
--- a/src/main/java/com/lin/sort/util/SortUtil.java
+++ b/src/main/java/com/lin/sort/util/SortUtil.java
@@ -2,15 +2,17 @@ package com.lin.sort.util;
/**
* 排序工具
+ *
+ * @author linqiankun
*/
public class SortUtil {
/**
* 交换
*
- * @param array
- * @param a
- * @param b
+ * @param array 数组
+ * @param a 索引
+ * @param b 索引
*/
public static void swap(int[] array, int a, int b) {
int temp = array[a];
@@ -21,18 +23,12 @@ public class SortUtil {
/**
* 比较器
*
- * @param a
- * @param b
- * @return
+ * @param a 索引
+ * @param b 索引
+ * @return 结果,0,—1,1
*/
public static int compare(int a, int b) {
- if (a > b) {
- return 1;
- } else if (a < b) {
- return -1;
- } else {
- return 0;
- }
+ return Integer.compare(a, b);
}
}
diff --git a/src/main/java/com/lin/tree/nome/BSTree.java b/src/main/java/com/lin/tree/nome/BSTree.java
index 362584caf6232366c777a3ae5da5baabc8afb703..16c1646da2116144aa577016d3f07e98677da023 100644
--- a/src/main/java/com/lin/tree/nome/BSTree.java
+++ b/src/main/java/com/lin/tree/nome/BSTree.java
@@ -5,7 +5,7 @@ import lombok.Data;
/**
* 二叉搜索树
*
- * @param
+ * @param 数据泛型
*/
public class BSTree> {
@@ -35,21 +35,22 @@ public class BSTree> {
/**
* 二叉搜索树递归查找
*
- * @param x
- * @param key
- * @return
+ * @param x 跟节点
+ * @param key 查找的数据
+ * @return 查找到的节点
*/
private BSTNode search(BSTNode x, T key) {
- if (x == null)
+ if (x == null) {
return x;
-
+ }
int cmp = key.compareTo(x.key);
- if (cmp < 0)
+ if (cmp < 0) {
return search(x.left, key);
- else if (cmp > 0)
+ } else if (cmp > 0) {
return search(x.right, key);
- else
+ } else {
return x;
+ }
}
public BSTNode search(T key) {
@@ -59,20 +60,21 @@ public class BSTree> {
/**
* 二叉搜索树非递归查找
*
- * @param x
- * @param key
- * @return
+ * @param x 跟节点
+ * @param key 查找的数据
+ * @return 具备数据的节点
*/
private BSTNode iterativeSearch(BSTNode x, T key) {
while (x != null) {
int cmp = key.compareTo(x.key);
- if (cmp < 0)
+ if (cmp < 0) {
x = x.left;
- else if (cmp > 0)
+ } else if (cmp > 0) {
x = x.right;
- else
+ } else {
return x;
+ }
}
return x;
@@ -85,22 +87,30 @@ public class BSTree> {
/**
* 查找最大节点
*
- * @param tree
- * @return
+ * @param tree 跟节点
+ * @return 最大节点
*/
private BSTNode maximum(BSTNode tree) {
- if (tree == null)
+ if (tree == null) {
return null;
+ }
- while (tree.right != null)
+ while (tree.right != null) {
tree = tree.right;
+ }
return tree;
}
+ /**
+ * 查找最大节数据
+ *
+ * @return 最大节点
+ */
public T maximum() {
BSTNode p = maximum(mRoot);
- if (p != null)
+ if (p != null) {
return p.key;
+ }
return null;
}
@@ -108,22 +118,29 @@ public class BSTree> {
/**
* 查找最小节点
*
- * @param tree
- * @return
+ * @param tree 跟节点
+ * @return 最小节点
*/
private BSTNode minimum(BSTNode tree) {
- if (tree == null)
+ if (tree == null) {
return null;
-
- while (tree.left != null)
+ }
+ while (tree.left != null) {
tree = tree.left;
+ }
return tree;
}
+ /**
+ * 查找最小数据
+ *
+ * @return 最小节点
+ */
public T minimum() {
BSTNode p = minimum(mRoot);
- if (p != null)
+ if (p != null) {
return p.key;
+ }
return null;
}
@@ -131,14 +148,14 @@ public class BSTree> {
/**
* 查找前驱节点
*
- * @param x
- * @return
+ * @param x 当前节点
+ * @return 前驱节点
*/
public BSTNode predecessor(BSTNode x) {
// 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
- if (x.left != null)
+ if (x.left != null) {
return maximum(x.left);
-
+ }
// 如果x没有左孩子。则x有以下两种可能:
// (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
// (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
@@ -154,14 +171,14 @@ public class BSTree> {
/**
* 查找后继节点
*
- * @param x
- * @return
+ * @param x 当前节点
+ * @return 后继节点
*/
public BSTNode successor(BSTNode x) {
// 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
- if (x.right != null)
+ if (x.right != null) {
return minimum(x.right);
-
+ }
// 如果x没有右孩子。则x有以下两种可能:
// (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
// (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
@@ -177,8 +194,8 @@ public class BSTree> {
/**
* 将节点插入二叉树
*
- * @param bst
- * @param z
+ * @param bst 二叉树
+ * @param z 插入的节点
*/
private void insert(BSTree bst, BSTNode z) {
int cmp;
@@ -189,121 +206,140 @@ public class BSTree> {
while (x != null) {
y = x;
cmp = z.key.compareTo(x.key);
- if (cmp < 0)
+ if (cmp < 0) {
x = x.left;
- else
+ } else {
x = x.right;
+ }
}
z.parent = y;
- if (y == null)
+ if (y == null) {
bst.mRoot = z;
- else {
+ } else {
cmp = z.key.compareTo(y.key);
- if (cmp < 0)
+ if (cmp < 0) {
y.left = z;
- else
+ } else {
+
y.right = z;
+ }
}
}
+ /**
+ * 插入数据
+ *
+ * @param key 要插入的数据
+ */
public void insert(T key) {
BSTNode z = new BSTNode(key, null, null, null);
-
- // 如果新建结点失败,则返回。
- if (z != null)
- insert(this, z);
+ insert(this, z);
}
/**
* 删除节点并返回
*
- * @param bst
- * @param z
- * @return
+ * @param bst 二叉树
+ * @param z 要删除的节点
+ * @return 要删除的的节点
*/
private BSTNode remove(BSTree bst, BSTNode z) {
- BSTNode x = null;
- BSTNode y = null;
+ BSTNode x;
+ BSTNode y;
- if ((z.left == null) || (z.right == null))
+ if ((z.left == null) || (z.right == null)) {
y = z;
- else
+ } else {
y = successor(z);
-
- if (y.left != null)
+ }
+ if (y.left != null) {
x = y.left;
- else
+ } else {
x = y.right;
-
- if (x != null)
+ }
+ if (x != null) {
x.parent = y.parent;
-
- if (y.parent == null)
+ }
+ if (y.parent == null) {
bst.mRoot = x;
- else if (y == y.parent.left)
+ } else if (y == y.parent.left) {
y.parent.left = x;
- else
+ } else {
y.parent.right = x;
-
- if (y != z)
+ }
+ if (y != z) {
z.key = y.key;
-
+ }
return y;
}
+ /**
+ * 删除数据
+ *
+ * @param key 要删除的数据
+ */
public void remove(T key) {
BSTNode z, node;
- if ((z = search(mRoot, key)) != null)
- if ((node = remove(this, z)) != null)
- node = null;
+ if ((z = search(mRoot, key)) != null) {
+ node = remove(this, z);
+ }
}
/**
* 打印二叉搜索树
*
- * @param tree
- * @param key
- * @param direction
+ * @param tree 跟节点
+ * @param key 数据
+ * @param direction 层级
*/
private void print(BSTNode tree, T key, int direction) {
-
+ // tree是根节点
if (tree != null) {
-
- if (direction == 0) // tree是根节点
+ if (direction == 0) {
System.out.printf("%2d is root\n", tree.key);
- else // tree是分支节点
+ }
+ // tree是分支节点
+ else {
System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction == 1 ? "right" : "left");
-
+ }
print(tree.left, tree.key, -1);
print(tree.right, tree.key, 1);
}
}
+ /**
+ * 打印二叉树
+ */
public void print() {
- if (mRoot != null)
+ if (mRoot != null) {
print(mRoot, mRoot.key, 0);
+ }
}
/**
* 销毁二叉树
*
- * @param tree
+ * @param tree 跟节点
*/
private void destroy(BSTNode tree) {
- if (tree == null)
+ if (tree == null) {
return;
-
- if (tree.left != null)
+ }
+ if (tree.left != null) {
destroy(tree.left);
- if (tree.right != null)
+ }
+ if (tree.right != null) {
destroy(tree.right);
-
+ }
tree = null;
}
+ /**
+ * 销毁二叉树
+ */
public void clear() {
destroy(mRoot);
mRoot = null;
diff --git a/src/main/java/com/lin/tree/nome/Tree.java b/src/main/java/com/lin/tree/nome/Tree.java
index 91694a48ba2fa75eea4a4a2bad42b7a5354e1eac..96b0001866812368e751cdc780e0fb2a52271bf3 100644
--- a/src/main/java/com/lin/tree/nome/Tree.java
+++ b/src/main/java/com/lin/tree/nome/Tree.java
@@ -9,13 +9,15 @@ import java.util.concurrent.LinkedBlockingQueue;
/**
* 普通二叉树
+ *
+ * @author linqiankun
*/
public class Tree {
/**
* 递归实现前序遍历
*
- * @param node
+ * @param node 跟节点
*/
public void preOrderTraverse(Node node) {
if (node != null) {
@@ -28,7 +30,7 @@ public class Tree {
/**
* 非递归实现的前序遍历
*
- * @param root
+ * @param root 跟节点
*/
public void nrPreOrderTraverse(Node root) {
//借助栈实现
@@ -50,7 +52,7 @@ public class Tree {
/**
* 递归实现的中序遍历
*
- * @param node
+ * @param node 跟节点
*/
public void inOrderTraverse(Node node) {
if (node != null) {
@@ -63,7 +65,7 @@ public class Tree {
/**
* 非递归实现的中序遍历
*
- * @param root
+ * @param root 跟节点
*/
public void nrInOrderTraverse(Node root) {
Stack> stack = new Stack>();
@@ -82,7 +84,7 @@ public class Tree {
/**
* 递归实现的后续遍历
*
- * @param node
+ * @param node 跟节点
*/
public void postOrderTraverse(Node node) {
if (node != null) {
@@ -95,13 +97,14 @@ public class Tree {
/**
* 非递归实现的后续遍历
*
- * @param root
+ * @param root 跟节点
*/
public void nrPostOrderTraverse(Node root) {
Stack> stack = new Stack>();
Node node = root;
- Node preNode = null;//表示最近一次访问的节点
+ //表示最近一次访问的节点
+ Node preNode = null;
while (node != null || !stack.isEmpty()) {
while (node != null) {
@@ -125,7 +128,7 @@ public class Tree {
/**
* 借助队列实现的层序遍历
*
- * @param node
+ * @param node 跟节点
*/
public void levelTraverse(Node node) {
//借助队列实现
@@ -145,29 +148,30 @@ public class Tree {
/**
* 获取树的深度
*
- * @param node
- * @return
+ * @param node 跟节点
+ * @return 树的深度
*/
private Integer getHeight(Node node) {
- if (node == null)
+ if (node == null) {
return 0;
- else {
+ } else {
int left = getHeight(node.getLeft());
int right = getHeight(node.getRight());
- return left > right ? left + 1 : right + 1;//左子树 右子树最深的,再加上父节点本身深度1
+ //左子树 右子树最深的,再加上父节点本身深度1
+ return left > right ? left + 1 : right + 1;
}
}
/**
* 获取节点数量
*
- * @param node
- * @return
+ * @param node 跟节点
+ * @return 节点数量
*/
private Integer getSize(Node node) {
- if (node == null)
+ if (node == null) {
return 0;
- else {
+ } else {
int leftSize = getSize(node.getLeft());
int rightSize = getSize(node.getRight());
return leftSize + rightSize + 1;
diff --git a/src/main/java/com/lin/Util/ArraySortUtil.java b/src/main/java/com/lin/util/ArraySortUtil.java
similarity index 73%
rename from src/main/java/com/lin/Util/ArraySortUtil.java
rename to src/main/java/com/lin/util/ArraySortUtil.java
index f9154eb129c0bff2b091784431d97486dc3134cb..00d1b4144e1b0b777e8a1eb5045f9b1ebb548f30 100644
--- a/src/main/java/com/lin/Util/ArraySortUtil.java
+++ b/src/main/java/com/lin/util/ArraySortUtil.java
@@ -1,9 +1,9 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
/**
- * excel导出工具
+ * 数组排序工具
*
* @author 九分石人,2020-02-24
*/
@@ -11,5 +11,4 @@ import lombok.extern.slf4j.Slf4j;
public class ArraySortUtil {
-
}
diff --git a/src/main/java/com/lin/util/ExcelExportUtil.java b/src/main/java/com/lin/util/ExcelExportUtil.java
index bd263362ec830e9db53df4390e4c45c5868ed1c8..ce330185cd911b5537416bec5f534a03d55da5f0 100644
--- a/src/main/java/com/lin/util/ExcelExportUtil.java
+++ b/src/main/java/com/lin/util/ExcelExportUtil.java
@@ -26,6 +26,7 @@ public class ExcelExportUtil {
/**
* 导出Excel主方法,调用此方法即可(该方法未测试)
*
+ * @param 类型泛型
* @param fileName 文件名
* @param list 数据源
* @param title 标题数组
@@ -35,8 +36,9 @@ public class ExcelExportUtil {
public static void exportExcel(String fileName, @NotNull List list, String[] title, HttpServletResponse response) {
// 制作导出文件
HSSFWorkbook excel;
+ int maxNum = 5000;
try {
- if (list.size() > 5000) {
+ if (list.size() > maxNum) {
fileName = "数据量过大";
List listError = new ArrayList<>();
listError.add("数据量超过5000,无法导出");
@@ -68,6 +70,7 @@ public class ExcelExportUtil {
/**
* 制作HSSFWorkbook格式的Excel文件
*
+ * @param 类型泛型
* @param list 数据源,要导出的数据
* @param title 标题列表
* @return excel格式的文件
diff --git a/src/main/java/com/lin/util/ExcelImportUtil.java b/src/main/java/com/lin/util/ExcelImportUtil.java
index c9da58c2f5edbfb07272c08da9dbcd99048b71e7..502027663319894970f01b77813d1054b21cf60d 100644
--- a/src/main/java/com/lin/util/ExcelImportUtil.java
+++ b/src/main/java/com/lin/util/ExcelImportUtil.java
@@ -43,7 +43,8 @@ public class ExcelImportUtil {
public static List readExcelFile(InputStream fileInputStream, int validColumn, int startRow) {
try {
Workbook workbook = getWeebWork(fileInputStream);
- Sheet sheet = workbook.getSheetAt(0);// 只取sheet1的数据
+ // 只取sheet1的数据
+ Sheet sheet = workbook.getSheetAt(0);
return readFromSheet(sheet, validColumn, startRow);
} catch (Exception e) {
log.error(e.getMessage());
@@ -97,13 +98,18 @@ public class ExcelImportUtil {
@NotNull
private static List readFromSheet(@NotNull Sheet sheet, int validColumn, int startRow) {
List list = new ArrayList<>();
- int rownum = sheet.getLastRowNum();// 获取总行
- int totalCellNum = sheet.getRow(1).getLastCellNum();// 获取总列数
+ // 限制不能超过5000条数据
+ int maxNum = 5000;
+ // 获取总行
+ int rowNum = sheet.getLastRowNum();
+ // 获取总列数
+ int totalCellNum = sheet.getRow(1).getLastCellNum();
// 设置获取总行数,如果大于5000,则截止为5000.
- if (rownum >= 5000)
- rownum = 5000;
+ if (rowNum >= maxNum) {
+ rowNum = 5000;
+ }
// 从第三行开始处理数据
- for (int i = (startRow - 1); i <= rownum; i++) {
+ for (int i = (startRow - 1); i <= rowNum; i++) {
Row row = sheet.getRow(i);
if (row != null) {
// 第X列数据不完整的话 视为无效数据 不予添加进list中
@@ -111,16 +117,19 @@ public class ExcelImportUtil {
continue;
}
try {
- row.getCell(validColumn).setCellType(Cell.CELL_TYPE_STRING); //设置成文本格式
+ //设置成文本格式
+ row.getCell(validColumn).setCellType(Cell.CELL_TYPE_STRING);
} catch (Exception e) {
log.error(e.getMessage());
}
- String[] cells = new String[totalCellNum];// 根据标题行生成相同列数的String数组
+ // 根据标题行生成相同列数的String数组
+ String[] cells = new String[totalCellNum];
for (int j = 0; j < totalCellNum; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
if (!"".equals(String.valueOf(cell).trim())) {
- cells[j] = String.valueOf(cell).trim();// 每个有效数据单元格都转化为String类型
+ // 每个有效数据单元格都转化为String类型
+ cells[j] = String.valueOf(cell).trim();
} else {
cells[j] = null;
}
diff --git a/src/main/java/com/lin/util/FileReadUtil.java b/src/main/java/com/lin/util/FileReadUtil.java
index 2e33e8fa863ae40239efb8b5e48fb1b60c4d88ab..c195747e7057af1b84a87fb5988ce4a9e83bac0b 100644
--- a/src/main/java/com/lin/util/FileReadUtil.java
+++ b/src/main/java/com/lin/util/FileReadUtil.java
@@ -22,7 +22,7 @@ public class FileReadUtil {
*/
public static File readToFile(String filePath) {
File file = new File(filePath);
- if(file.exists()){
+ if (file.exists()) {
return file;
}
return null;
@@ -42,23 +42,30 @@ public class FileReadUtil {
try {
is = new FileInputStream(filePath);
reader = new BufferedReader(new InputStreamReader(is));
+ // 用来保存每行读取的内容
+ String line;
+ // 读取第一行
+ line = reader.readLine();
- String line; // 用来保存每行读取的内容
- line = reader.readLine(); // 读取第一行
-
- while (line != null) { // 如果 line 为空说明读完了
- buffer.append(line); // 将读到的内容添加到 buffer 中
- buffer.append("\n"); // 添加换行符
- line = reader.readLine(); // 读取下一行
+ // 如果 line 为空说明读完了
+ while (line != null) {
+ // 将读到的内容添加到 buffer 中
+ buffer.append(line);
+ // 添加换行符
+ buffer.append("\n");
+ // 读取下一行
+ line = reader.readLine();
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
try {
- if (reader != null)
+ if (reader != null) {
reader.close();
- if (is != null)
+ }
+ if (is != null) {
is.close();
+ }
} catch (IOException e) {
log.error(e.getMessage());
}
diff --git a/src/main/java/com/lin/util/FileWriteUtil.java b/src/main/java/com/lin/util/FileWriteUtil.java
index ca6a7051711d5126545299eccf78e11c28c98a86..160ae542ba014cb2f01780735ef9bdf4dbf41ff6 100644
--- a/src/main/java/com/lin/util/FileWriteUtil.java
+++ b/src/main/java/com/lin/util/FileWriteUtil.java
@@ -52,10 +52,12 @@ public class FileWriteUtil {
log.error(e.getMessage());
} finally {
try {
- if (writer != null)
+ if (writer != null) {
writer.close();
- if (outputStream != null)
+ }
+ if (outputStream != null) {
outputStream.close();
+ }
} catch (IOException e) {
log.error(e.getMessage());
}
@@ -84,8 +86,9 @@ public class FileWriteUtil {
e.printStackTrace();
} finally {
try {
- if (outputStream != null)
+ if (outputStream != null) {
outputStream.close();
+ }
} catch (IOException e) {
log.error(e.getMessage());
}
diff --git a/src/main/java/com/lin/util/HttpConnectionUtil.java b/src/main/java/com/lin/util/HttpConnectionUtil.java
index 911d3cc58eac6eff83eee4995d8df13704134026..5ce1c3506d132adbbd4d33330031b6cc76d01adb 100644
--- a/src/main/java/com/lin/util/HttpConnectionUtil.java
+++ b/src/main/java/com/lin/util/HttpConnectionUtil.java
@@ -1,5 +1,6 @@
package com.lin.util;
+import com.lin.enums.ResponseStatusCode;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@@ -9,7 +10,7 @@ import java.net.URLEncoder;
/**
* Http连接工具
- *
+ *
* @author 九分石人, 2019-12-31
*/
@Slf4j
@@ -32,8 +33,8 @@ public class HttpConnectionUtil {
InputStream inputStream = null;
String result = "";
try {
- String newURL = getNewUrlByUrlEncodeParam(url);
- URL realUrl = new URL(newURL);
+ String newUrl = getNewUrlByUrlEncodeParam(url);
+ URL realUrl = new URL(newUrl);
String method = "GET";
connection = (HttpURLConnection) realUrl.openConnection();
@@ -45,7 +46,7 @@ public class HttpConnectionUtil {
connection.connect();
int responseCode = connection.getResponseCode();
- if (responseCode == 200) {
+ if (responseCode == ResponseStatusCode.OK.getCode()) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
@@ -73,8 +74,8 @@ public class HttpConnectionUtil {
InputStream inputStream = null;
String result = "";
try {
- String newURL = getNewUrlByUrlEncodeParam(url);
- URL realUrl = new URL(newURL);
+ String newUrl = getNewUrlByUrlEncodeParam(url);
+ URL realUrl = new URL(newUrl);
String method = "POST";
connection = (HttpURLConnection) realUrl.openConnection();
@@ -90,7 +91,7 @@ public class HttpConnectionUtil {
out.flush();
int responseCode = connection.getResponseCode();
- if (responseCode == 200) {
+ if (responseCode == ResponseStatusCode.OK.getCode()) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
@@ -167,7 +168,7 @@ public class HttpConnectionUtil {
*/
private static String getNewUrlByUrlEncodeParam(String url) throws UnsupportedEncodingException {
StringBuilder urlForEncode = new StringBuilder();
- String newURL;
+ String newUrl;
if (url.contains("=")) {
String[] urlBeforeEncode = url.split("=");
for (int i = 1; i < urlBeforeEncode.length; i++) {
@@ -179,13 +180,13 @@ public class HttpConnectionUtil {
if (i == urlBeforeEncode.length - 1) {
urlBeforeEncode[i] = URLEncoder.encode(urlBeforeEncode[i], encode);
}
- urlForEncode = urlForEncode.append(urlBeforeEncode[i]);
+ urlForEncode.append(urlBeforeEncode[i]);
if (i != urlBeforeEncode.length - 1) {
urlForEncode = urlForEncode.append("=");
}
}
- newURL = urlBeforeEncode[0] + "=" + urlForEncode.toString();
- return newURL;
+ newUrl = urlBeforeEncode[0] + "=" + urlForEncode.toString();
+ return newUrl;
}
return url;
}
diff --git a/src/main/java/com/lin/util/HttpUrlConnectionUtil.java b/src/main/java/com/lin/util/HttpUrlConnectionUtil.java
index 520aa756a195198e197ea38ecbf5c8dff2f53cf8..02aacb9b5dc174c5e3f5eecafcbe45c9809524f2 100644
--- a/src/main/java/com/lin/util/HttpUrlConnectionUtil.java
+++ b/src/main/java/com/lin/util/HttpUrlConnectionUtil.java
@@ -1,5 +1,6 @@
package com.lin.util;
+import com.lin.enums.ResponseStatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
@@ -8,6 +9,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
+import java.net.ResponseCache;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
@@ -37,7 +39,7 @@ public class HttpUrlConnectionUtil {
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
-// SSLSocketFactory ssl = HttpUrlConnectionUtil.getSSL();
+// SSLSocketFactory ssl = HttpUrlConnectionUtil.getSsl();
// https请求需要
// connection.setSSLSocketFactory(ssl);
connection.setDoOutput(true);
@@ -51,7 +53,7 @@ public class HttpUrlConnectionUtil {
int responseCode = connection.getResponseCode();
// 结果
String result;
- if (responseCode != 200) {
+ if (responseCode != ResponseStatusCode.OK.getCode()) {
result = IOUtils.toString(connection.getErrorStream());
} else {
result = IOUtils.toString(connection.getInputStream());
@@ -81,7 +83,7 @@ public class HttpUrlConnectionUtil {
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
-// SSLSocketFactory ssl = HttpUrlConnectionUtil.getSSL();
+// SSLSocketFactory ssl = HttpUrlConnectionUtil.getSsl();
// https请求需要
// connection.setSSLSocketFactory(ssl);
connection.setDoOutput(true);
@@ -98,7 +100,7 @@ public class HttpUrlConnectionUtil {
int responseCode = connection.getResponseCode();
String result;
- if (responseCode != 200) {
+ if (responseCode != ResponseStatusCode.OK.getCode()) {
result = IOUtils.toString(connection.getErrorStream());
} else {
result = IOUtils.toString(connection.getInputStream());
@@ -123,7 +125,7 @@ public class HttpUrlConnectionUtil {
* @throws KeyManagementException 异常
* @author com.lin, 2019-12-21
*/
- private static SSLSocketFactory getSSL() throws NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException {
+ private static SSLSocketFactory getSsl() throws NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustManager = new TrustManager[]{};
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, trustManager, new java.security.SecureRandom());
diff --git a/src/main/java/com/lin/util/ProvinceCityUtil.java b/src/main/java/com/lin/util/ProvinceCityUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..05cb4a716e8429ebbc55fa785d5bcbe71c401773
--- /dev/null
+++ b/src/main/java/com/lin/util/ProvinceCityUtil.java
@@ -0,0 +1,121 @@
+package com.lin.util;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author 九分石人
+ */
+@Slf4j
+public class ProvinceCityUtil {
+
+ /**
+ * 解析地址
+ *
+ * @param address 地址
+ * @return 解析后的地址
+ */
+ public static List