From 4f3d78d80d5b33e78cc5fe00fde86c19b53644db Mon Sep 17 00:00:00 2001 From: emptypoint <1215582715@qq.com> Date: Fri, 20 Jan 2023 18:55:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ArrayUtil=E4=B8=ADisSorted?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95=EF=BC=8C=E5=B9=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/ArrayUtil.java | 121 ++++++++++++++---- .../cn/hutool/core/util/ArrayUtilTest.java | 37 ++++++ 2 files changed, 133 insertions(+), 25 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java index 20245a69f..068e8ddda 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java @@ -215,11 +215,10 @@ public class ArrayUtil extends PrimitiveArrayUtil { @SuppressWarnings("unchecked") public static int matchIndex(final Predicate matcher, final int beginIndexInclude, final T... array) { if (isNotEmpty(array)) { - final int length = array.length; - if (null == matcher && beginIndexInclude < length) { + if (null == matcher && beginIndexInclude < array.length) { return beginIndexInclude; } - for (int i = beginIndexInclude; i < length; i++) { + for (int i = beginIndexInclude; i < array.length; i++) { if (matcher.test(array[i])) { return i; } @@ -1930,23 +1929,58 @@ public class ArrayUtil extends PrimitiveArrayUtil { // O(n)时间复杂度检查数组是否有序 /** - * 检查数组是否按升序排列,即 {@code comparator.compare(array[i], array[i + 1]) <= 0} - *

若传入空数组或空比较器,则返回{@code false}

+ * 检查数组是否有序,升序或者降序,使用指定比较器比较 + *

若传入空数组或空比较器,则返回{@code false};元素全部相等,返回 {@code true}

* - * @param array 数组 - * @param comparator 比较器 * @param 数组元素类型 - * @return 数组是否升序 + * @param array 数组 + * @param comparator 比较器,需要自己处理null值比较 + * @return 数组是否有序 * @since 6.0.0 */ public static boolean isSorted(final T[] array, final Comparator comparator) { - if (array == null || comparator == null) { + if (isEmpty(array) || null == comparator) { return false; } final int size = array.length - 1; + final int cmp = comparator.compare(array[0], array[size]); + if (cmp < 0) { + return isSortedASC(array, comparator); + } else if (cmp > 0) { + return isSortedDESC(array, comparator); + } for (int i = 0; i < size; i++) { - if (comparator.compare(array[i], array[i + 1]) > 0) { + if (comparator.compare(array[i], array[i + 1]) != 0) { + return false; + } + } + return true; + } + + /** + * 检查数组是否有序,升序或者降序 + *

若传入空数组,则返回{@code false};元素全部相等,返回 {@code true}

+ * + * @param 数组元素类型,该类型需要实现Comparable接口 + * @param array 数组 + * @return 数组是否有序 + * @since 6.0.0 + * @throws NullPointerException 如果数组元素含有null值 + */ + public static > boolean isSorted(final T[] array) { + if (isEmpty(array)) { + return false; + } + final int size = array.length - 1; + final int cmp = array[0].compareTo(array[size]); + if (cmp < 0) { + return isSortedASC(array); + } else if (cmp > 0) { + return isSortedDESC(array); + } + for (int i = 0; i < size; i++) { + if (array[i].compareTo(array[i + 1]) != 0) { return false; } } @@ -1962,21 +1996,7 @@ public class ArrayUtil extends PrimitiveArrayUtil { * @return 数组是否升序 * @author FengBaoheng * @since 5.5.2 - */ - public static > boolean isSorted(final T[] array) { - return isSortedASC(array); - } - - - /** - * 检查数组是否升序,即 {@code array[i].compareTo(array[i + 1]) <= 0} - *

若传入空数组,则返回{@code false}

- * - * @param 数组元素类型,该类型需要实现Comparable接口 - * @param array 数组 - * @return 数组是否升序 - * @author FengBaoheng - * @since 5.5.2 + * @throws NullPointerException 如果数组元素含有null值 */ public static > boolean isSortedASC(final T[] array) { if (isEmpty(array)) { @@ -2002,6 +2022,7 @@ public class ArrayUtil extends PrimitiveArrayUtil { * @return 数组是否降序 * @author FengBaoheng * @since 5.5.2 + * @throws NullPointerException 如果数组元素含有null值 */ public static > boolean isSortedDESC(final T[] array) { if (isEmpty(array)) { @@ -2017,4 +2038,54 @@ public class ArrayUtil extends PrimitiveArrayUtil { return true; } + + /** + * 检查数组是否升序,使用指定的比较器比较,即 {@code comparator.compare(array[i], array[i + 1]) <= 0} + *

若传入空数组或空比较器,则返回{@code false}

+ * + * @param 数组元素类型 + * @param array 数组 + * @param comparator 比较器,需要自己处理null值比较 + * @return 数组是否升序 + * @since 6.0.0 + */ + public static boolean isSortedASC(final T[] array, final Comparator comparator) { + if (isEmpty(array) || null == comparator) { + return false; + } + + final int size = array.length - 1; + for (int i = 0; i < size; i++) { + if (comparator.compare(array[i], array[i + 1]) > 0) { + return false; + } + } + + return true; + } + + /** + * 检查数组是否降序,使用指定的比较器比较,即 {@code comparator.compare(array[i], array[i + 1]) >= 0} + *

若传入空数组或空比较器,则返回{@code false}

+ * + * @param 数组元素类型 + * @param array 数组 + * @param comparator 比较器,需要自己处理null值比较 + * @return 数组是否降序 + * @since 6.0.0 + */ + public static boolean isSortedDESC(final T[] array, final Comparator comparator) { + if (isEmpty(array) || null == comparator) { + return false; + } + + final int size = array.length - 1; + for (int i = 0; i < size; i++) { + if (comparator.compare(array[i], array[i + 1]) < 0) { + return false; + } + } + + return true; + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java index 52a26ab15..d2223bbb2 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java @@ -636,4 +636,41 @@ public class ArrayUtilTest { Assert.assertArrayEquals(new int[]{2, 3, 4}, ArrayUtil.sub(arr, -1, 1)); Assert.assertArrayEquals(new int[]{2, 3, 4}, ArrayUtil.sub(arr, -4, -1)); } + + @Test + public void isSortedTest() { + final Integer[] a = {1, 1, 2, 2, 2, 3, 3}; + Assert.assertTrue(ArrayUtil.isSorted(a)); + Assert.assertTrue(ArrayUtil.isSorted(a, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(a, null)); + + final Integer[] b = {1, 1, 1, 1, 1, 1}; + Assert.assertTrue(ArrayUtil.isSorted(b)); + Assert.assertTrue(ArrayUtil.isSorted(b, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(a, null)); + + final Integer[] c = {3, 3, 2, 2, 2, 1, 1}; + Assert.assertTrue(ArrayUtil.isSorted(c)); + Assert.assertTrue(ArrayUtil.isSorted(c, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(a, null)); + + Assert.assertFalse(ArrayUtil.isSorted(null)); + Assert.assertFalse(ArrayUtil.isSorted(null, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(null, null)); + + final Integer[] d = {}; + Assert.assertFalse(ArrayUtil.isSorted(d)); + Assert.assertFalse(ArrayUtil.isSorted(d, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(d, null)); + + final Integer[] e = {1}; + Assert.assertTrue(ArrayUtil.isSorted(e)); + Assert.assertTrue(ArrayUtil.isSorted(e, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(e, null)); + + final Integer[] f = {1, 2}; + Assert.assertTrue(ArrayUtil.isSorted(f)); + Assert.assertTrue(ArrayUtil.isSorted(f, Integer::compareTo)); + Assert.assertFalse(ArrayUtil.isSorted(f, null)); + } }