1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.util; 28 29 import jdk.internal.HotSpotIntrinsicCandidate; 30 import jdk.internal.util.ArraysSupport; 31 32 import java.lang.reflect.Array; 33 import java.util.concurrent.ForkJoinPool; 34 import java.util.function.BinaryOperator; 35 import java.util.function.Consumer; 36 import java.util.function.DoubleBinaryOperator; 37 import java.util.function.IntBinaryOperator; 38 import java.util.function.IntFunction; 39 import java.util.function.IntToDoubleFunction; 40 import java.util.function.IntToLongFunction; 41 import java.util.function.IntUnaryOperator; 42 import java.util.function.LongBinaryOperator; 43 import java.util.function.UnaryOperator; 44 import java.util.stream.DoubleStream; 45 import java.util.stream.IntStream; 46 import java.util.stream.LongStream; 47 import java.util.stream.Stream; 48 import java.util.stream.StreamSupport; 49 50 /** 51 * This class contains various methods for manipulating arrays (such as 52 * sorting and searching). This class also contains a static factory 53 * that allows arrays to be viewed as lists. 54 * 55 * <p>The methods in this class all throw a {@code NullPointerException}, 56 * if the specified array reference is null, except where noted. 57 * 58 * <p>The documentation for the methods contained in this class includes 59 * brief descriptions of the <i>implementations</i>. Such descriptions should 60 * be regarded as <i>implementation notes</i>, rather than parts of the 61 * <i>specification</i>. Implementors should feel free to substitute other 62 * algorithms, so long as the specification itself is adhered to. (For 63 * example, the algorithm used by {@code sort(Object[])} does not have to be 64 * a MergeSort, but it does have to be <i>stable</i>.) 65 * 66 * <p>This class is a member of the 67 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> 68 * Java Collections Framework</a>. 69 * 70 * @author Josh Bloch 71 * @author Neal Gafter 72 * @author John Rose 73 * @since 1.2 74 */ 75 public class Arrays { 76 77 /** 78 * The minimum array length below which a parallel sorting 79 * algorithm will not further partition the sorting task. Using 80 * smaller sizes typically results in memory contention across 81 * tasks that makes parallel speedups unlikely. 82 * @hide 83 */ 84 // Android-changed: Make MIN_ARRAY_SORT_GRAN public and @hide (used by harmony ArraysTest). 85 public static final int MIN_ARRAY_SORT_GRAN = 1 << 13; 86 87 // Suppresses default constructor, ensuring non-instantiability. Arrays()88 private Arrays() {} 89 90 /** 91 * A comparator that implements the natural ordering of a group of 92 * mutually comparable elements. May be used when a supplied 93 * comparator is null. To simplify code-sharing within underlying 94 * implementations, the compare method only declares type Object 95 * for its second argument. 96 * 97 * Arrays class implementor's note: It is an empirical matter 98 * whether ComparableTimSort offers any performance benefit over 99 * TimSort used with this comparator. If not, you are better off 100 * deleting or bypassing ComparableTimSort. There is currently no 101 * empirical case for separating them for parallel sorting, so all 102 * public Object parallelSort methods use the same comparator 103 * based implementation. 104 */ 105 static final class NaturalOrder implements Comparator<Object> { 106 @SuppressWarnings("unchecked") compare(Object first, Object second)107 public int compare(Object first, Object second) { 108 return ((Comparable<Object>)first).compareTo(second); 109 } 110 static final NaturalOrder INSTANCE = new NaturalOrder(); 111 } 112 113 /** 114 * Checks that {@code fromIndex} and {@code toIndex} are in 115 * the range and throws an exception if they aren't. 116 */ rangeCheck(int arrayLength, int fromIndex, int toIndex)117 static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { 118 if (fromIndex > toIndex) { 119 throw new IllegalArgumentException( 120 "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); 121 } 122 if (fromIndex < 0) { 123 throw new ArrayIndexOutOfBoundsException(fromIndex); 124 } 125 if (toIndex > arrayLength) { 126 throw new ArrayIndexOutOfBoundsException(toIndex); 127 } 128 } 129 130 /* 131 * Sorting methods. Note that all public "sort" methods take the 132 * same form: Performing argument checks if necessary, and then 133 * expanding arguments into those required for the internal 134 * implementation methods residing in other package-private 135 * classes (except for legacyMergeSort, included in this class). 136 */ 137 138 /** 139 * Sorts the specified array into ascending numerical order. 140 * 141 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 142 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 143 * offers O(n log(n)) performance on many data sets that cause other 144 * quicksorts to degrade to quadratic performance, and is typically 145 * faster than traditional (one-pivot) Quicksort implementations. 146 * 147 * @param a the array to be sorted 148 */ sort(int[] a)149 public static void sort(int[] a) { 150 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 151 } 152 153 /** 154 * Sorts the specified range of the array into ascending order. The range 155 * to be sorted extends from the index {@code fromIndex}, inclusive, to 156 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 157 * the range to be sorted is empty. 158 * 159 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 160 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 161 * offers O(n log(n)) performance on many data sets that cause other 162 * quicksorts to degrade to quadratic performance, and is typically 163 * faster than traditional (one-pivot) Quicksort implementations. 164 * 165 * @param a the array to be sorted 166 * @param fromIndex the index of the first element, inclusive, to be sorted 167 * @param toIndex the index of the last element, exclusive, to be sorted 168 * 169 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 170 * @throws ArrayIndexOutOfBoundsException 171 * if {@code fromIndex < 0} or {@code toIndex > a.length} 172 */ sort(int[] a, int fromIndex, int toIndex)173 public static void sort(int[] a, int fromIndex, int toIndex) { 174 rangeCheck(a.length, fromIndex, toIndex); 175 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 176 } 177 178 /** 179 * Sorts the specified array into ascending numerical order. 180 * 181 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 182 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 183 * offers O(n log(n)) performance on many data sets that cause other 184 * quicksorts to degrade to quadratic performance, and is typically 185 * faster than traditional (one-pivot) Quicksort implementations. 186 * 187 * @param a the array to be sorted 188 */ sort(long[] a)189 public static void sort(long[] a) { 190 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 191 } 192 193 /** 194 * Sorts the specified range of the array into ascending order. The range 195 * to be sorted extends from the index {@code fromIndex}, inclusive, to 196 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 197 * the range to be sorted is empty. 198 * 199 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 200 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 201 * offers O(n log(n)) performance on many data sets that cause other 202 * quicksorts to degrade to quadratic performance, and is typically 203 * faster than traditional (one-pivot) Quicksort implementations. 204 * 205 * @param a the array to be sorted 206 * @param fromIndex the index of the first element, inclusive, to be sorted 207 * @param toIndex the index of the last element, exclusive, to be sorted 208 * 209 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 210 * @throws ArrayIndexOutOfBoundsException 211 * if {@code fromIndex < 0} or {@code toIndex > a.length} 212 */ sort(long[] a, int fromIndex, int toIndex)213 public static void sort(long[] a, int fromIndex, int toIndex) { 214 rangeCheck(a.length, fromIndex, toIndex); 215 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 216 } 217 218 /** 219 * Sorts the specified array into ascending numerical order. 220 * 221 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 222 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 223 * offers O(n log(n)) performance on many data sets that cause other 224 * quicksorts to degrade to quadratic performance, and is typically 225 * faster than traditional (one-pivot) Quicksort implementations. 226 * 227 * @param a the array to be sorted 228 */ sort(short[] a)229 public static void sort(short[] a) { 230 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 231 } 232 233 /** 234 * Sorts the specified range of the array into ascending order. The range 235 * to be sorted extends from the index {@code fromIndex}, inclusive, to 236 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 237 * the range to be sorted is empty. 238 * 239 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 240 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 241 * offers O(n log(n)) performance on many data sets that cause other 242 * quicksorts to degrade to quadratic performance, and is typically 243 * faster than traditional (one-pivot) Quicksort implementations. 244 * 245 * @param a the array to be sorted 246 * @param fromIndex the index of the first element, inclusive, to be sorted 247 * @param toIndex the index of the last element, exclusive, to be sorted 248 * 249 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 250 * @throws ArrayIndexOutOfBoundsException 251 * if {@code fromIndex < 0} or {@code toIndex > a.length} 252 */ sort(short[] a, int fromIndex, int toIndex)253 public static void sort(short[] a, int fromIndex, int toIndex) { 254 rangeCheck(a.length, fromIndex, toIndex); 255 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 256 } 257 258 /** 259 * Sorts the specified array into ascending numerical order. 260 * 261 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 262 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 263 * offers O(n log(n)) performance on many data sets that cause other 264 * quicksorts to degrade to quadratic performance, and is typically 265 * faster than traditional (one-pivot) Quicksort implementations. 266 * 267 * @param a the array to be sorted 268 */ sort(char[] a)269 public static void sort(char[] a) { 270 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 271 } 272 273 /** 274 * Sorts the specified range of the array into ascending order. The range 275 * to be sorted extends from the index {@code fromIndex}, inclusive, to 276 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 277 * the range to be sorted is empty. 278 * 279 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 280 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 281 * offers O(n log(n)) performance on many data sets that cause other 282 * quicksorts to degrade to quadratic performance, and is typically 283 * faster than traditional (one-pivot) Quicksort implementations. 284 * 285 * @param a the array to be sorted 286 * @param fromIndex the index of the first element, inclusive, to be sorted 287 * @param toIndex the index of the last element, exclusive, to be sorted 288 * 289 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 290 * @throws ArrayIndexOutOfBoundsException 291 * if {@code fromIndex < 0} or {@code toIndex > a.length} 292 */ sort(char[] a, int fromIndex, int toIndex)293 public static void sort(char[] a, int fromIndex, int toIndex) { 294 rangeCheck(a.length, fromIndex, toIndex); 295 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 296 } 297 298 /** 299 * Sorts the specified array into ascending numerical order. 300 * 301 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 302 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 303 * offers O(n log(n)) performance on many data sets that cause other 304 * quicksorts to degrade to quadratic performance, and is typically 305 * faster than traditional (one-pivot) Quicksort implementations. 306 * 307 * @param a the array to be sorted 308 */ sort(byte[] a)309 public static void sort(byte[] a) { 310 DualPivotQuicksort.sort(a, 0, a.length - 1); 311 } 312 313 /** 314 * Sorts the specified range of the array into ascending order. The range 315 * to be sorted extends from the index {@code fromIndex}, inclusive, to 316 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 317 * the range to be sorted is empty. 318 * 319 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 320 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 321 * offers O(n log(n)) performance on many data sets that cause other 322 * quicksorts to degrade to quadratic performance, and is typically 323 * faster than traditional (one-pivot) Quicksort implementations. 324 * 325 * @param a the array to be sorted 326 * @param fromIndex the index of the first element, inclusive, to be sorted 327 * @param toIndex the index of the last element, exclusive, to be sorted 328 * 329 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 330 * @throws ArrayIndexOutOfBoundsException 331 * if {@code fromIndex < 0} or {@code toIndex > a.length} 332 */ sort(byte[] a, int fromIndex, int toIndex)333 public static void sort(byte[] a, int fromIndex, int toIndex) { 334 rangeCheck(a.length, fromIndex, toIndex); 335 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 336 } 337 338 /** 339 * Sorts the specified array into ascending numerical order. 340 * 341 * <p>The {@code <} relation does not provide a total order on all float 342 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 343 * value compares neither less than, greater than, nor equal to any value, 344 * even itself. This method uses the total order imposed by the method 345 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 346 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 347 * other value and all {@code Float.NaN} values are considered equal. 348 * 349 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 350 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 351 * offers O(n log(n)) performance on many data sets that cause other 352 * quicksorts to degrade to quadratic performance, and is typically 353 * faster than traditional (one-pivot) Quicksort implementations. 354 * 355 * @param a the array to be sorted 356 */ sort(float[] a)357 public static void sort(float[] a) { 358 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 359 } 360 361 /** 362 * Sorts the specified range of the array into ascending order. The range 363 * to be sorted extends from the index {@code fromIndex}, inclusive, to 364 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 365 * the range to be sorted is empty. 366 * 367 * <p>The {@code <} relation does not provide a total order on all float 368 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 369 * value compares neither less than, greater than, nor equal to any value, 370 * even itself. This method uses the total order imposed by the method 371 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 372 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 373 * other value and all {@code Float.NaN} values are considered equal. 374 * 375 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 376 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 377 * offers O(n log(n)) performance on many data sets that cause other 378 * quicksorts to degrade to quadratic performance, and is typically 379 * faster than traditional (one-pivot) Quicksort implementations. 380 * 381 * @param a the array to be sorted 382 * @param fromIndex the index of the first element, inclusive, to be sorted 383 * @param toIndex the index of the last element, exclusive, to be sorted 384 * 385 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 386 * @throws ArrayIndexOutOfBoundsException 387 * if {@code fromIndex < 0} or {@code toIndex > a.length} 388 */ sort(float[] a, int fromIndex, int toIndex)389 public static void sort(float[] a, int fromIndex, int toIndex) { 390 rangeCheck(a.length, fromIndex, toIndex); 391 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 392 } 393 394 /** 395 * Sorts the specified array into ascending numerical order. 396 * 397 * <p>The {@code <} relation does not provide a total order on all double 398 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 399 * value compares neither less than, greater than, nor equal to any value, 400 * even itself. This method uses the total order imposed by the method 401 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 402 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 403 * other value and all {@code Double.NaN} values are considered equal. 404 * 405 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 406 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 407 * offers O(n log(n)) performance on many data sets that cause other 408 * quicksorts to degrade to quadratic performance, and is typically 409 * faster than traditional (one-pivot) Quicksort implementations. 410 * 411 * @param a the array to be sorted 412 */ sort(double[] a)413 public static void sort(double[] a) { 414 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 415 } 416 417 /** 418 * Sorts the specified range of the array into ascending order. The range 419 * to be sorted extends from the index {@code fromIndex}, inclusive, to 420 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 421 * the range to be sorted is empty. 422 * 423 * <p>The {@code <} relation does not provide a total order on all double 424 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 425 * value compares neither less than, greater than, nor equal to any value, 426 * even itself. This method uses the total order imposed by the method 427 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 428 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 429 * other value and all {@code Double.NaN} values are considered equal. 430 * 431 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 432 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 433 * offers O(n log(n)) performance on many data sets that cause other 434 * quicksorts to degrade to quadratic performance, and is typically 435 * faster than traditional (one-pivot) Quicksort implementations. 436 * 437 * @param a the array to be sorted 438 * @param fromIndex the index of the first element, inclusive, to be sorted 439 * @param toIndex the index of the last element, exclusive, to be sorted 440 * 441 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 442 * @throws ArrayIndexOutOfBoundsException 443 * if {@code fromIndex < 0} or {@code toIndex > a.length} 444 */ sort(double[] a, int fromIndex, int toIndex)445 public static void sort(double[] a, int fromIndex, int toIndex) { 446 rangeCheck(a.length, fromIndex, toIndex); 447 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 448 } 449 450 /** 451 * Sorts the specified array into ascending numerical order. 452 * 453 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 454 * array into sub-arrays that are themselves sorted and then merged. When 455 * the sub-array length reaches a minimum granularity, the sub-array is 456 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 457 * method. If the length of the specified array is less than the minimum 458 * granularity, then it is sorted using the appropriate {@link 459 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a 460 * working space no greater than the size of the original array. The 461 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 462 * execute any parallel tasks. 463 * 464 * @param a the array to be sorted 465 * 466 * @since 1.8 467 */ parallelSort(byte[] a)468 public static void parallelSort(byte[] a) { 469 int n = a.length, p, g; 470 if (n <= MIN_ARRAY_SORT_GRAN || 471 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 472 DualPivotQuicksort.sort(a, 0, n - 1); 473 else 474 new ArraysParallelSortHelpers.FJByte.Sorter 475 (null, a, new byte[n], 0, n, 0, 476 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 477 MIN_ARRAY_SORT_GRAN : g).invoke(); 478 } 479 480 /** 481 * Sorts the specified range of the array into ascending numerical order. 482 * The range to be sorted extends from the index {@code fromIndex}, 483 * inclusive, to the index {@code toIndex}, exclusive. If 484 * {@code fromIndex == toIndex}, the range to be sorted is empty. 485 * 486 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 487 * array into sub-arrays that are themselves sorted and then merged. When 488 * the sub-array length reaches a minimum granularity, the sub-array is 489 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 490 * method. If the length of the specified array is less than the minimum 491 * granularity, then it is sorted using the appropriate {@link 492 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working 493 * space no greater than the size of the specified range of the original 494 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 495 * used to execute any parallel tasks. 496 * 497 * @param a the array to be sorted 498 * @param fromIndex the index of the first element, inclusive, to be sorted 499 * @param toIndex the index of the last element, exclusive, to be sorted 500 * 501 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 502 * @throws ArrayIndexOutOfBoundsException 503 * if {@code fromIndex < 0} or {@code toIndex > a.length} 504 * 505 * @since 1.8 506 */ parallelSort(byte[] a, int fromIndex, int toIndex)507 public static void parallelSort(byte[] a, int fromIndex, int toIndex) { 508 rangeCheck(a.length, fromIndex, toIndex); 509 int n = toIndex - fromIndex, p, g; 510 if (n <= MIN_ARRAY_SORT_GRAN || 511 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 512 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 513 else 514 new ArraysParallelSortHelpers.FJByte.Sorter 515 (null, a, new byte[n], fromIndex, n, 0, 516 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 517 MIN_ARRAY_SORT_GRAN : g).invoke(); 518 } 519 520 /** 521 * Sorts the specified array into ascending numerical order. 522 * 523 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 524 * array into sub-arrays that are themselves sorted and then merged. When 525 * the sub-array length reaches a minimum granularity, the sub-array is 526 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 527 * method. If the length of the specified array is less than the minimum 528 * granularity, then it is sorted using the appropriate {@link 529 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a 530 * working space no greater than the size of the original array. The 531 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 532 * execute any parallel tasks. 533 * 534 * @param a the array to be sorted 535 * 536 * @since 1.8 537 */ parallelSort(char[] a)538 public static void parallelSort(char[] a) { 539 int n = a.length, p, g; 540 if (n <= MIN_ARRAY_SORT_GRAN || 541 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 542 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 543 else 544 new ArraysParallelSortHelpers.FJChar.Sorter 545 (null, a, new char[n], 0, n, 0, 546 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 547 MIN_ARRAY_SORT_GRAN : g).invoke(); 548 } 549 550 /** 551 * Sorts the specified range of the array into ascending numerical order. 552 * The range to be sorted extends from the index {@code fromIndex}, 553 * inclusive, to the index {@code toIndex}, exclusive. If 554 * {@code fromIndex == toIndex}, the range to be sorted is empty. 555 * 556 @implNote The sorting algorithm is a parallel sort-merge that breaks the 557 * array into sub-arrays that are themselves sorted and then merged. When 558 * the sub-array length reaches a minimum granularity, the sub-array is 559 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 560 * method. If the length of the specified array is less than the minimum 561 * granularity, then it is sorted using the appropriate {@link 562 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working 563 * space no greater than the size of the specified range of the original 564 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 565 * used to execute any parallel tasks. 566 * 567 * @param a the array to be sorted 568 * @param fromIndex the index of the first element, inclusive, to be sorted 569 * @param toIndex the index of the last element, exclusive, to be sorted 570 * 571 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 572 * @throws ArrayIndexOutOfBoundsException 573 * if {@code fromIndex < 0} or {@code toIndex > a.length} 574 * 575 * @since 1.8 576 */ parallelSort(char[] a, int fromIndex, int toIndex)577 public static void parallelSort(char[] a, int fromIndex, int toIndex) { 578 rangeCheck(a.length, fromIndex, toIndex); 579 int n = toIndex - fromIndex, p, g; 580 if (n <= MIN_ARRAY_SORT_GRAN || 581 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 582 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 583 else 584 new ArraysParallelSortHelpers.FJChar.Sorter 585 (null, a, new char[n], fromIndex, n, 0, 586 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 587 MIN_ARRAY_SORT_GRAN : g).invoke(); 588 } 589 590 /** 591 * Sorts the specified array into ascending numerical order. 592 * 593 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 594 * array into sub-arrays that are themselves sorted and then merged. When 595 * the sub-array length reaches a minimum granularity, the sub-array is 596 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 597 * method. If the length of the specified array is less than the minimum 598 * granularity, then it is sorted using the appropriate {@link 599 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a 600 * working space no greater than the size of the original array. The 601 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 602 * execute any parallel tasks. 603 * 604 * @param a the array to be sorted 605 * 606 * @since 1.8 607 */ parallelSort(short[] a)608 public static void parallelSort(short[] a) { 609 int n = a.length, p, g; 610 if (n <= MIN_ARRAY_SORT_GRAN || 611 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 612 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 613 else 614 new ArraysParallelSortHelpers.FJShort.Sorter 615 (null, a, new short[n], 0, n, 0, 616 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 617 MIN_ARRAY_SORT_GRAN : g).invoke(); 618 } 619 620 /** 621 * Sorts the specified range of the array into ascending numerical order. 622 * The range to be sorted extends from the index {@code fromIndex}, 623 * inclusive, to the index {@code toIndex}, exclusive. If 624 * {@code fromIndex == toIndex}, the range to be sorted is empty. 625 * 626 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 627 * array into sub-arrays that are themselves sorted and then merged. When 628 * the sub-array length reaches a minimum granularity, the sub-array is 629 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 630 * method. If the length of the specified array is less than the minimum 631 * granularity, then it is sorted using the appropriate {@link 632 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working 633 * space no greater than the size of the specified range of the original 634 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 635 * used to execute any parallel tasks. 636 * 637 * @param a the array to be sorted 638 * @param fromIndex the index of the first element, inclusive, to be sorted 639 * @param toIndex the index of the last element, exclusive, to be sorted 640 * 641 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 642 * @throws ArrayIndexOutOfBoundsException 643 * if {@code fromIndex < 0} or {@code toIndex > a.length} 644 * 645 * @since 1.8 646 */ parallelSort(short[] a, int fromIndex, int toIndex)647 public static void parallelSort(short[] a, int fromIndex, int toIndex) { 648 rangeCheck(a.length, fromIndex, toIndex); 649 int n = toIndex - fromIndex, p, g; 650 if (n <= MIN_ARRAY_SORT_GRAN || 651 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 652 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 653 else 654 new ArraysParallelSortHelpers.FJShort.Sorter 655 (null, a, new short[n], fromIndex, n, 0, 656 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 657 MIN_ARRAY_SORT_GRAN : g).invoke(); 658 } 659 660 /** 661 * Sorts the specified array into ascending numerical order. 662 * 663 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 664 * array into sub-arrays that are themselves sorted and then merged. When 665 * the sub-array length reaches a minimum granularity, the sub-array is 666 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 667 * method. If the length of the specified array is less than the minimum 668 * granularity, then it is sorted using the appropriate {@link 669 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a 670 * working space no greater than the size of the original array. The 671 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 672 * execute any parallel tasks. 673 * 674 * @param a the array to be sorted 675 * 676 * @since 1.8 677 */ parallelSort(int[] a)678 public static void parallelSort(int[] a) { 679 int n = a.length, p, g; 680 if (n <= MIN_ARRAY_SORT_GRAN || 681 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 682 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 683 else 684 new ArraysParallelSortHelpers.FJInt.Sorter 685 (null, a, new int[n], 0, n, 0, 686 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 687 MIN_ARRAY_SORT_GRAN : g).invoke(); 688 } 689 690 /** 691 * Sorts the specified range of the array into ascending numerical order. 692 * The range to be sorted extends from the index {@code fromIndex}, 693 * inclusive, to the index {@code toIndex}, exclusive. If 694 * {@code fromIndex == toIndex}, the range to be sorted is empty. 695 * 696 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 697 * array into sub-arrays that are themselves sorted and then merged. When 698 * the sub-array length reaches a minimum granularity, the sub-array is 699 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 700 * method. If the length of the specified array is less than the minimum 701 * granularity, then it is sorted using the appropriate {@link 702 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working 703 * space no greater than the size of the specified range of the original 704 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 705 * used to execute any parallel tasks. 706 * 707 * @param a the array to be sorted 708 * @param fromIndex the index of the first element, inclusive, to be sorted 709 * @param toIndex the index of the last element, exclusive, to be sorted 710 * 711 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 712 * @throws ArrayIndexOutOfBoundsException 713 * if {@code fromIndex < 0} or {@code toIndex > a.length} 714 * 715 * @since 1.8 716 */ parallelSort(int[] a, int fromIndex, int toIndex)717 public static void parallelSort(int[] a, int fromIndex, int toIndex) { 718 rangeCheck(a.length, fromIndex, toIndex); 719 int n = toIndex - fromIndex, p, g; 720 if (n <= MIN_ARRAY_SORT_GRAN || 721 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 722 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 723 else 724 new ArraysParallelSortHelpers.FJInt.Sorter 725 (null, a, new int[n], fromIndex, n, 0, 726 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 727 MIN_ARRAY_SORT_GRAN : g).invoke(); 728 } 729 730 /** 731 * Sorts the specified array into ascending numerical order. 732 * 733 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 734 * array into sub-arrays that are themselves sorted and then merged. When 735 * the sub-array length reaches a minimum granularity, the sub-array is 736 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 737 * method. If the length of the specified array is less than the minimum 738 * granularity, then it is sorted using the appropriate {@link 739 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a 740 * working space no greater than the size of the original array. The 741 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 742 * execute any parallel tasks. 743 * 744 * @param a the array to be sorted 745 * 746 * @since 1.8 747 */ parallelSort(long[] a)748 public static void parallelSort(long[] a) { 749 int n = a.length, p, g; 750 if (n <= MIN_ARRAY_SORT_GRAN || 751 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 752 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 753 else 754 new ArraysParallelSortHelpers.FJLong.Sorter 755 (null, a, new long[n], 0, n, 0, 756 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 757 MIN_ARRAY_SORT_GRAN : g).invoke(); 758 } 759 760 /** 761 * Sorts the specified range of the array into ascending numerical order. 762 * The range to be sorted extends from the index {@code fromIndex}, 763 * inclusive, to the index {@code toIndex}, exclusive. If 764 * {@code fromIndex == toIndex}, the range to be sorted is empty. 765 * 766 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 767 * array into sub-arrays that are themselves sorted and then merged. When 768 * the sub-array length reaches a minimum granularity, the sub-array is 769 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 770 * method. If the length of the specified array is less than the minimum 771 * granularity, then it is sorted using the appropriate {@link 772 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working 773 * space no greater than the size of the specified range of the original 774 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 775 * used to execute any parallel tasks. 776 * 777 * @param a the array to be sorted 778 * @param fromIndex the index of the first element, inclusive, to be sorted 779 * @param toIndex the index of the last element, exclusive, to be sorted 780 * 781 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 782 * @throws ArrayIndexOutOfBoundsException 783 * if {@code fromIndex < 0} or {@code toIndex > a.length} 784 * 785 * @since 1.8 786 */ parallelSort(long[] a, int fromIndex, int toIndex)787 public static void parallelSort(long[] a, int fromIndex, int toIndex) { 788 rangeCheck(a.length, fromIndex, toIndex); 789 int n = toIndex - fromIndex, p, g; 790 if (n <= MIN_ARRAY_SORT_GRAN || 791 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 792 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 793 else 794 new ArraysParallelSortHelpers.FJLong.Sorter 795 (null, a, new long[n], fromIndex, n, 0, 796 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 797 MIN_ARRAY_SORT_GRAN : g).invoke(); 798 } 799 800 /** 801 * Sorts the specified array into ascending numerical order. 802 * 803 * <p>The {@code <} relation does not provide a total order on all float 804 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 805 * value compares neither less than, greater than, nor equal to any value, 806 * even itself. This method uses the total order imposed by the method 807 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 808 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 809 * other value and all {@code Float.NaN} values are considered equal. 810 * 811 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 812 * array into sub-arrays that are themselves sorted and then merged. When 813 * the sub-array length reaches a minimum granularity, the sub-array is 814 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 815 * method. If the length of the specified array is less than the minimum 816 * granularity, then it is sorted using the appropriate {@link 817 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a 818 * working space no greater than the size of the original array. The 819 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 820 * execute any parallel tasks. 821 * 822 * @param a the array to be sorted 823 * 824 * @since 1.8 825 */ parallelSort(float[] a)826 public static void parallelSort(float[] a) { 827 int n = a.length, p, g; 828 if (n <= MIN_ARRAY_SORT_GRAN || 829 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 830 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 831 else 832 new ArraysParallelSortHelpers.FJFloat.Sorter 833 (null, a, new float[n], 0, n, 0, 834 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 835 MIN_ARRAY_SORT_GRAN : g).invoke(); 836 } 837 838 /** 839 * Sorts the specified range of the array into ascending numerical order. 840 * The range to be sorted extends from the index {@code fromIndex}, 841 * inclusive, to the index {@code toIndex}, exclusive. If 842 * {@code fromIndex == toIndex}, the range to be sorted is empty. 843 * 844 * <p>The {@code <} relation does not provide a total order on all float 845 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 846 * value compares neither less than, greater than, nor equal to any value, 847 * even itself. This method uses the total order imposed by the method 848 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 849 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 850 * other value and all {@code Float.NaN} values are considered equal. 851 * 852 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 853 * array into sub-arrays that are themselves sorted and then merged. When 854 * the sub-array length reaches a minimum granularity, the sub-array is 855 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 856 * method. If the length of the specified array is less than the minimum 857 * granularity, then it is sorted using the appropriate {@link 858 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working 859 * space no greater than the size of the specified range of the original 860 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 861 * used to execute any parallel tasks. 862 * 863 * @param a the array to be sorted 864 * @param fromIndex the index of the first element, inclusive, to be sorted 865 * @param toIndex the index of the last element, exclusive, to be sorted 866 * 867 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 868 * @throws ArrayIndexOutOfBoundsException 869 * if {@code fromIndex < 0} or {@code toIndex > a.length} 870 * 871 * @since 1.8 872 */ parallelSort(float[] a, int fromIndex, int toIndex)873 public static void parallelSort(float[] a, int fromIndex, int toIndex) { 874 rangeCheck(a.length, fromIndex, toIndex); 875 int n = toIndex - fromIndex, p, g; 876 if (n <= MIN_ARRAY_SORT_GRAN || 877 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 878 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 879 else 880 new ArraysParallelSortHelpers.FJFloat.Sorter 881 (null, a, new float[n], fromIndex, n, 0, 882 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 883 MIN_ARRAY_SORT_GRAN : g).invoke(); 884 } 885 886 /** 887 * Sorts the specified array into ascending numerical order. 888 * 889 * <p>The {@code <} relation does not provide a total order on all double 890 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 891 * value compares neither less than, greater than, nor equal to any value, 892 * even itself. This method uses the total order imposed by the method 893 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 894 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 895 * other value and all {@code Double.NaN} values are considered equal. 896 * 897 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 898 * array into sub-arrays that are themselves sorted and then merged. When 899 * the sub-array length reaches a minimum granularity, the sub-array is 900 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 901 * method. If the length of the specified array is less than the minimum 902 * granularity, then it is sorted using the appropriate {@link 903 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a 904 * working space no greater than the size of the original array. The 905 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 906 * execute any parallel tasks. 907 * 908 * @param a the array to be sorted 909 * 910 * @since 1.8 911 */ parallelSort(double[] a)912 public static void parallelSort(double[] a) { 913 int n = a.length, p, g; 914 if (n <= MIN_ARRAY_SORT_GRAN || 915 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 916 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 917 else 918 new ArraysParallelSortHelpers.FJDouble.Sorter 919 (null, a, new double[n], 0, n, 0, 920 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 921 MIN_ARRAY_SORT_GRAN : g).invoke(); 922 } 923 924 /** 925 * Sorts the specified range of the array into ascending numerical order. 926 * The range to be sorted extends from the index {@code fromIndex}, 927 * inclusive, to the index {@code toIndex}, exclusive. If 928 * {@code fromIndex == toIndex}, the range to be sorted is empty. 929 * 930 * <p>The {@code <} relation does not provide a total order on all double 931 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 932 * value compares neither less than, greater than, nor equal to any value, 933 * even itself. This method uses the total order imposed by the method 934 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 935 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 936 * other value and all {@code Double.NaN} values are considered equal. 937 * 938 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 939 * array into sub-arrays that are themselves sorted and then merged. When 940 * the sub-array length reaches a minimum granularity, the sub-array is 941 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 942 * method. If the length of the specified array is less than the minimum 943 * granularity, then it is sorted using the appropriate {@link 944 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working 945 * space no greater than the size of the specified range of the original 946 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 947 * used to execute any parallel tasks. 948 * 949 * @param a the array to be sorted 950 * @param fromIndex the index of the first element, inclusive, to be sorted 951 * @param toIndex the index of the last element, exclusive, to be sorted 952 * 953 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 954 * @throws ArrayIndexOutOfBoundsException 955 * if {@code fromIndex < 0} or {@code toIndex > a.length} 956 * 957 * @since 1.8 958 */ parallelSort(double[] a, int fromIndex, int toIndex)959 public static void parallelSort(double[] a, int fromIndex, int toIndex) { 960 rangeCheck(a.length, fromIndex, toIndex); 961 int n = toIndex - fromIndex, p, g; 962 if (n <= MIN_ARRAY_SORT_GRAN || 963 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 964 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 965 else 966 new ArraysParallelSortHelpers.FJDouble.Sorter 967 (null, a, new double[n], fromIndex, n, 0, 968 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 969 MIN_ARRAY_SORT_GRAN : g).invoke(); 970 } 971 972 /** 973 * Sorts the specified array of objects into ascending order, according 974 * to the {@linkplain Comparable natural ordering} of its elements. 975 * All elements in the array must implement the {@link Comparable} 976 * interface. Furthermore, all elements in the array must be 977 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 978 * not throw a {@code ClassCastException} for any elements {@code e1} 979 * and {@code e2} in the array). 980 * 981 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 982 * not be reordered as a result of the sort. 983 * 984 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 985 * array into sub-arrays that are themselves sorted and then merged. When 986 * the sub-array length reaches a minimum granularity, the sub-array is 987 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 988 * method. If the length of the specified array is less than the minimum 989 * granularity, then it is sorted using the appropriate {@link 990 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 991 * working space no greater than the size of the original array. The 992 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 993 * execute any parallel tasks. 994 * 995 * @param <T> the class of the objects to be sorted 996 * @param a the array to be sorted 997 * 998 * @throws ClassCastException if the array contains elements that are not 999 * <i>mutually comparable</i> (for example, strings and integers) 1000 * @throws IllegalArgumentException (optional) if the natural 1001 * ordering of the array elements is found to violate the 1002 * {@link Comparable} contract 1003 * 1004 * @since 1.8 1005 */ 1006 @SuppressWarnings("unchecked") parallelSort(T[] a)1007 public static <T extends Comparable<? super T>> void parallelSort(T[] a) { 1008 int n = a.length, p, g; 1009 if (n <= MIN_ARRAY_SORT_GRAN || 1010 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1011 TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0); 1012 else 1013 new ArraysParallelSortHelpers.FJObject.Sorter<> 1014 (null, a, 1015 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1016 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1017 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1018 } 1019 1020 /** 1021 * Sorts the specified range of the specified array of objects into 1022 * ascending order, according to the 1023 * {@linkplain Comparable natural ordering} of its 1024 * elements. The range to be sorted extends from index 1025 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 1026 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 1027 * elements in this range must implement the {@link Comparable} 1028 * interface. Furthermore, all elements in this range must be <i>mutually 1029 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1030 * {@code ClassCastException} for any elements {@code e1} and 1031 * {@code e2} in the array). 1032 * 1033 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1034 * not be reordered as a result of the sort. 1035 * 1036 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1037 * array into sub-arrays that are themselves sorted and then merged. When 1038 * the sub-array length reaches a minimum granularity, the sub-array is 1039 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1040 * method. If the length of the specified array is less than the minimum 1041 * granularity, then it is sorted using the appropriate {@link 1042 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1043 * space no greater than the size of the specified range of the original 1044 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1045 * used to execute any parallel tasks. 1046 * 1047 * @param <T> the class of the objects to be sorted 1048 * @param a the array to be sorted 1049 * @param fromIndex the index of the first element (inclusive) to be 1050 * sorted 1051 * @param toIndex the index of the last element (exclusive) to be sorted 1052 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1053 * (optional) if the natural ordering of the array elements is 1054 * found to violate the {@link Comparable} contract 1055 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1056 * {@code toIndex > a.length} 1057 * @throws ClassCastException if the array contains elements that are 1058 * not <i>mutually comparable</i> (for example, strings and 1059 * integers). 1060 * 1061 * @since 1.8 1062 */ 1063 @SuppressWarnings("unchecked") 1064 public static <T extends Comparable<? super T>> parallelSort(T[] a, int fromIndex, int toIndex)1065 void parallelSort(T[] a, int fromIndex, int toIndex) { 1066 rangeCheck(a.length, fromIndex, toIndex); 1067 int n = toIndex - fromIndex, p, g; 1068 if (n <= MIN_ARRAY_SORT_GRAN || 1069 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1070 TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0); 1071 else 1072 new ArraysParallelSortHelpers.FJObject.Sorter<> 1073 (null, a, 1074 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1075 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1076 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1077 } 1078 1079 /** 1080 * Sorts the specified array of objects according to the order induced by 1081 * the specified comparator. All elements in the array must be 1082 * <i>mutually comparable</i> by the specified comparator (that is, 1083 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1084 * for any elements {@code e1} and {@code e2} in the array). 1085 * 1086 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1087 * not be reordered as a result of the sort. 1088 * 1089 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1090 * array into sub-arrays that are themselves sorted and then merged. When 1091 * the sub-array length reaches a minimum granularity, the sub-array is 1092 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1093 * method. If the length of the specified array is less than the minimum 1094 * granularity, then it is sorted using the appropriate {@link 1095 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 1096 * working space no greater than the size of the original array. The 1097 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 1098 * execute any parallel tasks. 1099 * 1100 * @param <T> the class of the objects to be sorted 1101 * @param a the array to be sorted 1102 * @param cmp the comparator to determine the order of the array. A 1103 * {@code null} value indicates that the elements' 1104 * {@linkplain Comparable natural ordering} should be used. 1105 * @throws ClassCastException if the array contains elements that are 1106 * not <i>mutually comparable</i> using the specified comparator 1107 * @throws IllegalArgumentException (optional) if the comparator is 1108 * found to violate the {@link java.util.Comparator} contract 1109 * 1110 * @since 1.8 1111 */ 1112 @SuppressWarnings("unchecked") parallelSort(T[] a, Comparator<? super T> cmp)1113 public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) { 1114 if (cmp == null) 1115 cmp = NaturalOrder.INSTANCE; 1116 int n = a.length, p, g; 1117 if (n <= MIN_ARRAY_SORT_GRAN || 1118 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1119 TimSort.sort(a, 0, n, cmp, null, 0, 0); 1120 else 1121 new ArraysParallelSortHelpers.FJObject.Sorter<> 1122 (null, a, 1123 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1124 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1125 MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1126 } 1127 1128 /** 1129 * Sorts the specified range of the specified array of objects according 1130 * to the order induced by the specified comparator. The range to be 1131 * sorted extends from index {@code fromIndex}, inclusive, to index 1132 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1133 * range to be sorted is empty.) All elements in the range must be 1134 * <i>mutually comparable</i> by the specified comparator (that is, 1135 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1136 * for any elements {@code e1} and {@code e2} in the range). 1137 * 1138 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1139 * not be reordered as a result of the sort. 1140 * 1141 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1142 * array into sub-arrays that are themselves sorted and then merged. When 1143 * the sub-array length reaches a minimum granularity, the sub-array is 1144 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1145 * method. If the length of the specified array is less than the minimum 1146 * granularity, then it is sorted using the appropriate {@link 1147 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1148 * space no greater than the size of the specified range of the original 1149 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1150 * used to execute any parallel tasks. 1151 * 1152 * @param <T> the class of the objects to be sorted 1153 * @param a the array to be sorted 1154 * @param fromIndex the index of the first element (inclusive) to be 1155 * sorted 1156 * @param toIndex the index of the last element (exclusive) to be sorted 1157 * @param cmp the comparator to determine the order of the array. A 1158 * {@code null} value indicates that the elements' 1159 * {@linkplain Comparable natural ordering} should be used. 1160 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1161 * (optional) if the natural ordering of the array elements is 1162 * found to violate the {@link Comparable} contract 1163 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1164 * {@code toIndex > a.length} 1165 * @throws ClassCastException if the array contains elements that are 1166 * not <i>mutually comparable</i> (for example, strings and 1167 * integers). 1168 * 1169 * @since 1.8 1170 */ 1171 @SuppressWarnings("unchecked") parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)1172 public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, 1173 Comparator<? super T> cmp) { 1174 rangeCheck(a.length, fromIndex, toIndex); 1175 if (cmp == null) 1176 cmp = NaturalOrder.INSTANCE; 1177 int n = toIndex - fromIndex, p, g; 1178 if (n <= MIN_ARRAY_SORT_GRAN || 1179 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1180 TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0); 1181 else 1182 new ArraysParallelSortHelpers.FJObject.Sorter<> 1183 (null, a, 1184 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1185 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1186 MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1187 } 1188 1189 /* 1190 * Sorting of complex type arrays. 1191 */ 1192 1193 // Android-removed: LegacyMergeSort class (unused on Android). 1194 1195 /** 1196 * Sorts the specified array of objects into ascending order, according 1197 * to the {@linkplain Comparable natural ordering} of its elements. 1198 * All elements in the array must implement the {@link Comparable} 1199 * interface. Furthermore, all elements in the array must be 1200 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 1201 * not throw a {@code ClassCastException} for any elements {@code e1} 1202 * and {@code e2} in the array). 1203 * 1204 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1205 * not be reordered as a result of the sort. 1206 * 1207 * <p>Implementation note: This implementation is a stable, adaptive, 1208 * iterative mergesort that requires far fewer than n lg(n) comparisons 1209 * when the input array is partially sorted, while offering the 1210 * performance of a traditional mergesort when the input array is 1211 * randomly ordered. If the input array is nearly sorted, the 1212 * implementation requires approximately n comparisons. Temporary 1213 * storage requirements vary from a small constant for nearly sorted 1214 * input arrays to n/2 object references for randomly ordered input 1215 * arrays. 1216 * 1217 * <p>The implementation takes equal advantage of ascending and 1218 * descending order in its input array, and can take advantage of 1219 * ascending and descending order in different parts of the same 1220 * input array. It is well-suited to merging two or more sorted arrays: 1221 * simply concatenate the arrays and sort the resulting array. 1222 * 1223 * <p>The implementation was adapted from Tim Peters's list sort for Python 1224 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1225 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1226 * Sorting and Information Theoretic Complexity", in Proceedings of the 1227 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1228 * January 1993. 1229 * 1230 * @param a the array to be sorted 1231 * @throws ClassCastException if the array contains elements that are not 1232 * <i>mutually comparable</i> (for example, strings and integers) 1233 * @throws IllegalArgumentException (optional) if the natural 1234 * ordering of the array elements is found to violate the 1235 * {@link Comparable} contract 1236 */ sort(Object[] a)1237 public static void sort(Object[] a) { 1238 // Android-removed: LegacyMergeSort support. 1239 // if (LegacyMergeSort.userRequested) 1240 // legacyMergeSort(a); 1241 // else 1242 ComparableTimSort.sort(a, 0, a.length, null, 0, 0); 1243 } 1244 1245 // Android-removed: legacyMergeSort() (unused on Android). 1246 1247 /** 1248 * Sorts the specified range of the specified array of objects into 1249 * ascending order, according to the 1250 * {@linkplain Comparable natural ordering} of its 1251 * elements. The range to be sorted extends from index 1252 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 1253 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 1254 * elements in this range must implement the {@link Comparable} 1255 * interface. Furthermore, all elements in this range must be <i>mutually 1256 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1257 * {@code ClassCastException} for any elements {@code e1} and 1258 * {@code e2} in the array). 1259 * 1260 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1261 * not be reordered as a result of the sort. 1262 * 1263 * <p>Implementation note: This implementation is a stable, adaptive, 1264 * iterative mergesort that requires far fewer than n lg(n) comparisons 1265 * when the input array is partially sorted, while offering the 1266 * performance of a traditional mergesort when the input array is 1267 * randomly ordered. If the input array is nearly sorted, the 1268 * implementation requires approximately n comparisons. Temporary 1269 * storage requirements vary from a small constant for nearly sorted 1270 * input arrays to n/2 object references for randomly ordered input 1271 * arrays. 1272 * 1273 * <p>The implementation takes equal advantage of ascending and 1274 * descending order in its input array, and can take advantage of 1275 * ascending and descending order in different parts of the same 1276 * input array. It is well-suited to merging two or more sorted arrays: 1277 * simply concatenate the arrays and sort the resulting array. 1278 * 1279 * <p>The implementation was adapted from Tim Peters's list sort for Python 1280 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1281 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1282 * Sorting and Information Theoretic Complexity", in Proceedings of the 1283 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1284 * January 1993. 1285 * 1286 * @param a the array to be sorted 1287 * @param fromIndex the index of the first element (inclusive) to be 1288 * sorted 1289 * @param toIndex the index of the last element (exclusive) to be sorted 1290 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1291 * (optional) if the natural ordering of the array elements is 1292 * found to violate the {@link Comparable} contract 1293 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1294 * {@code toIndex > a.length} 1295 * @throws ClassCastException if the array contains elements that are 1296 * not <i>mutually comparable</i> (for example, strings and 1297 * integers). 1298 */ sort(Object[] a, int fromIndex, int toIndex)1299 public static void sort(Object[] a, int fromIndex, int toIndex) { 1300 rangeCheck(a.length, fromIndex, toIndex); 1301 // Android-removed: LegacyMergeSort support. 1302 // if (LegacyMergeSort.userRequested) 1303 // legacyMergeSort(a, fromIndex, toIndex); 1304 // else 1305 ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0); 1306 } 1307 1308 // Android-removed: legacyMergeSort() (unused on Android). 1309 1310 /** 1311 * Tuning parameter: list size at or below which insertion sort will be 1312 * used in preference to mergesort. 1313 * To be removed in a future release. 1314 */ 1315 private static final int INSERTIONSORT_THRESHOLD = 7; 1316 1317 /** 1318 * Src is the source array that starts at index 0 1319 * Dest is the (possibly larger) array destination with a possible offset 1320 * low is the index in dest to start sorting 1321 * high is the end index in dest to end sorting 1322 * off is the offset to generate corresponding low, high in src 1323 * To be removed in a future release. 1324 */ 1325 @SuppressWarnings({"unchecked", "rawtypes"}) mergeSort(Object[] src, Object[] dest, int low, int high, int off)1326 private static void mergeSort(Object[] src, 1327 Object[] dest, 1328 int low, 1329 int high, 1330 int off) { 1331 int length = high - low; 1332 1333 // Insertion sort on smallest arrays 1334 if (length < INSERTIONSORT_THRESHOLD) { 1335 for (int i=low; i<high; i++) 1336 for (int j=i; j>low && 1337 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) 1338 swap(dest, j, j-1); 1339 return; 1340 } 1341 1342 // Recursively sort halves of dest into src 1343 int destLow = low; 1344 int destHigh = high; 1345 low += off; 1346 high += off; 1347 int mid = (low + high) >>> 1; 1348 mergeSort(dest, src, low, mid, -off); 1349 mergeSort(dest, src, mid, high, -off); 1350 1351 // If list is already sorted, just copy from src to dest. This is an 1352 // optimization that results in faster sorts for nearly ordered lists. 1353 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { 1354 System.arraycopy(src, low, dest, destLow, length); 1355 return; 1356 } 1357 1358 // Merge sorted halves (now in src) into dest 1359 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 1360 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) 1361 dest[i] = src[p++]; 1362 else 1363 dest[i] = src[q++]; 1364 } 1365 } 1366 1367 /** 1368 * Swaps x[a] with x[b]. 1369 */ swap(Object[] x, int a, int b)1370 private static void swap(Object[] x, int a, int b) { 1371 Object t = x[a]; 1372 x[a] = x[b]; 1373 x[b] = t; 1374 } 1375 1376 /** 1377 * Sorts the specified array of objects according to the order induced by 1378 * the specified comparator. All elements in the array must be 1379 * <i>mutually comparable</i> by the specified comparator (that is, 1380 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1381 * for any elements {@code e1} and {@code e2} in the array). 1382 * 1383 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1384 * not be reordered as a result of the sort. 1385 * 1386 * <p>Implementation note: This implementation is a stable, adaptive, 1387 * iterative mergesort that requires far fewer than n lg(n) comparisons 1388 * when the input array is partially sorted, while offering the 1389 * performance of a traditional mergesort when the input array is 1390 * randomly ordered. If the input array is nearly sorted, the 1391 * implementation requires approximately n comparisons. Temporary 1392 * storage requirements vary from a small constant for nearly sorted 1393 * input arrays to n/2 object references for randomly ordered input 1394 * arrays. 1395 * 1396 * <p>The implementation takes equal advantage of ascending and 1397 * descending order in its input array, and can take advantage of 1398 * ascending and descending order in different parts of the same 1399 * input array. It is well-suited to merging two or more sorted arrays: 1400 * simply concatenate the arrays and sort the resulting array. 1401 * 1402 * <p>The implementation was adapted from Tim Peters's list sort for Python 1403 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1404 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1405 * Sorting and Information Theoretic Complexity", in Proceedings of the 1406 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1407 * January 1993. 1408 * 1409 * @param <T> the class of the objects to be sorted 1410 * @param a the array to be sorted 1411 * @param c the comparator to determine the order of the array. A 1412 * {@code null} value indicates that the elements' 1413 * {@linkplain Comparable natural ordering} should be used. 1414 * @throws ClassCastException if the array contains elements that are 1415 * not <i>mutually comparable</i> using the specified comparator 1416 * @throws IllegalArgumentException (optional) if the comparator is 1417 * found to violate the {@link Comparator} contract 1418 */ sort(T[] a, Comparator<? super T> c)1419 public static <T> void sort(T[] a, Comparator<? super T> c) { 1420 if (c == null) { 1421 sort(a); 1422 } else { 1423 // Android-removed: LegacyMergeSort support. 1424 // if (LegacyMergeSort.userRequested) 1425 // legacyMergeSort(a, c); 1426 // else 1427 TimSort.sort(a, 0, a.length, c, null, 0, 0); 1428 } 1429 } 1430 1431 // Android-removed: legacyMergeSort() (unused on Android). 1432 1433 /** 1434 * Sorts the specified range of the specified array of objects according 1435 * to the order induced by the specified comparator. The range to be 1436 * sorted extends from index {@code fromIndex}, inclusive, to index 1437 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1438 * range to be sorted is empty.) All elements in the range must be 1439 * <i>mutually comparable</i> by the specified comparator (that is, 1440 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1441 * for any elements {@code e1} and {@code e2} in the range). 1442 * 1443 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1444 * not be reordered as a result of the sort. 1445 * 1446 * <p>Implementation note: This implementation is a stable, adaptive, 1447 * iterative mergesort that requires far fewer than n lg(n) comparisons 1448 * when the input array is partially sorted, while offering the 1449 * performance of a traditional mergesort when the input array is 1450 * randomly ordered. If the input array is nearly sorted, the 1451 * implementation requires approximately n comparisons. Temporary 1452 * storage requirements vary from a small constant for nearly sorted 1453 * input arrays to n/2 object references for randomly ordered input 1454 * arrays. 1455 * 1456 * <p>The implementation takes equal advantage of ascending and 1457 * descending order in its input array, and can take advantage of 1458 * ascending and descending order in different parts of the same 1459 * input array. It is well-suited to merging two or more sorted arrays: 1460 * simply concatenate the arrays and sort the resulting array. 1461 * 1462 * <p>The implementation was adapted from Tim Peters's list sort for Python 1463 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1464 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1465 * Sorting and Information Theoretic Complexity", in Proceedings of the 1466 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1467 * January 1993. 1468 * 1469 * @param <T> the class of the objects to be sorted 1470 * @param a the array to be sorted 1471 * @param fromIndex the index of the first element (inclusive) to be 1472 * sorted 1473 * @param toIndex the index of the last element (exclusive) to be sorted 1474 * @param c the comparator to determine the order of the array. A 1475 * {@code null} value indicates that the elements' 1476 * {@linkplain Comparable natural ordering} should be used. 1477 * @throws ClassCastException if the array contains elements that are not 1478 * <i>mutually comparable</i> using the specified comparator. 1479 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1480 * (optional) if the comparator is found to violate the 1481 * {@link Comparator} contract 1482 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1483 * {@code toIndex > a.length} 1484 */ sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)1485 public static <T> void sort(T[] a, int fromIndex, int toIndex, 1486 Comparator<? super T> c) { 1487 if (c == null) { 1488 sort(a, fromIndex, toIndex); 1489 } else { 1490 rangeCheck(a.length, fromIndex, toIndex); 1491 // Android-removed: LegacyMergeSort support. 1492 // if (LegacyMergeSort.userRequested) 1493 // legacyMergeSort(a, fromIndex, toIndex, c); 1494 // else 1495 TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0); 1496 } 1497 } 1498 1499 // Android-removed: legacyMergeSort() (unused on Android). 1500 // Android-removed: mergeSort() (unused on Android). 1501 1502 // Parallel prefix 1503 1504 /** 1505 * Cumulates, in parallel, each element of the given array in place, 1506 * using the supplied function. For example if the array initially 1507 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1508 * then upon return the array holds {@code [2, 3, 3, 6]}. 1509 * Parallel prefix computation is usually more efficient than 1510 * sequential loops for large arrays. 1511 * 1512 * @param <T> the class of the objects in the array 1513 * @param array the array, which is modified in-place by this method 1514 * @param op a side-effect-free, associative function to perform the 1515 * cumulation 1516 * @throws NullPointerException if the specified array or function is null 1517 * @since 1.8 1518 */ parallelPrefix(T[] array, BinaryOperator<T> op)1519 public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) { 1520 Objects.requireNonNull(op); 1521 if (array.length > 0) 1522 new ArrayPrefixHelpers.CumulateTask<> 1523 (null, op, array, 0, array.length).invoke(); 1524 } 1525 1526 /** 1527 * Performs {@link #parallelPrefix(Object[], BinaryOperator)} 1528 * for the given subrange of the array. 1529 * 1530 * @param <T> the class of the objects in the array 1531 * @param array the array 1532 * @param fromIndex the index of the first element, inclusive 1533 * @param toIndex the index of the last element, exclusive 1534 * @param op a side-effect-free, associative function to perform the 1535 * cumulation 1536 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1537 * @throws ArrayIndexOutOfBoundsException 1538 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1539 * @throws NullPointerException if the specified array or function is null 1540 * @since 1.8 1541 */ parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)1542 public static <T> void parallelPrefix(T[] array, int fromIndex, 1543 int toIndex, BinaryOperator<T> op) { 1544 Objects.requireNonNull(op); 1545 rangeCheck(array.length, fromIndex, toIndex); 1546 if (fromIndex < toIndex) 1547 new ArrayPrefixHelpers.CumulateTask<> 1548 (null, op, array, fromIndex, toIndex).invoke(); 1549 } 1550 1551 /** 1552 * Cumulates, in parallel, each element of the given array in place, 1553 * using the supplied function. For example if the array initially 1554 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1555 * then upon return the array holds {@code [2, 3, 3, 6]}. 1556 * Parallel prefix computation is usually more efficient than 1557 * sequential loops for large arrays. 1558 * 1559 * @param array the array, which is modified in-place by this method 1560 * @param op a side-effect-free, associative function to perform the 1561 * cumulation 1562 * @throws NullPointerException if the specified array or function is null 1563 * @since 1.8 1564 */ parallelPrefix(long[] array, LongBinaryOperator op)1565 public static void parallelPrefix(long[] array, LongBinaryOperator op) { 1566 Objects.requireNonNull(op); 1567 if (array.length > 0) 1568 new ArrayPrefixHelpers.LongCumulateTask 1569 (null, op, array, 0, array.length).invoke(); 1570 } 1571 1572 /** 1573 * Performs {@link #parallelPrefix(long[], LongBinaryOperator)} 1574 * for the given subrange of the array. 1575 * 1576 * @param array the array 1577 * @param fromIndex the index of the first element, inclusive 1578 * @param toIndex the index of the last element, exclusive 1579 * @param op a side-effect-free, associative function to perform the 1580 * cumulation 1581 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1582 * @throws ArrayIndexOutOfBoundsException 1583 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1584 * @throws NullPointerException if the specified array or function is null 1585 * @since 1.8 1586 */ parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)1587 public static void parallelPrefix(long[] array, int fromIndex, 1588 int toIndex, LongBinaryOperator op) { 1589 Objects.requireNonNull(op); 1590 rangeCheck(array.length, fromIndex, toIndex); 1591 if (fromIndex < toIndex) 1592 new ArrayPrefixHelpers.LongCumulateTask 1593 (null, op, array, fromIndex, toIndex).invoke(); 1594 } 1595 1596 /** 1597 * Cumulates, in parallel, each element of the given array in place, 1598 * using the supplied function. For example if the array initially 1599 * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition, 1600 * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}. 1601 * Parallel prefix computation is usually more efficient than 1602 * sequential loops for large arrays. 1603 * 1604 * <p> Because floating-point operations may not be strictly associative, 1605 * the returned result may not be identical to the value that would be 1606 * obtained if the operation was performed sequentially. 1607 * 1608 * @param array the array, which is modified in-place by this method 1609 * @param op a side-effect-free function to perform the cumulation 1610 * @throws NullPointerException if the specified array or function is null 1611 * @since 1.8 1612 */ parallelPrefix(double[] array, DoubleBinaryOperator op)1613 public static void parallelPrefix(double[] array, DoubleBinaryOperator op) { 1614 Objects.requireNonNull(op); 1615 if (array.length > 0) 1616 new ArrayPrefixHelpers.DoubleCumulateTask 1617 (null, op, array, 0, array.length).invoke(); 1618 } 1619 1620 /** 1621 * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)} 1622 * for the given subrange of the array. 1623 * 1624 * @param array the array 1625 * @param fromIndex the index of the first element, inclusive 1626 * @param toIndex the index of the last element, exclusive 1627 * @param op a side-effect-free, associative function to perform the 1628 * cumulation 1629 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1630 * @throws ArrayIndexOutOfBoundsException 1631 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1632 * @throws NullPointerException if the specified array or function is null 1633 * @since 1.8 1634 */ parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)1635 public static void parallelPrefix(double[] array, int fromIndex, 1636 int toIndex, DoubleBinaryOperator op) { 1637 Objects.requireNonNull(op); 1638 rangeCheck(array.length, fromIndex, toIndex); 1639 if (fromIndex < toIndex) 1640 new ArrayPrefixHelpers.DoubleCumulateTask 1641 (null, op, array, fromIndex, toIndex).invoke(); 1642 } 1643 1644 /** 1645 * Cumulates, in parallel, each element of the given array in place, 1646 * using the supplied function. For example if the array initially 1647 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1648 * then upon return the array holds {@code [2, 3, 3, 6]}. 1649 * Parallel prefix computation is usually more efficient than 1650 * sequential loops for large arrays. 1651 * 1652 * @param array the array, which is modified in-place by this method 1653 * @param op a side-effect-free, associative function to perform the 1654 * cumulation 1655 * @throws NullPointerException if the specified array or function is null 1656 * @since 1.8 1657 */ parallelPrefix(int[] array, IntBinaryOperator op)1658 public static void parallelPrefix(int[] array, IntBinaryOperator op) { 1659 Objects.requireNonNull(op); 1660 if (array.length > 0) 1661 new ArrayPrefixHelpers.IntCumulateTask 1662 (null, op, array, 0, array.length).invoke(); 1663 } 1664 1665 /** 1666 * Performs {@link #parallelPrefix(int[], IntBinaryOperator)} 1667 * for the given subrange of the array. 1668 * 1669 * @param array the array 1670 * @param fromIndex the index of the first element, inclusive 1671 * @param toIndex the index of the last element, exclusive 1672 * @param op a side-effect-free, associative function to perform the 1673 * cumulation 1674 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1675 * @throws ArrayIndexOutOfBoundsException 1676 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1677 * @throws NullPointerException if the specified array or function is null 1678 * @since 1.8 1679 */ parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)1680 public static void parallelPrefix(int[] array, int fromIndex, 1681 int toIndex, IntBinaryOperator op) { 1682 Objects.requireNonNull(op); 1683 rangeCheck(array.length, fromIndex, toIndex); 1684 if (fromIndex < toIndex) 1685 new ArrayPrefixHelpers.IntCumulateTask 1686 (null, op, array, fromIndex, toIndex).invoke(); 1687 } 1688 1689 // Searching 1690 1691 /** 1692 * Searches the specified array of longs for the specified value using the 1693 * binary search algorithm. The array must be sorted (as 1694 * by the {@link #sort(long[])} method) prior to making this call. If it 1695 * is not sorted, the results are undefined. If the array contains 1696 * multiple elements with the specified value, there is no guarantee which 1697 * one will be found. 1698 * 1699 * @param a the array to be searched 1700 * @param key the value to be searched for 1701 * @return index of the search key, if it is contained in the array; 1702 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1703 * <i>insertion point</i> is defined as the point at which the 1704 * key would be inserted into the array: the index of the first 1705 * element greater than the key, or {@code a.length} if all 1706 * elements in the array are less than the specified key. Note 1707 * that this guarantees that the return value will be >= 0 if 1708 * and only if the key is found. 1709 */ binarySearch(long[] a, long key)1710 public static int binarySearch(long[] a, long key) { 1711 return binarySearch0(a, 0, a.length, key); 1712 } 1713 1714 /** 1715 * Searches a range of 1716 * the specified array of longs for the specified value using the 1717 * binary search algorithm. 1718 * The range must be sorted (as 1719 * by the {@link #sort(long[], int, int)} method) 1720 * prior to making this call. If it 1721 * is not sorted, the results are undefined. If the range contains 1722 * multiple elements with the specified value, there is no guarantee which 1723 * one will be found. 1724 * 1725 * @param a the array to be searched 1726 * @param fromIndex the index of the first element (inclusive) to be 1727 * searched 1728 * @param toIndex the index of the last element (exclusive) to be searched 1729 * @param key the value to be searched for 1730 * @return index of the search key, if it is contained in the array 1731 * within the specified range; 1732 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1733 * <i>insertion point</i> is defined as the point at which the 1734 * key would be inserted into the array: the index of the first 1735 * element in the range greater than the key, 1736 * or {@code toIndex} if all 1737 * elements in the range are less than the specified key. Note 1738 * that this guarantees that the return value will be >= 0 if 1739 * and only if the key is found. 1740 * @throws IllegalArgumentException 1741 * if {@code fromIndex > toIndex} 1742 * @throws ArrayIndexOutOfBoundsException 1743 * if {@code fromIndex < 0 or toIndex > a.length} 1744 * @since 1.6 1745 */ binarySearch(long[] a, int fromIndex, int toIndex, long key)1746 public static int binarySearch(long[] a, int fromIndex, int toIndex, 1747 long key) { 1748 rangeCheck(a.length, fromIndex, toIndex); 1749 return binarySearch0(a, fromIndex, toIndex, key); 1750 } 1751 1752 // Like public version, but without range checks. binarySearch0(long[] a, int fromIndex, int toIndex, long key)1753 private static int binarySearch0(long[] a, int fromIndex, int toIndex, 1754 long key) { 1755 int low = fromIndex; 1756 int high = toIndex - 1; 1757 1758 while (low <= high) { 1759 int mid = (low + high) >>> 1; 1760 long midVal = a[mid]; 1761 1762 if (midVal < key) 1763 low = mid + 1; 1764 else if (midVal > key) 1765 high = mid - 1; 1766 else 1767 return mid; // key found 1768 } 1769 return -(low + 1); // key not found. 1770 } 1771 1772 /** 1773 * Searches the specified array of ints for the specified value using the 1774 * binary search algorithm. The array must be sorted (as 1775 * by the {@link #sort(int[])} method) prior to making this call. If it 1776 * is not sorted, the results are undefined. If the array contains 1777 * multiple elements with the specified value, there is no guarantee which 1778 * one will be found. 1779 * 1780 * @param a the array to be searched 1781 * @param key the value to be searched for 1782 * @return index of the search key, if it is contained in the array; 1783 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1784 * <i>insertion point</i> is defined as the point at which the 1785 * key would be inserted into the array: the index of the first 1786 * element greater than the key, or {@code a.length} if all 1787 * elements in the array are less than the specified key. Note 1788 * that this guarantees that the return value will be >= 0 if 1789 * and only if the key is found. 1790 */ binarySearch(int[] a, int key)1791 public static int binarySearch(int[] a, int key) { 1792 return binarySearch0(a, 0, a.length, key); 1793 } 1794 1795 /** 1796 * Searches a range of 1797 * the specified array of ints for the specified value using the 1798 * binary search algorithm. 1799 * The range must be sorted (as 1800 * by the {@link #sort(int[], int, int)} method) 1801 * prior to making this call. If it 1802 * is not sorted, the results are undefined. If the range contains 1803 * multiple elements with the specified value, there is no guarantee which 1804 * one will be found. 1805 * 1806 * @param a the array to be searched 1807 * @param fromIndex the index of the first element (inclusive) to be 1808 * searched 1809 * @param toIndex the index of the last element (exclusive) to be searched 1810 * @param key the value to be searched for 1811 * @return index of the search key, if it is contained in the array 1812 * within the specified range; 1813 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1814 * <i>insertion point</i> is defined as the point at which the 1815 * key would be inserted into the array: the index of the first 1816 * element in the range greater than the key, 1817 * or {@code toIndex} if all 1818 * elements in the range are less than the specified key. Note 1819 * that this guarantees that the return value will be >= 0 if 1820 * and only if the key is found. 1821 * @throws IllegalArgumentException 1822 * if {@code fromIndex > toIndex} 1823 * @throws ArrayIndexOutOfBoundsException 1824 * if {@code fromIndex < 0 or toIndex > a.length} 1825 * @since 1.6 1826 */ binarySearch(int[] a, int fromIndex, int toIndex, int key)1827 public static int binarySearch(int[] a, int fromIndex, int toIndex, 1828 int key) { 1829 rangeCheck(a.length, fromIndex, toIndex); 1830 return binarySearch0(a, fromIndex, toIndex, key); 1831 } 1832 1833 // Like public version, but without range checks. binarySearch0(int[] a, int fromIndex, int toIndex, int key)1834 private static int binarySearch0(int[] a, int fromIndex, int toIndex, 1835 int key) { 1836 int low = fromIndex; 1837 int high = toIndex - 1; 1838 1839 while (low <= high) { 1840 int mid = (low + high) >>> 1; 1841 int midVal = a[mid]; 1842 1843 if (midVal < key) 1844 low = mid + 1; 1845 else if (midVal > key) 1846 high = mid - 1; 1847 else 1848 return mid; // key found 1849 } 1850 return -(low + 1); // key not found. 1851 } 1852 1853 /** 1854 * Searches the specified array of shorts for the specified value using 1855 * the binary search algorithm. The array must be sorted 1856 * (as by the {@link #sort(short[])} method) prior to making this call. If 1857 * it is not sorted, the results are undefined. If the array contains 1858 * multiple elements with the specified value, there is no guarantee which 1859 * one will be found. 1860 * 1861 * @param a the array to be searched 1862 * @param key the value to be searched for 1863 * @return index of the search key, if it is contained in the array; 1864 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1865 * <i>insertion point</i> is defined as the point at which the 1866 * key would be inserted into the array: the index of the first 1867 * element greater than the key, or {@code a.length} if all 1868 * elements in the array are less than the specified key. Note 1869 * that this guarantees that the return value will be >= 0 if 1870 * and only if the key is found. 1871 */ binarySearch(short[] a, short key)1872 public static int binarySearch(short[] a, short key) { 1873 return binarySearch0(a, 0, a.length, key); 1874 } 1875 1876 /** 1877 * Searches a range of 1878 * the specified array of shorts for the specified value using 1879 * the binary search algorithm. 1880 * The range must be sorted 1881 * (as by the {@link #sort(short[], int, int)} method) 1882 * prior to making this call. If 1883 * it is not sorted, the results are undefined. If the range contains 1884 * multiple elements with the specified value, there is no guarantee which 1885 * one will be found. 1886 * 1887 * @param a the array to be searched 1888 * @param fromIndex the index of the first element (inclusive) to be 1889 * searched 1890 * @param toIndex the index of the last element (exclusive) to be searched 1891 * @param key the value to be searched for 1892 * @return index of the search key, if it is contained in the array 1893 * within the specified range; 1894 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1895 * <i>insertion point</i> is defined as the point at which the 1896 * key would be inserted into the array: the index of the first 1897 * element in the range greater than the key, 1898 * or {@code toIndex} if all 1899 * elements in the range are less than the specified key. Note 1900 * that this guarantees that the return value will be >= 0 if 1901 * and only if the key is found. 1902 * @throws IllegalArgumentException 1903 * if {@code fromIndex > toIndex} 1904 * @throws ArrayIndexOutOfBoundsException 1905 * if {@code fromIndex < 0 or toIndex > a.length} 1906 * @since 1.6 1907 */ binarySearch(short[] a, int fromIndex, int toIndex, short key)1908 public static int binarySearch(short[] a, int fromIndex, int toIndex, 1909 short key) { 1910 rangeCheck(a.length, fromIndex, toIndex); 1911 return binarySearch0(a, fromIndex, toIndex, key); 1912 } 1913 1914 // Like public version, but without range checks. binarySearch0(short[] a, int fromIndex, int toIndex, short key)1915 private static int binarySearch0(short[] a, int fromIndex, int toIndex, 1916 short key) { 1917 int low = fromIndex; 1918 int high = toIndex - 1; 1919 1920 while (low <= high) { 1921 int mid = (low + high) >>> 1; 1922 short midVal = a[mid]; 1923 1924 if (midVal < key) 1925 low = mid + 1; 1926 else if (midVal > key) 1927 high = mid - 1; 1928 else 1929 return mid; // key found 1930 } 1931 return -(low + 1); // key not found. 1932 } 1933 1934 /** 1935 * Searches the specified array of chars for the specified value using the 1936 * binary search algorithm. The array must be sorted (as 1937 * by the {@link #sort(char[])} method) prior to making this call. If it 1938 * is not sorted, the results are undefined. If the array contains 1939 * multiple elements with the specified value, there is no guarantee which 1940 * one will be found. 1941 * 1942 * @param a the array to be searched 1943 * @param key the value to be searched for 1944 * @return index of the search key, if it is contained in the array; 1945 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1946 * <i>insertion point</i> is defined as the point at which the 1947 * key would be inserted into the array: the index of the first 1948 * element greater than the key, or {@code a.length} if all 1949 * elements in the array are less than the specified key. Note 1950 * that this guarantees that the return value will be >= 0 if 1951 * and only if the key is found. 1952 */ binarySearch(char[] a, char key)1953 public static int binarySearch(char[] a, char key) { 1954 return binarySearch0(a, 0, a.length, key); 1955 } 1956 1957 /** 1958 * Searches a range of 1959 * the specified array of chars for the specified value using the 1960 * binary search algorithm. 1961 * The range must be sorted (as 1962 * by the {@link #sort(char[], int, int)} method) 1963 * prior to making this call. If it 1964 * is not sorted, the results are undefined. If the range contains 1965 * multiple elements with the specified value, there is no guarantee which 1966 * one will be found. 1967 * 1968 * @param a the array to be searched 1969 * @param fromIndex the index of the first element (inclusive) to be 1970 * searched 1971 * @param toIndex the index of the last element (exclusive) to be searched 1972 * @param key the value to be searched for 1973 * @return index of the search key, if it is contained in the array 1974 * within the specified range; 1975 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 1976 * <i>insertion point</i> is defined as the point at which the 1977 * key would be inserted into the array: the index of the first 1978 * element in the range greater than the key, 1979 * or {@code toIndex} if all 1980 * elements in the range are less than the specified key. Note 1981 * that this guarantees that the return value will be >= 0 if 1982 * and only if the key is found. 1983 * @throws IllegalArgumentException 1984 * if {@code fromIndex > toIndex} 1985 * @throws ArrayIndexOutOfBoundsException 1986 * if {@code fromIndex < 0 or toIndex > a.length} 1987 * @since 1.6 1988 */ binarySearch(char[] a, int fromIndex, int toIndex, char key)1989 public static int binarySearch(char[] a, int fromIndex, int toIndex, 1990 char key) { 1991 rangeCheck(a.length, fromIndex, toIndex); 1992 return binarySearch0(a, fromIndex, toIndex, key); 1993 } 1994 1995 // Like public version, but without range checks. binarySearch0(char[] a, int fromIndex, int toIndex, char key)1996 private static int binarySearch0(char[] a, int fromIndex, int toIndex, 1997 char key) { 1998 int low = fromIndex; 1999 int high = toIndex - 1; 2000 2001 while (low <= high) { 2002 int mid = (low + high) >>> 1; 2003 char midVal = a[mid]; 2004 2005 if (midVal < key) 2006 low = mid + 1; 2007 else if (midVal > key) 2008 high = mid - 1; 2009 else 2010 return mid; // key found 2011 } 2012 return -(low + 1); // key not found. 2013 } 2014 2015 /** 2016 * Searches the specified array of bytes for the specified value using the 2017 * binary search algorithm. The array must be sorted (as 2018 * by the {@link #sort(byte[])} method) prior to making this call. If it 2019 * is not sorted, the results are undefined. If the array contains 2020 * multiple elements with the specified value, there is no guarantee which 2021 * one will be found. 2022 * 2023 * @param a the array to be searched 2024 * @param key the value to be searched for 2025 * @return index of the search key, if it is contained in the array; 2026 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2027 * <i>insertion point</i> is defined as the point at which the 2028 * key would be inserted into the array: the index of the first 2029 * element greater than the key, or {@code a.length} if all 2030 * elements in the array are less than the specified key. Note 2031 * that this guarantees that the return value will be >= 0 if 2032 * and only if the key is found. 2033 */ binarySearch(byte[] a, byte key)2034 public static int binarySearch(byte[] a, byte key) { 2035 return binarySearch0(a, 0, a.length, key); 2036 } 2037 2038 /** 2039 * Searches a range of 2040 * the specified array of bytes for the specified value using the 2041 * binary search algorithm. 2042 * The range must be sorted (as 2043 * by the {@link #sort(byte[], int, int)} method) 2044 * prior to making this call. If it 2045 * is not sorted, the results are undefined. If the range contains 2046 * multiple elements with the specified value, there is no guarantee which 2047 * one will be found. 2048 * 2049 * @param a the array to be searched 2050 * @param fromIndex the index of the first element (inclusive) to be 2051 * searched 2052 * @param toIndex the index of the last element (exclusive) to be searched 2053 * @param key the value to be searched for 2054 * @return index of the search key, if it is contained in the array 2055 * within the specified range; 2056 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2057 * <i>insertion point</i> is defined as the point at which the 2058 * key would be inserted into the array: the index of the first 2059 * element in the range greater than the key, 2060 * or {@code toIndex} if all 2061 * elements in the range are less than the specified key. Note 2062 * that this guarantees that the return value will be >= 0 if 2063 * and only if the key is found. 2064 * @throws IllegalArgumentException 2065 * if {@code fromIndex > toIndex} 2066 * @throws ArrayIndexOutOfBoundsException 2067 * if {@code fromIndex < 0 or toIndex > a.length} 2068 * @since 1.6 2069 */ binarySearch(byte[] a, int fromIndex, int toIndex, byte key)2070 public static int binarySearch(byte[] a, int fromIndex, int toIndex, 2071 byte key) { 2072 rangeCheck(a.length, fromIndex, toIndex); 2073 return binarySearch0(a, fromIndex, toIndex, key); 2074 } 2075 2076 // Like public version, but without range checks. binarySearch0(byte[] a, int fromIndex, int toIndex, byte key)2077 private static int binarySearch0(byte[] a, int fromIndex, int toIndex, 2078 byte key) { 2079 int low = fromIndex; 2080 int high = toIndex - 1; 2081 2082 while (low <= high) { 2083 int mid = (low + high) >>> 1; 2084 byte midVal = a[mid]; 2085 2086 if (midVal < key) 2087 low = mid + 1; 2088 else if (midVal > key) 2089 high = mid - 1; 2090 else 2091 return mid; // key found 2092 } 2093 return -(low + 1); // key not found. 2094 } 2095 2096 /** 2097 * Searches the specified array of doubles for the specified value using 2098 * the binary search algorithm. The array must be sorted 2099 * (as by the {@link #sort(double[])} method) prior to making this call. 2100 * If it is not sorted, the results are undefined. If the array contains 2101 * multiple elements with the specified value, there is no guarantee which 2102 * one will be found. This method considers all NaN values to be 2103 * equivalent and equal. 2104 * 2105 * @param a the array to be searched 2106 * @param key the value to be searched for 2107 * @return index of the search key, if it is contained in the array; 2108 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2109 * <i>insertion point</i> is defined as the point at which the 2110 * key would be inserted into the array: the index of the first 2111 * element greater than the key, or {@code a.length} if all 2112 * elements in the array are less than the specified key. Note 2113 * that this guarantees that the return value will be >= 0 if 2114 * and only if the key is found. 2115 */ binarySearch(double[] a, double key)2116 public static int binarySearch(double[] a, double key) { 2117 return binarySearch0(a, 0, a.length, key); 2118 } 2119 2120 /** 2121 * Searches a range of 2122 * the specified array of doubles for the specified value using 2123 * the binary search algorithm. 2124 * The range must be sorted 2125 * (as by the {@link #sort(double[], int, int)} method) 2126 * prior to making this call. 2127 * If it is not sorted, the results are undefined. If the range contains 2128 * multiple elements with the specified value, there is no guarantee which 2129 * one will be found. This method considers all NaN values to be 2130 * equivalent and equal. 2131 * 2132 * @param a the array to be searched 2133 * @param fromIndex the index of the first element (inclusive) to be 2134 * searched 2135 * @param toIndex the index of the last element (exclusive) to be searched 2136 * @param key the value to be searched for 2137 * @return index of the search key, if it is contained in the array 2138 * within the specified range; 2139 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2140 * <i>insertion point</i> is defined as the point at which the 2141 * key would be inserted into the array: the index of the first 2142 * element in the range greater than the key, 2143 * or {@code toIndex} if all 2144 * elements in the range are less than the specified key. Note 2145 * that this guarantees that the return value will be >= 0 if 2146 * and only if the key is found. 2147 * @throws IllegalArgumentException 2148 * if {@code fromIndex > toIndex} 2149 * @throws ArrayIndexOutOfBoundsException 2150 * if {@code fromIndex < 0 or toIndex > a.length} 2151 * @since 1.6 2152 */ binarySearch(double[] a, int fromIndex, int toIndex, double key)2153 public static int binarySearch(double[] a, int fromIndex, int toIndex, 2154 double key) { 2155 rangeCheck(a.length, fromIndex, toIndex); 2156 return binarySearch0(a, fromIndex, toIndex, key); 2157 } 2158 2159 // Like public version, but without range checks. binarySearch0(double[] a, int fromIndex, int toIndex, double key)2160 private static int binarySearch0(double[] a, int fromIndex, int toIndex, 2161 double key) { 2162 int low = fromIndex; 2163 int high = toIndex - 1; 2164 2165 while (low <= high) { 2166 int mid = (low + high) >>> 1; 2167 double midVal = a[mid]; 2168 2169 if (midVal < key) 2170 low = mid + 1; // Neither val is NaN, thisVal is smaller 2171 else if (midVal > key) 2172 high = mid - 1; // Neither val is NaN, thisVal is larger 2173 else { 2174 long midBits = Double.doubleToLongBits(midVal); 2175 long keyBits = Double.doubleToLongBits(key); 2176 if (midBits == keyBits) // Values are equal 2177 return mid; // Key found 2178 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 2179 low = mid + 1; 2180 else // (0.0, -0.0) or (NaN, !NaN) 2181 high = mid - 1; 2182 } 2183 } 2184 return -(low + 1); // key not found. 2185 } 2186 2187 /** 2188 * Searches the specified array of floats for the specified value using 2189 * the binary search algorithm. The array must be sorted 2190 * (as by the {@link #sort(float[])} method) prior to making this call. If 2191 * it is not sorted, the results are undefined. If the array contains 2192 * multiple elements with the specified value, there is no guarantee which 2193 * one will be found. This method considers all NaN values to be 2194 * equivalent and equal. 2195 * 2196 * @param a the array to be searched 2197 * @param key the value to be searched for 2198 * @return index of the search key, if it is contained in the array; 2199 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2200 * <i>insertion point</i> is defined as the point at which the 2201 * key would be inserted into the array: the index of the first 2202 * element greater than the key, or {@code a.length} if all 2203 * elements in the array are less than the specified key. Note 2204 * that this guarantees that the return value will be >= 0 if 2205 * and only if the key is found. 2206 */ binarySearch(float[] a, float key)2207 public static int binarySearch(float[] a, float key) { 2208 return binarySearch0(a, 0, a.length, key); 2209 } 2210 2211 /** 2212 * Searches a range of 2213 * the specified array of floats for the specified value using 2214 * the binary search algorithm. 2215 * The range must be sorted 2216 * (as by the {@link #sort(float[], int, int)} method) 2217 * prior to making this call. If 2218 * it is not sorted, the results are undefined. If the range contains 2219 * multiple elements with the specified value, there is no guarantee which 2220 * one will be found. This method considers all NaN values to be 2221 * equivalent and equal. 2222 * 2223 * @param a the array to be searched 2224 * @param fromIndex the index of the first element (inclusive) to be 2225 * searched 2226 * @param toIndex the index of the last element (exclusive) to be searched 2227 * @param key the value to be searched for 2228 * @return index of the search key, if it is contained in the array 2229 * within the specified range; 2230 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2231 * <i>insertion point</i> is defined as the point at which the 2232 * key would be inserted into the array: the index of the first 2233 * element in the range greater than the key, 2234 * or {@code toIndex} if all 2235 * elements in the range are less than the specified key. Note 2236 * that this guarantees that the return value will be >= 0 if 2237 * and only if the key is found. 2238 * @throws IllegalArgumentException 2239 * if {@code fromIndex > toIndex} 2240 * @throws ArrayIndexOutOfBoundsException 2241 * if {@code fromIndex < 0 or toIndex > a.length} 2242 * @since 1.6 2243 */ binarySearch(float[] a, int fromIndex, int toIndex, float key)2244 public static int binarySearch(float[] a, int fromIndex, int toIndex, 2245 float key) { 2246 rangeCheck(a.length, fromIndex, toIndex); 2247 return binarySearch0(a, fromIndex, toIndex, key); 2248 } 2249 2250 // Like public version, but without range checks. binarySearch0(float[] a, int fromIndex, int toIndex, float key)2251 private static int binarySearch0(float[] a, int fromIndex, int toIndex, 2252 float key) { 2253 int low = fromIndex; 2254 int high = toIndex - 1; 2255 2256 while (low <= high) { 2257 int mid = (low + high) >>> 1; 2258 float midVal = a[mid]; 2259 2260 if (midVal < key) 2261 low = mid + 1; // Neither val is NaN, thisVal is smaller 2262 else if (midVal > key) 2263 high = mid - 1; // Neither val is NaN, thisVal is larger 2264 else { 2265 int midBits = Float.floatToIntBits(midVal); 2266 int keyBits = Float.floatToIntBits(key); 2267 if (midBits == keyBits) // Values are equal 2268 return mid; // Key found 2269 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 2270 low = mid + 1; 2271 else // (0.0, -0.0) or (NaN, !NaN) 2272 high = mid - 1; 2273 } 2274 } 2275 return -(low + 1); // key not found. 2276 } 2277 2278 /** 2279 * Searches the specified array for the specified object using the binary 2280 * search algorithm. The array must be sorted into ascending order 2281 * according to the 2282 * {@linkplain Comparable natural ordering} 2283 * of its elements (as by the 2284 * {@link #sort(Object[])} method) prior to making this call. 2285 * If it is not sorted, the results are undefined. 2286 * (If the array contains elements that are not mutually comparable (for 2287 * example, strings and integers), it <i>cannot</i> be sorted according 2288 * to the natural ordering of its elements, hence results are undefined.) 2289 * If the array contains multiple 2290 * elements equal to the specified object, there is no guarantee which 2291 * one will be found. 2292 * 2293 * @param a the array to be searched 2294 * @param key the value to be searched for 2295 * @return index of the search key, if it is contained in the array; 2296 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2297 * <i>insertion point</i> is defined as the point at which the 2298 * key would be inserted into the array: the index of the first 2299 * element greater than the key, or {@code a.length} if all 2300 * elements in the array are less than the specified key. Note 2301 * that this guarantees that the return value will be >= 0 if 2302 * and only if the key is found. 2303 * @throws ClassCastException if the search key is not comparable to the 2304 * elements of the array. 2305 */ binarySearch(Object[] a, Object key)2306 public static int binarySearch(Object[] a, Object key) { 2307 return binarySearch0(a, 0, a.length, key); 2308 } 2309 2310 /** 2311 * Searches a range of 2312 * the specified array for the specified object using the binary 2313 * search algorithm. 2314 * The range must be sorted into ascending order 2315 * according to the 2316 * {@linkplain Comparable natural ordering} 2317 * of its elements (as by the 2318 * {@link #sort(Object[], int, int)} method) prior to making this 2319 * call. If it is not sorted, the results are undefined. 2320 * (If the range contains elements that are not mutually comparable (for 2321 * example, strings and integers), it <i>cannot</i> be sorted according 2322 * to the natural ordering of its elements, hence results are undefined.) 2323 * If the range contains multiple 2324 * elements equal to the specified object, there is no guarantee which 2325 * one will be found. 2326 * 2327 * @param a the array to be searched 2328 * @param fromIndex the index of the first element (inclusive) to be 2329 * searched 2330 * @param toIndex the index of the last element (exclusive) to be searched 2331 * @param key the value to be searched for 2332 * @return index of the search key, if it is contained in the array 2333 * within the specified range; 2334 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2335 * <i>insertion point</i> is defined as the point at which the 2336 * key would be inserted into the array: the index of the first 2337 * element in the range greater than the key, 2338 * or {@code toIndex} if all 2339 * elements in the range are less than the specified key. Note 2340 * that this guarantees that the return value will be >= 0 if 2341 * and only if the key is found. 2342 * @throws ClassCastException if the search key is not comparable to the 2343 * elements of the array within the specified range. 2344 * @throws IllegalArgumentException 2345 * if {@code fromIndex > toIndex} 2346 * @throws ArrayIndexOutOfBoundsException 2347 * if {@code fromIndex < 0 or toIndex > a.length} 2348 * @since 1.6 2349 */ binarySearch(Object[] a, int fromIndex, int toIndex, Object key)2350 public static int binarySearch(Object[] a, int fromIndex, int toIndex, 2351 Object key) { 2352 rangeCheck(a.length, fromIndex, toIndex); 2353 return binarySearch0(a, fromIndex, toIndex, key); 2354 } 2355 2356 // Like public version, but without range checks. binarySearch0(Object[] a, int fromIndex, int toIndex, Object key)2357 private static int binarySearch0(Object[] a, int fromIndex, int toIndex, 2358 Object key) { 2359 int low = fromIndex; 2360 int high = toIndex - 1; 2361 2362 while (low <= high) { 2363 int mid = (low + high) >>> 1; 2364 @SuppressWarnings("rawtypes") 2365 Comparable midVal = (Comparable)a[mid]; 2366 @SuppressWarnings("unchecked") 2367 int cmp = midVal.compareTo(key); 2368 2369 if (cmp < 0) 2370 low = mid + 1; 2371 else if (cmp > 0) 2372 high = mid - 1; 2373 else 2374 return mid; // key found 2375 } 2376 return -(low + 1); // key not found. 2377 } 2378 2379 /** 2380 * Searches the specified array for the specified object using the binary 2381 * search algorithm. The array must be sorted into ascending order 2382 * according to the specified comparator (as by the 2383 * {@link #sort(Object[], Comparator) sort(T[], Comparator)} 2384 * method) prior to making this call. If it is 2385 * not sorted, the results are undefined. 2386 * If the array contains multiple 2387 * elements equal to the specified object, there is no guarantee which one 2388 * will be found. 2389 * 2390 * @param <T> the class of the objects in the array 2391 * @param a the array to be searched 2392 * @param key the value to be searched for 2393 * @param c the comparator by which the array is ordered. A 2394 * {@code null} value indicates that the elements' 2395 * {@linkplain Comparable natural ordering} should be used. 2396 * @return index of the search key, if it is contained in the array; 2397 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2398 * <i>insertion point</i> is defined as the point at which the 2399 * key would be inserted into the array: the index of the first 2400 * element greater than the key, or {@code a.length} if all 2401 * elements in the array are less than the specified key. Note 2402 * that this guarantees that the return value will be >= 0 if 2403 * and only if the key is found. 2404 * @throws ClassCastException if the array contains elements that are not 2405 * <i>mutually comparable</i> using the specified comparator, 2406 * or the search key is not comparable to the 2407 * elements of the array using this comparator. 2408 */ binarySearch(T[] a, T key, Comparator<? super T> c)2409 public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) { 2410 return binarySearch0(a, 0, a.length, key, c); 2411 } 2412 2413 /** 2414 * Searches a range of 2415 * the specified array for the specified object using the binary 2416 * search algorithm. 2417 * The range must be sorted into ascending order 2418 * according to the specified comparator (as by the 2419 * {@link #sort(Object[], int, int, Comparator) 2420 * sort(T[], int, int, Comparator)} 2421 * method) prior to making this call. 2422 * If it is not sorted, the results are undefined. 2423 * If the range contains multiple elements equal to the specified object, 2424 * there is no guarantee which one will be found. 2425 * 2426 * @param <T> the class of the objects in the array 2427 * @param a the array to be searched 2428 * @param fromIndex the index of the first element (inclusive) to be 2429 * searched 2430 * @param toIndex the index of the last element (exclusive) to be searched 2431 * @param key the value to be searched for 2432 * @param c the comparator by which the array is ordered. A 2433 * {@code null} value indicates that the elements' 2434 * {@linkplain Comparable natural ordering} should be used. 2435 * @return index of the search key, if it is contained in the array 2436 * within the specified range; 2437 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The 2438 * <i>insertion point</i> is defined as the point at which the 2439 * key would be inserted into the array: the index of the first 2440 * element in the range greater than the key, 2441 * or {@code toIndex} if all 2442 * elements in the range are less than the specified key. Note 2443 * that this guarantees that the return value will be >= 0 if 2444 * and only if the key is found. 2445 * @throws ClassCastException if the range contains elements that are not 2446 * <i>mutually comparable</i> using the specified comparator, 2447 * or the search key is not comparable to the 2448 * elements in the range using this comparator. 2449 * @throws IllegalArgumentException 2450 * if {@code fromIndex > toIndex} 2451 * @throws ArrayIndexOutOfBoundsException 2452 * if {@code fromIndex < 0 or toIndex > a.length} 2453 * @since 1.6 2454 */ binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)2455 public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, 2456 T key, Comparator<? super T> c) { 2457 rangeCheck(a.length, fromIndex, toIndex); 2458 return binarySearch0(a, fromIndex, toIndex, key, c); 2459 } 2460 2461 // Like public version, but without range checks. binarySearch0(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)2462 private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex, 2463 T key, Comparator<? super T> c) { 2464 if (c == null) { 2465 return binarySearch0(a, fromIndex, toIndex, key); 2466 } 2467 int low = fromIndex; 2468 int high = toIndex - 1; 2469 2470 while (low <= high) { 2471 int mid = (low + high) >>> 1; 2472 T midVal = a[mid]; 2473 int cmp = c.compare(midVal, key); 2474 if (cmp < 0) 2475 low = mid + 1; 2476 else if (cmp > 0) 2477 high = mid - 1; 2478 else 2479 return mid; // key found 2480 } 2481 return -(low + 1); // key not found. 2482 } 2483 2484 // Equality Testing 2485 2486 /** 2487 * Returns {@code true} if the two specified arrays of longs are 2488 * <i>equal</i> to one another. Two arrays are considered equal if both 2489 * arrays contain the same number of elements, and all corresponding pairs 2490 * of elements in the two arrays are equal. In other words, two arrays 2491 * are equal if they contain the same elements in the same order. Also, 2492 * two array references are considered equal if both are {@code null}. 2493 * 2494 * @param a one array to be tested for equality 2495 * @param a2 the other array to be tested for equality 2496 * @return {@code true} if the two arrays are equal 2497 */ equals(long[] a, long[] a2)2498 public static boolean equals(long[] a, long[] a2) { 2499 if (a==a2) 2500 return true; 2501 if (a==null || a2==null) 2502 return false; 2503 2504 int length = a.length; 2505 if (a2.length != length) 2506 return false; 2507 2508 return ArraysSupport.mismatch(a, a2, length) < 0; 2509 } 2510 2511 /** 2512 * Returns true if the two specified arrays of longs, over the specified 2513 * ranges, are <i>equal</i> to one another. 2514 * 2515 * <p>Two arrays are considered equal if the number of elements covered by 2516 * each range is the same, and all corresponding pairs of elements over the 2517 * specified ranges in the two arrays are equal. In other words, two arrays 2518 * are equal if they contain, over the specified ranges, the same elements 2519 * in the same order. 2520 * 2521 * @param a the first array to be tested for equality 2522 * @param aFromIndex the index (inclusive) of the first element in the 2523 * first array to be tested 2524 * @param aToIndex the index (exclusive) of the last element in the 2525 * first array to be tested 2526 * @param b the second array to be tested fro equality 2527 * @param bFromIndex the index (inclusive) of the first element in the 2528 * second array to be tested 2529 * @param bToIndex the index (exclusive) of the last element in the 2530 * second array to be tested 2531 * @return {@code true} if the two arrays, over the specified ranges, are 2532 * equal 2533 * @throws IllegalArgumentException 2534 * if {@code aFromIndex > aToIndex} or 2535 * if {@code bFromIndex > bToIndex} 2536 * @throws ArrayIndexOutOfBoundsException 2537 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2538 * if {@code bFromIndex < 0 or bToIndex > b.length} 2539 * @throws NullPointerException 2540 * if either array is {@code null} 2541 * @since 9 2542 */ equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)2543 public static boolean equals(long[] a, int aFromIndex, int aToIndex, 2544 long[] b, int bFromIndex, int bToIndex) { 2545 rangeCheck(a.length, aFromIndex, aToIndex); 2546 rangeCheck(b.length, bFromIndex, bToIndex); 2547 2548 int aLength = aToIndex - aFromIndex; 2549 int bLength = bToIndex - bFromIndex; 2550 if (aLength != bLength) 2551 return false; 2552 2553 return ArraysSupport.mismatch(a, aFromIndex, 2554 b, bFromIndex, 2555 aLength) < 0; 2556 } 2557 2558 /** 2559 * Returns {@code true} if the two specified arrays of ints are 2560 * <i>equal</i> to one another. Two arrays are considered equal if both 2561 * arrays contain the same number of elements, and all corresponding pairs 2562 * of elements in the two arrays are equal. In other words, two arrays 2563 * are equal if they contain the same elements in the same order. Also, 2564 * two array references are considered equal if both are {@code null}. 2565 * 2566 * @param a one array to be tested for equality 2567 * @param a2 the other array to be tested for equality 2568 * @return {@code true} if the two arrays are equal 2569 */ equals(int[] a, int[] a2)2570 public static boolean equals(int[] a, int[] a2) { 2571 if (a==a2) 2572 return true; 2573 if (a==null || a2==null) 2574 return false; 2575 2576 int length = a.length; 2577 if (a2.length != length) 2578 return false; 2579 2580 return ArraysSupport.mismatch(a, a2, length) < 0; 2581 } 2582 2583 /** 2584 * Returns true if the two specified arrays of ints, over the specified 2585 * ranges, are <i>equal</i> to one another. 2586 * 2587 * <p>Two arrays are considered equal if the number of elements covered by 2588 * each range is the same, and all corresponding pairs of elements over the 2589 * specified ranges in the two arrays are equal. In other words, two arrays 2590 * are equal if they contain, over the specified ranges, the same elements 2591 * in the same order. 2592 * 2593 * @param a the first array to be tested for equality 2594 * @param aFromIndex the index (inclusive) of the first element in the 2595 * first array to be tested 2596 * @param aToIndex the index (exclusive) of the last element in the 2597 * first array to be tested 2598 * @param b the second array to be tested fro equality 2599 * @param bFromIndex the index (inclusive) of the first element in the 2600 * second array to be tested 2601 * @param bToIndex the index (exclusive) of the last element in the 2602 * second array to be tested 2603 * @return {@code true} if the two arrays, over the specified ranges, are 2604 * equal 2605 * @throws IllegalArgumentException 2606 * if {@code aFromIndex > aToIndex} or 2607 * if {@code bFromIndex > bToIndex} 2608 * @throws ArrayIndexOutOfBoundsException 2609 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2610 * if {@code bFromIndex < 0 or bToIndex > b.length} 2611 * @throws NullPointerException 2612 * if either array is {@code null} 2613 * @since 9 2614 */ equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)2615 public static boolean equals(int[] a, int aFromIndex, int aToIndex, 2616 int[] b, int bFromIndex, int bToIndex) { 2617 rangeCheck(a.length, aFromIndex, aToIndex); 2618 rangeCheck(b.length, bFromIndex, bToIndex); 2619 2620 int aLength = aToIndex - aFromIndex; 2621 int bLength = bToIndex - bFromIndex; 2622 if (aLength != bLength) 2623 return false; 2624 2625 return ArraysSupport.mismatch(a, aFromIndex, 2626 b, bFromIndex, 2627 aLength) < 0; 2628 } 2629 2630 /** 2631 * Returns {@code true} if the two specified arrays of shorts are 2632 * <i>equal</i> to one another. Two arrays are considered equal if both 2633 * arrays contain the same number of elements, and all corresponding pairs 2634 * of elements in the two arrays are equal. In other words, two arrays 2635 * are equal if they contain the same elements in the same order. Also, 2636 * two array references are considered equal if both are {@code null}. 2637 * 2638 * @param a one array to be tested for equality 2639 * @param a2 the other array to be tested for equality 2640 * @return {@code true} if the two arrays are equal 2641 */ equals(short[] a, short a2[])2642 public static boolean equals(short[] a, short a2[]) { 2643 if (a==a2) 2644 return true; 2645 if (a==null || a2==null) 2646 return false; 2647 2648 int length = a.length; 2649 if (a2.length != length) 2650 return false; 2651 2652 return ArraysSupport.mismatch(a, a2, length) < 0; 2653 } 2654 2655 /** 2656 * Returns true if the two specified arrays of shorts, over the specified 2657 * ranges, are <i>equal</i> to one another. 2658 * 2659 * <p>Two arrays are considered equal if the number of elements covered by 2660 * each range is the same, and all corresponding pairs of elements over the 2661 * specified ranges in the two arrays are equal. In other words, two arrays 2662 * are equal if they contain, over the specified ranges, the same elements 2663 * in the same order. 2664 * 2665 * @param a the first array to be tested for equality 2666 * @param aFromIndex the index (inclusive) of the first element in the 2667 * first array to be tested 2668 * @param aToIndex the index (exclusive) of the last element in the 2669 * first array to be tested 2670 * @param b the second array to be tested fro equality 2671 * @param bFromIndex the index (inclusive) of the first element in the 2672 * second array to be tested 2673 * @param bToIndex the index (exclusive) of the last element in the 2674 * second array to be tested 2675 * @return {@code true} if the two arrays, over the specified ranges, are 2676 * equal 2677 * @throws IllegalArgumentException 2678 * if {@code aFromIndex > aToIndex} or 2679 * if {@code bFromIndex > bToIndex} 2680 * @throws ArrayIndexOutOfBoundsException 2681 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2682 * if {@code bFromIndex < 0 or bToIndex > b.length} 2683 * @throws NullPointerException 2684 * if either array is {@code null} 2685 * @since 9 2686 */ equals(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)2687 public static boolean equals(short[] a, int aFromIndex, int aToIndex, 2688 short[] b, int bFromIndex, int bToIndex) { 2689 rangeCheck(a.length, aFromIndex, aToIndex); 2690 rangeCheck(b.length, bFromIndex, bToIndex); 2691 2692 int aLength = aToIndex - aFromIndex; 2693 int bLength = bToIndex - bFromIndex; 2694 if (aLength != bLength) 2695 return false; 2696 2697 return ArraysSupport.mismatch(a, aFromIndex, 2698 b, bFromIndex, 2699 aLength) < 0; 2700 } 2701 2702 /** 2703 * Returns {@code true} if the two specified arrays of chars are 2704 * <i>equal</i> to one another. Two arrays are considered equal if both 2705 * arrays contain the same number of elements, and all corresponding pairs 2706 * of elements in the two arrays are equal. In other words, two arrays 2707 * are equal if they contain the same elements in the same order. Also, 2708 * two array references are considered equal if both are {@code null}. 2709 * 2710 * @param a one array to be tested for equality 2711 * @param a2 the other array to be tested for equality 2712 * @return {@code true} if the two arrays are equal 2713 */ 2714 @HotSpotIntrinsicCandidate equals(char[] a, char[] a2)2715 public static boolean equals(char[] a, char[] a2) { 2716 if (a==a2) 2717 return true; 2718 if (a==null || a2==null) 2719 return false; 2720 2721 int length = a.length; 2722 if (a2.length != length) 2723 return false; 2724 2725 return ArraysSupport.mismatch(a, a2, length) < 0; 2726 } 2727 2728 /** 2729 * Returns true if the two specified arrays of chars, over the specified 2730 * ranges, are <i>equal</i> to one another. 2731 * 2732 * <p>Two arrays are considered equal if the number of elements covered by 2733 * each range is the same, and all corresponding pairs of elements over the 2734 * specified ranges in the two arrays are equal. In other words, two arrays 2735 * are equal if they contain, over the specified ranges, the same elements 2736 * in the same order. 2737 * 2738 * @param a the first array to be tested for equality 2739 * @param aFromIndex the index (inclusive) of the first element in the 2740 * first array to be tested 2741 * @param aToIndex the index (exclusive) of the last element in the 2742 * first array to be tested 2743 * @param b the second array to be tested fro equality 2744 * @param bFromIndex the index (inclusive) of the first element in the 2745 * second array to be tested 2746 * @param bToIndex the index (exclusive) of the last element in the 2747 * second array to be tested 2748 * @return {@code true} if the two arrays, over the specified ranges, are 2749 * equal 2750 * @throws IllegalArgumentException 2751 * if {@code aFromIndex > aToIndex} or 2752 * if {@code bFromIndex > bToIndex} 2753 * @throws ArrayIndexOutOfBoundsException 2754 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2755 * if {@code bFromIndex < 0 or bToIndex > b.length} 2756 * @throws NullPointerException 2757 * if either array is {@code null} 2758 * @since 9 2759 */ equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)2760 public static boolean equals(char[] a, int aFromIndex, int aToIndex, 2761 char[] b, int bFromIndex, int bToIndex) { 2762 rangeCheck(a.length, aFromIndex, aToIndex); 2763 rangeCheck(b.length, bFromIndex, bToIndex); 2764 2765 int aLength = aToIndex - aFromIndex; 2766 int bLength = bToIndex - bFromIndex; 2767 if (aLength != bLength) 2768 return false; 2769 2770 return ArraysSupport.mismatch(a, aFromIndex, 2771 b, bFromIndex, 2772 aLength) < 0; 2773 } 2774 2775 /** 2776 * Returns {@code true} if the two specified arrays of bytes are 2777 * <i>equal</i> to one another. Two arrays are considered equal if both 2778 * arrays contain the same number of elements, and all corresponding pairs 2779 * of elements in the two arrays are equal. In other words, two arrays 2780 * are equal if they contain the same elements in the same order. Also, 2781 * two array references are considered equal if both are {@code null}. 2782 * 2783 * @param a one array to be tested for equality 2784 * @param a2 the other array to be tested for equality 2785 * @return {@code true} if the two arrays are equal 2786 */ 2787 @HotSpotIntrinsicCandidate equals(byte[] a, byte[] a2)2788 public static boolean equals(byte[] a, byte[] a2) { 2789 if (a==a2) 2790 return true; 2791 if (a==null || a2==null) 2792 return false; 2793 2794 int length = a.length; 2795 if (a2.length != length) 2796 return false; 2797 2798 return ArraysSupport.mismatch(a, a2, length) < 0; 2799 } 2800 2801 /** 2802 * Returns true if the two specified arrays of bytes, over the specified 2803 * ranges, are <i>equal</i> to one another. 2804 * 2805 * <p>Two arrays are considered equal if the number of elements covered by 2806 * each range is the same, and all corresponding pairs of elements over the 2807 * specified ranges in the two arrays are equal. In other words, two arrays 2808 * are equal if they contain, over the specified ranges, the same elements 2809 * in the same order. 2810 * 2811 * @param a the first array to be tested for equality 2812 * @param aFromIndex the index (inclusive) of the first element in the 2813 * first array to be tested 2814 * @param aToIndex the index (exclusive) of the last element in the 2815 * first array to be tested 2816 * @param b the second array to be tested fro equality 2817 * @param bFromIndex the index (inclusive) of the first element in the 2818 * second array to be tested 2819 * @param bToIndex the index (exclusive) of the last element in the 2820 * second array to be tested 2821 * @return {@code true} if the two arrays, over the specified ranges, are 2822 * equal 2823 * @throws IllegalArgumentException 2824 * if {@code aFromIndex > aToIndex} or 2825 * if {@code bFromIndex > bToIndex} 2826 * @throws ArrayIndexOutOfBoundsException 2827 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2828 * if {@code bFromIndex < 0 or bToIndex > b.length} 2829 * @throws NullPointerException 2830 * if either array is {@code null} 2831 * @since 9 2832 */ equals(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)2833 public static boolean equals(byte[] a, int aFromIndex, int aToIndex, 2834 byte[] b, int bFromIndex, int bToIndex) { 2835 rangeCheck(a.length, aFromIndex, aToIndex); 2836 rangeCheck(b.length, bFromIndex, bToIndex); 2837 2838 int aLength = aToIndex - aFromIndex; 2839 int bLength = bToIndex - bFromIndex; 2840 if (aLength != bLength) 2841 return false; 2842 2843 return ArraysSupport.mismatch(a, aFromIndex, 2844 b, bFromIndex, 2845 aLength) < 0; 2846 } 2847 2848 /** 2849 * Returns {@code true} if the two specified arrays of booleans are 2850 * <i>equal</i> to one another. Two arrays are considered equal if both 2851 * arrays contain the same number of elements, and all corresponding pairs 2852 * of elements in the two arrays are equal. In other words, two arrays 2853 * are equal if they contain the same elements in the same order. Also, 2854 * two array references are considered equal if both are {@code null}. 2855 * 2856 * @param a one array to be tested for equality 2857 * @param a2 the other array to be tested for equality 2858 * @return {@code true} if the two arrays are equal 2859 */ equals(boolean[] a, boolean[] a2)2860 public static boolean equals(boolean[] a, boolean[] a2) { 2861 if (a==a2) 2862 return true; 2863 if (a==null || a2==null) 2864 return false; 2865 2866 int length = a.length; 2867 if (a2.length != length) 2868 return false; 2869 2870 return ArraysSupport.mismatch(a, a2, length) < 0; 2871 } 2872 2873 /** 2874 * Returns true if the two specified arrays of booleans, over the specified 2875 * ranges, are <i>equal</i> to one another. 2876 * 2877 * <p>Two arrays are considered equal if the number of elements covered by 2878 * each range is the same, and all corresponding pairs of elements over the 2879 * specified ranges in the two arrays are equal. In other words, two arrays 2880 * are equal if they contain, over the specified ranges, the same elements 2881 * in the same order. 2882 * 2883 * @param a the first array to be tested for equality 2884 * @param aFromIndex the index (inclusive) of the first element in the 2885 * first array to be tested 2886 * @param aToIndex the index (exclusive) of the last element in the 2887 * first array to be tested 2888 * @param b the second array to be tested fro equality 2889 * @param bFromIndex the index (inclusive) of the first element in the 2890 * second array to be tested 2891 * @param bToIndex the index (exclusive) of the last element in the 2892 * second array to be tested 2893 * @return {@code true} if the two arrays, over the specified ranges, are 2894 * equal 2895 * @throws IllegalArgumentException 2896 * if {@code aFromIndex > aToIndex} or 2897 * if {@code bFromIndex > bToIndex} 2898 * @throws ArrayIndexOutOfBoundsException 2899 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2900 * if {@code bFromIndex < 0 or bToIndex > b.length} 2901 * @throws NullPointerException 2902 * if either array is {@code null} 2903 * @since 9 2904 */ equals(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)2905 public static boolean equals(boolean[] a, int aFromIndex, int aToIndex, 2906 boolean[] b, int bFromIndex, int bToIndex) { 2907 rangeCheck(a.length, aFromIndex, aToIndex); 2908 rangeCheck(b.length, bFromIndex, bToIndex); 2909 2910 int aLength = aToIndex - aFromIndex; 2911 int bLength = bToIndex - bFromIndex; 2912 if (aLength != bLength) 2913 return false; 2914 2915 return ArraysSupport.mismatch(a, aFromIndex, 2916 b, bFromIndex, 2917 aLength) < 0; 2918 } 2919 2920 /** 2921 * Returns {@code true} if the two specified arrays of doubles are 2922 * <i>equal</i> to one another. Two arrays are considered equal if both 2923 * arrays contain the same number of elements, and all corresponding pairs 2924 * of elements in the two arrays are equal. In other words, two arrays 2925 * are equal if they contain the same elements in the same order. Also, 2926 * two array references are considered equal if both are {@code null}. 2927 * 2928 * Two doubles {@code d1} and {@code d2} are considered equal if: 2929 * <pre> {@code new Double(d1).equals(new Double(d2))}</pre> 2930 * (Unlike the {@code ==} operator, this method considers 2931 * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.) 2932 * 2933 * @param a one array to be tested for equality 2934 * @param a2 the other array to be tested for equality 2935 * @return {@code true} if the two arrays are equal 2936 * @see Double#equals(Object) 2937 */ equals(double[] a, double[] a2)2938 public static boolean equals(double[] a, double[] a2) { 2939 if (a==a2) 2940 return true; 2941 if (a==null || a2==null) 2942 return false; 2943 2944 int length = a.length; 2945 if (a2.length != length) 2946 return false; 2947 2948 return ArraysSupport.mismatch(a, a2, length) < 0; 2949 } 2950 2951 /** 2952 * Returns true if the two specified arrays of doubles, over the specified 2953 * ranges, are <i>equal</i> to one another. 2954 * 2955 * <p>Two arrays are considered equal if the number of elements covered by 2956 * each range is the same, and all corresponding pairs of elements over the 2957 * specified ranges in the two arrays are equal. In other words, two arrays 2958 * are equal if they contain, over the specified ranges, the same elements 2959 * in the same order. 2960 * 2961 * <p>Two doubles {@code d1} and {@code d2} are considered equal if: 2962 * <pre> {@code new Double(d1).equals(new Double(d2))}</pre> 2963 * (Unlike the {@code ==} operator, this method considers 2964 * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.) 2965 * 2966 * @param a the first array to be tested for equality 2967 * @param aFromIndex the index (inclusive) of the first element in the 2968 * first array to be tested 2969 * @param aToIndex the index (exclusive) of the last element in the 2970 * first array to be tested 2971 * @param b the second array to be tested fro equality 2972 * @param bFromIndex the index (inclusive) of the first element in the 2973 * second array to be tested 2974 * @param bToIndex the index (exclusive) of the last element in the 2975 * second array to be tested 2976 * @return {@code true} if the two arrays, over the specified ranges, are 2977 * equal 2978 * @throws IllegalArgumentException 2979 * if {@code aFromIndex > aToIndex} or 2980 * if {@code bFromIndex > bToIndex} 2981 * @throws ArrayIndexOutOfBoundsException 2982 * if {@code aFromIndex < 0 or aToIndex > a.length} or 2983 * if {@code bFromIndex < 0 or bToIndex > b.length} 2984 * @throws NullPointerException 2985 * if either array is {@code null} 2986 * @see Double#equals(Object) 2987 * @since 9 2988 */ equals(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)2989 public static boolean equals(double[] a, int aFromIndex, int aToIndex, 2990 double[] b, int bFromIndex, int bToIndex) { 2991 rangeCheck(a.length, aFromIndex, aToIndex); 2992 rangeCheck(b.length, bFromIndex, bToIndex); 2993 2994 int aLength = aToIndex - aFromIndex; 2995 int bLength = bToIndex - bFromIndex; 2996 if (aLength != bLength) 2997 return false; 2998 2999 return ArraysSupport.mismatch(a, aFromIndex, 3000 b, bFromIndex, aLength) < 0; 3001 } 3002 3003 /** 3004 * Returns {@code true} if the two specified arrays of floats are 3005 * <i>equal</i> to one another. Two arrays are considered equal if both 3006 * arrays contain the same number of elements, and all corresponding pairs 3007 * of elements in the two arrays are equal. In other words, two arrays 3008 * are equal if they contain the same elements in the same order. Also, 3009 * two array references are considered equal if both are {@code null}. 3010 * 3011 * Two floats {@code f1} and {@code f2} are considered equal if: 3012 * <pre> {@code new Float(f1).equals(new Float(f2))}</pre> 3013 * (Unlike the {@code ==} operator, this method considers 3014 * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.) 3015 * 3016 * @param a one array to be tested for equality 3017 * @param a2 the other array to be tested for equality 3018 * @return {@code true} if the two arrays are equal 3019 * @see Float#equals(Object) 3020 */ equals(float[] a, float[] a2)3021 public static boolean equals(float[] a, float[] a2) { 3022 if (a==a2) 3023 return true; 3024 if (a==null || a2==null) 3025 return false; 3026 3027 int length = a.length; 3028 if (a2.length != length) 3029 return false; 3030 3031 return ArraysSupport.mismatch(a, a2, length) < 0; 3032 } 3033 3034 /** 3035 * Returns true if the two specified arrays of floats, over the specified 3036 * ranges, are <i>equal</i> to one another. 3037 * 3038 * <p>Two arrays are considered equal if the number of elements covered by 3039 * each range is the same, and all corresponding pairs of elements over the 3040 * specified ranges in the two arrays are equal. In other words, two arrays 3041 * are equal if they contain, over the specified ranges, the same elements 3042 * in the same order. 3043 * 3044 * <p>Two floats {@code f1} and {@code f2} are considered equal if: 3045 * <pre> {@code new Float(f1).equals(new Float(f2))}</pre> 3046 * (Unlike the {@code ==} operator, this method considers 3047 * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.) 3048 * 3049 * @param a the first array to be tested for equality 3050 * @param aFromIndex the index (inclusive) of the first element in the 3051 * first array to be tested 3052 * @param aToIndex the index (exclusive) of the last element in the 3053 * first array to be tested 3054 * @param b the second array to be tested fro equality 3055 * @param bFromIndex the index (inclusive) of the first element in the 3056 * second array to be tested 3057 * @param bToIndex the index (exclusive) of the last element in the 3058 * second array to be tested 3059 * @return {@code true} if the two arrays, over the specified ranges, are 3060 * equal 3061 * @throws IllegalArgumentException 3062 * if {@code aFromIndex > aToIndex} or 3063 * if {@code bFromIndex > bToIndex} 3064 * @throws ArrayIndexOutOfBoundsException 3065 * if {@code aFromIndex < 0 or aToIndex > a.length} or 3066 * if {@code bFromIndex < 0 or bToIndex > b.length} 3067 * @throws NullPointerException 3068 * if either array is {@code null} 3069 * @see Float#equals(Object) 3070 * @since 9 3071 */ equals(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)3072 public static boolean equals(float[] a, int aFromIndex, int aToIndex, 3073 float[] b, int bFromIndex, int bToIndex) { 3074 rangeCheck(a.length, aFromIndex, aToIndex); 3075 rangeCheck(b.length, bFromIndex, bToIndex); 3076 3077 int aLength = aToIndex - aFromIndex; 3078 int bLength = bToIndex - bFromIndex; 3079 if (aLength != bLength) 3080 return false; 3081 3082 return ArraysSupport.mismatch(a, aFromIndex, 3083 b, bFromIndex, aLength) < 0; 3084 } 3085 3086 /** 3087 * Returns {@code true} if the two specified arrays of Objects are 3088 * <i>equal</i> to one another. The two arrays are considered equal if 3089 * both arrays contain the same number of elements, and all corresponding 3090 * pairs of elements in the two arrays are equal. Two objects {@code e1} 3091 * and {@code e2} are considered <i>equal</i> if 3092 * {@code Objects.equals(e1, e2)}. 3093 * In other words, the two arrays are equal if 3094 * they contain the same elements in the same order. Also, two array 3095 * references are considered equal if both are {@code null}. 3096 * 3097 * @param a one array to be tested for equality 3098 * @param a2 the other array to be tested for equality 3099 * @return {@code true} if the two arrays are equal 3100 */ equals(Object[] a, Object[] a2)3101 public static boolean equals(Object[] a, Object[] a2) { 3102 if (a==a2) 3103 return true; 3104 if (a==null || a2==null) 3105 return false; 3106 3107 int length = a.length; 3108 if (a2.length != length) 3109 return false; 3110 3111 for (int i=0; i<length; i++) { 3112 if (!Objects.equals(a[i], a2[i])) 3113 return false; 3114 } 3115 3116 return true; 3117 } 3118 3119 /** 3120 * Returns true if the two specified arrays of Objects, over the specified 3121 * ranges, are <i>equal</i> to one another. 3122 * 3123 * <p>Two arrays are considered equal if the number of elements covered by 3124 * each range is the same, and all corresponding pairs of elements over the 3125 * specified ranges in the two arrays are equal. In other words, two arrays 3126 * are equal if they contain, over the specified ranges, the same elements 3127 * in the same order. 3128 * 3129 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if 3130 * {@code Objects.equals(e1, e2)}. 3131 * 3132 * @param a the first array to be tested for equality 3133 * @param aFromIndex the index (inclusive) of the first element in the 3134 * first array to be tested 3135 * @param aToIndex the index (exclusive) of the last element in the 3136 * first array to be tested 3137 * @param b the second array to be tested fro equality 3138 * @param bFromIndex the index (inclusive) of the first element in the 3139 * second array to be tested 3140 * @param bToIndex the index (exclusive) of the last element in the 3141 * second array to be tested 3142 * @return {@code true} if the two arrays, over the specified ranges, are 3143 * equal 3144 * @throws IllegalArgumentException 3145 * if {@code aFromIndex > aToIndex} or 3146 * if {@code bFromIndex > bToIndex} 3147 * @throws ArrayIndexOutOfBoundsException 3148 * if {@code aFromIndex < 0 or aToIndex > a.length} or 3149 * if {@code bFromIndex < 0 or bToIndex > b.length} 3150 * @throws NullPointerException 3151 * if either array is {@code null} 3152 * @since 9 3153 */ equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)3154 public static boolean equals(Object[] a, int aFromIndex, int aToIndex, 3155 Object[] b, int bFromIndex, int bToIndex) { 3156 rangeCheck(a.length, aFromIndex, aToIndex); 3157 rangeCheck(b.length, bFromIndex, bToIndex); 3158 3159 int aLength = aToIndex - aFromIndex; 3160 int bLength = bToIndex - bFromIndex; 3161 if (aLength != bLength) 3162 return false; 3163 3164 for (int i = 0; i < aLength; i++) { 3165 if (!Objects.equals(a[aFromIndex++], b[bFromIndex++])) 3166 return false; 3167 } 3168 3169 return true; 3170 } 3171 3172 /** 3173 * Returns {@code true} if the two specified arrays of Objects are 3174 * <i>equal</i> to one another. 3175 * 3176 * <p>Two arrays are considered equal if both arrays contain the same number 3177 * of elements, and all corresponding pairs of elements in the two arrays 3178 * are equal. In other words, the two arrays are equal if they contain the 3179 * same elements in the same order. Also, two array references are 3180 * considered equal if both are {@code null}. 3181 * 3182 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if, 3183 * given the specified comparator, {@code cmp.compare(e1, e2) == 0}. 3184 * 3185 * @param a one array to be tested for equality 3186 * @param a2 the other array to be tested for equality 3187 * @param cmp the comparator to compare array elements 3188 * @param <T> the type of array elements 3189 * @return {@code true} if the two arrays are equal 3190 * @throws NullPointerException if the comparator is {@code null} 3191 * @since 9 3192 */ equals(T[] a, T[] a2, Comparator<? super T> cmp)3193 public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) { 3194 Objects.requireNonNull(cmp); 3195 if (a==a2) 3196 return true; 3197 if (a==null || a2==null) 3198 return false; 3199 3200 int length = a.length; 3201 if (a2.length != length) 3202 return false; 3203 3204 for (int i=0; i<length; i++) { 3205 if (cmp.compare(a[i], a2[i]) != 0) 3206 return false; 3207 } 3208 3209 return true; 3210 } 3211 3212 /** 3213 * Returns true if the two specified arrays of Objects, over the specified 3214 * ranges, are <i>equal</i> to one another. 3215 * 3216 * <p>Two arrays are considered equal if the number of elements covered by 3217 * each range is the same, and all corresponding pairs of elements over the 3218 * specified ranges in the two arrays are equal. In other words, two arrays 3219 * are equal if they contain, over the specified ranges, the same elements 3220 * in the same order. 3221 * 3222 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if, 3223 * given the specified comparator, {@code cmp.compare(e1, e2) == 0}. 3224 * 3225 * @param a the first array to be tested for equality 3226 * @param aFromIndex the index (inclusive) of the first element in the 3227 * first array to be tested 3228 * @param aToIndex the index (exclusive) of the last element in the 3229 * first array to be tested 3230 * @param b the second array to be tested fro equality 3231 * @param bFromIndex the index (inclusive) of the first element in the 3232 * second array to be tested 3233 * @param bToIndex the index (exclusive) of the last element in the 3234 * second array to be tested 3235 * @param cmp the comparator to compare array elements 3236 * @param <T> the type of array elements 3237 * @return {@code true} if the two arrays, over the specified ranges, are 3238 * equal 3239 * @throws IllegalArgumentException 3240 * if {@code aFromIndex > aToIndex} or 3241 * if {@code bFromIndex > bToIndex} 3242 * @throws ArrayIndexOutOfBoundsException 3243 * if {@code aFromIndex < 0 or aToIndex > a.length} or 3244 * if {@code bFromIndex < 0 or bToIndex > b.length} 3245 * @throws NullPointerException 3246 * if either array or the comparator is {@code null} 3247 * @since 9 3248 */ equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)3249 public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex, 3250 T[] b, int bFromIndex, int bToIndex, 3251 Comparator<? super T> cmp) { 3252 Objects.requireNonNull(cmp); 3253 rangeCheck(a.length, aFromIndex, aToIndex); 3254 rangeCheck(b.length, bFromIndex, bToIndex); 3255 3256 int aLength = aToIndex - aFromIndex; 3257 int bLength = bToIndex - bFromIndex; 3258 if (aLength != bLength) 3259 return false; 3260 3261 for (int i = 0; i < aLength; i++) { 3262 if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0) 3263 return false; 3264 } 3265 3266 return true; 3267 } 3268 3269 // Filling 3270 3271 /** 3272 * Assigns the specified long value to each element of the specified array 3273 * of longs. 3274 * 3275 * @param a the array to be filled 3276 * @param val the value to be stored in all elements of the array 3277 */ fill(long[] a, long val)3278 public static void fill(long[] a, long val) { 3279 for (int i = 0, len = a.length; i < len; i++) 3280 a[i] = val; 3281 } 3282 3283 /** 3284 * Assigns the specified long value to each element of the specified 3285 * range of the specified array of longs. The range to be filled 3286 * extends from index {@code fromIndex}, inclusive, to index 3287 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3288 * range to be filled is empty.) 3289 * 3290 * @param a the array to be filled 3291 * @param fromIndex the index of the first element (inclusive) to be 3292 * filled with the specified value 3293 * @param toIndex the index of the last element (exclusive) to be 3294 * filled with the specified value 3295 * @param val the value to be stored in all elements of the array 3296 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3297 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3298 * {@code toIndex > a.length} 3299 */ fill(long[] a, int fromIndex, int toIndex, long val)3300 public static void fill(long[] a, int fromIndex, int toIndex, long val) { 3301 rangeCheck(a.length, fromIndex, toIndex); 3302 for (int i = fromIndex; i < toIndex; i++) 3303 a[i] = val; 3304 } 3305 3306 /** 3307 * Assigns the specified int value to each element of the specified array 3308 * of ints. 3309 * 3310 * @param a the array to be filled 3311 * @param val the value to be stored in all elements of the array 3312 */ fill(int[] a, int val)3313 public static void fill(int[] a, int val) { 3314 for (int i = 0, len = a.length; i < len; i++) 3315 a[i] = val; 3316 } 3317 3318 /** 3319 * Assigns the specified int value to each element of the specified 3320 * range of the specified array of ints. The range to be filled 3321 * extends from index {@code fromIndex}, inclusive, to index 3322 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3323 * range to be filled is empty.) 3324 * 3325 * @param a the array to be filled 3326 * @param fromIndex the index of the first element (inclusive) to be 3327 * filled with the specified value 3328 * @param toIndex the index of the last element (exclusive) to be 3329 * filled with the specified value 3330 * @param val the value to be stored in all elements of the array 3331 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3332 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3333 * {@code toIndex > a.length} 3334 */ fill(int[] a, int fromIndex, int toIndex, int val)3335 public static void fill(int[] a, int fromIndex, int toIndex, int val) { 3336 rangeCheck(a.length, fromIndex, toIndex); 3337 for (int i = fromIndex; i < toIndex; i++) 3338 a[i] = val; 3339 } 3340 3341 /** 3342 * Assigns the specified short value to each element of the specified array 3343 * of shorts. 3344 * 3345 * @param a the array to be filled 3346 * @param val the value to be stored in all elements of the array 3347 */ fill(short[] a, short val)3348 public static void fill(short[] a, short val) { 3349 for (int i = 0, len = a.length; i < len; i++) 3350 a[i] = val; 3351 } 3352 3353 /** 3354 * Assigns the specified short value to each element of the specified 3355 * range of the specified array of shorts. The range to be filled 3356 * extends from index {@code fromIndex}, inclusive, to index 3357 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3358 * range to be filled is empty.) 3359 * 3360 * @param a the array to be filled 3361 * @param fromIndex the index of the first element (inclusive) to be 3362 * filled with the specified value 3363 * @param toIndex the index of the last element (exclusive) to be 3364 * filled with the specified value 3365 * @param val the value to be stored in all elements of the array 3366 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3367 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3368 * {@code toIndex > a.length} 3369 */ fill(short[] a, int fromIndex, int toIndex, short val)3370 public static void fill(short[] a, int fromIndex, int toIndex, short val) { 3371 rangeCheck(a.length, fromIndex, toIndex); 3372 for (int i = fromIndex; i < toIndex; i++) 3373 a[i] = val; 3374 } 3375 3376 /** 3377 * Assigns the specified char value to each element of the specified array 3378 * of chars. 3379 * 3380 * @param a the array to be filled 3381 * @param val the value to be stored in all elements of the array 3382 */ fill(char[] a, char val)3383 public static void fill(char[] a, char val) { 3384 for (int i = 0, len = a.length; i < len; i++) 3385 a[i] = val; 3386 } 3387 3388 /** 3389 * Assigns the specified char value to each element of the specified 3390 * range of the specified array of chars. The range to be filled 3391 * extends from index {@code fromIndex}, inclusive, to index 3392 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3393 * range to be filled is empty.) 3394 * 3395 * @param a the array to be filled 3396 * @param fromIndex the index of the first element (inclusive) to be 3397 * filled with the specified value 3398 * @param toIndex the index of the last element (exclusive) to be 3399 * filled with the specified value 3400 * @param val the value to be stored in all elements of the array 3401 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3402 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3403 * {@code toIndex > a.length} 3404 */ fill(char[] a, int fromIndex, int toIndex, char val)3405 public static void fill(char[] a, int fromIndex, int toIndex, char val) { 3406 rangeCheck(a.length, fromIndex, toIndex); 3407 for (int i = fromIndex; i < toIndex; i++) 3408 a[i] = val; 3409 } 3410 3411 /** 3412 * Assigns the specified byte value to each element of the specified array 3413 * of bytes. 3414 * 3415 * @param a the array to be filled 3416 * @param val the value to be stored in all elements of the array 3417 */ fill(byte[] a, byte val)3418 public static void fill(byte[] a, byte val) { 3419 for (int i = 0, len = a.length; i < len; i++) 3420 a[i] = val; 3421 } 3422 3423 /** 3424 * Assigns the specified byte value to each element of the specified 3425 * range of the specified array of bytes. The range to be filled 3426 * extends from index {@code fromIndex}, inclusive, to index 3427 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3428 * range to be filled is empty.) 3429 * 3430 * @param a the array to be filled 3431 * @param fromIndex the index of the first element (inclusive) to be 3432 * filled with the specified value 3433 * @param toIndex the index of the last element (exclusive) to be 3434 * filled with the specified value 3435 * @param val the value to be stored in all elements of the array 3436 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3437 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3438 * {@code toIndex > a.length} 3439 */ fill(byte[] a, int fromIndex, int toIndex, byte val)3440 public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { 3441 rangeCheck(a.length, fromIndex, toIndex); 3442 for (int i = fromIndex; i < toIndex; i++) 3443 a[i] = val; 3444 } 3445 3446 /** 3447 * Assigns the specified boolean value to each element of the specified 3448 * array of booleans. 3449 * 3450 * @param a the array to be filled 3451 * @param val the value to be stored in all elements of the array 3452 */ fill(boolean[] a, boolean val)3453 public static void fill(boolean[] a, boolean val) { 3454 for (int i = 0, len = a.length; i < len; i++) 3455 a[i] = val; 3456 } 3457 3458 /** 3459 * Assigns the specified boolean value to each element of the specified 3460 * range of the specified array of booleans. The range to be filled 3461 * extends from index {@code fromIndex}, inclusive, to index 3462 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3463 * range to be filled is empty.) 3464 * 3465 * @param a the array to be filled 3466 * @param fromIndex the index of the first element (inclusive) to be 3467 * filled with the specified value 3468 * @param toIndex the index of the last element (exclusive) to be 3469 * filled with the specified value 3470 * @param val the value to be stored in all elements of the array 3471 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3472 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3473 * {@code toIndex > a.length} 3474 */ fill(boolean[] a, int fromIndex, int toIndex, boolean val)3475 public static void fill(boolean[] a, int fromIndex, int toIndex, 3476 boolean val) { 3477 rangeCheck(a.length, fromIndex, toIndex); 3478 for (int i = fromIndex; i < toIndex; i++) 3479 a[i] = val; 3480 } 3481 3482 /** 3483 * Assigns the specified double value to each element of the specified 3484 * array of doubles. 3485 * 3486 * @param a the array to be filled 3487 * @param val the value to be stored in all elements of the array 3488 */ fill(double[] a, double val)3489 public static void fill(double[] a, double val) { 3490 for (int i = 0, len = a.length; i < len; i++) 3491 a[i] = val; 3492 } 3493 3494 /** 3495 * Assigns the specified double value to each element of the specified 3496 * range of the specified array of doubles. The range to be filled 3497 * extends from index {@code fromIndex}, inclusive, to index 3498 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3499 * range to be filled is empty.) 3500 * 3501 * @param a the array to be filled 3502 * @param fromIndex the index of the first element (inclusive) to be 3503 * filled with the specified value 3504 * @param toIndex the index of the last element (exclusive) to be 3505 * filled with the specified value 3506 * @param val the value to be stored in all elements of the array 3507 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3508 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3509 * {@code toIndex > a.length} 3510 */ fill(double[] a, int fromIndex, int toIndex,double val)3511 public static void fill(double[] a, int fromIndex, int toIndex,double val){ 3512 rangeCheck(a.length, fromIndex, toIndex); 3513 for (int i = fromIndex; i < toIndex; i++) 3514 a[i] = val; 3515 } 3516 3517 /** 3518 * Assigns the specified float value to each element of the specified array 3519 * of floats. 3520 * 3521 * @param a the array to be filled 3522 * @param val the value to be stored in all elements of the array 3523 */ fill(float[] a, float val)3524 public static void fill(float[] a, float val) { 3525 for (int i = 0, len = a.length; i < len; i++) 3526 a[i] = val; 3527 } 3528 3529 /** 3530 * Assigns the specified float value to each element of the specified 3531 * range of the specified array of floats. The range to be filled 3532 * extends from index {@code fromIndex}, inclusive, to index 3533 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3534 * range to be filled is empty.) 3535 * 3536 * @param a the array to be filled 3537 * @param fromIndex the index of the first element (inclusive) to be 3538 * filled with the specified value 3539 * @param toIndex the index of the last element (exclusive) to be 3540 * filled with the specified value 3541 * @param val the value to be stored in all elements of the array 3542 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3543 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3544 * {@code toIndex > a.length} 3545 */ fill(float[] a, int fromIndex, int toIndex, float val)3546 public static void fill(float[] a, int fromIndex, int toIndex, float val) { 3547 rangeCheck(a.length, fromIndex, toIndex); 3548 for (int i = fromIndex; i < toIndex; i++) 3549 a[i] = val; 3550 } 3551 3552 /** 3553 * Assigns the specified Object reference to each element of the specified 3554 * array of Objects. 3555 * 3556 * @param a the array to be filled 3557 * @param val the value to be stored in all elements of the array 3558 * @throws ArrayStoreException if the specified value is not of a 3559 * runtime type that can be stored in the specified array 3560 */ fill(Object[] a, Object val)3561 public static void fill(Object[] a, Object val) { 3562 for (int i = 0, len = a.length; i < len; i++) 3563 a[i] = val; 3564 } 3565 3566 /** 3567 * Assigns the specified Object reference to each element of the specified 3568 * range of the specified array of Objects. The range to be filled 3569 * extends from index {@code fromIndex}, inclusive, to index 3570 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 3571 * range to be filled is empty.) 3572 * 3573 * @param a the array to be filled 3574 * @param fromIndex the index of the first element (inclusive) to be 3575 * filled with the specified value 3576 * @param toIndex the index of the last element (exclusive) to be 3577 * filled with the specified value 3578 * @param val the value to be stored in all elements of the array 3579 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 3580 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 3581 * {@code toIndex > a.length} 3582 * @throws ArrayStoreException if the specified value is not of a 3583 * runtime type that can be stored in the specified array 3584 */ fill(Object[] a, int fromIndex, int toIndex, Object val)3585 public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { 3586 rangeCheck(a.length, fromIndex, toIndex); 3587 for (int i = fromIndex; i < toIndex; i++) 3588 a[i] = val; 3589 } 3590 3591 // Cloning 3592 3593 /** 3594 * Copies the specified array, truncating or padding with nulls (if necessary) 3595 * so the copy has the specified length. For all indices that are 3596 * valid in both the original array and the copy, the two arrays will 3597 * contain identical values. For any indices that are valid in the 3598 * copy but not the original, the copy will contain {@code null}. 3599 * Such indices will exist if and only if the specified length 3600 * is greater than that of the original array. 3601 * The resulting array is of exactly the same class as the original array. 3602 * 3603 * @param <T> the class of the objects in the array 3604 * @param original the array to be copied 3605 * @param newLength the length of the copy to be returned 3606 * @return a copy of the original array, truncated or padded with nulls 3607 * to obtain the specified length 3608 * @throws NegativeArraySizeException if {@code newLength} is negative 3609 * @throws NullPointerException if {@code original} is null 3610 * @since 1.6 3611 */ 3612 @SuppressWarnings("unchecked") copyOf(T[] original, int newLength)3613 public static <T> T[] copyOf(T[] original, int newLength) { 3614 return (T[]) copyOf(original, newLength, original.getClass()); 3615 } 3616 3617 /** 3618 * Copies the specified array, truncating or padding with nulls (if necessary) 3619 * so the copy has the specified length. For all indices that are 3620 * valid in both the original array and the copy, the two arrays will 3621 * contain identical values. For any indices that are valid in the 3622 * copy but not the original, the copy will contain {@code null}. 3623 * Such indices will exist if and only if the specified length 3624 * is greater than that of the original array. 3625 * The resulting array is of the class {@code newType}. 3626 * 3627 * @param <U> the class of the objects in the original array 3628 * @param <T> the class of the objects in the returned array 3629 * @param original the array to be copied 3630 * @param newLength the length of the copy to be returned 3631 * @param newType the class of the copy to be returned 3632 * @return a copy of the original array, truncated or padded with nulls 3633 * to obtain the specified length 3634 * @throws NegativeArraySizeException if {@code newLength} is negative 3635 * @throws NullPointerException if {@code original} is null 3636 * @throws ArrayStoreException if an element copied from 3637 * {@code original} is not of a runtime type that can be stored in 3638 * an array of class {@code newType} 3639 * @since 1.6 3640 */ 3641 @HotSpotIntrinsicCandidate copyOf(U[] original, int newLength, Class<? extends T[]> newType)3642 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { 3643 @SuppressWarnings("unchecked") 3644 T[] copy = ((Object)newType == (Object)Object[].class) 3645 ? (T[]) new Object[newLength] 3646 : (T[]) Array.newInstance(newType.getComponentType(), newLength); 3647 System.arraycopy(original, 0, copy, 0, 3648 Math.min(original.length, newLength)); 3649 return copy; 3650 } 3651 3652 /** 3653 * Copies the specified array, truncating or padding with zeros (if necessary) 3654 * so the copy has the specified length. For all indices that are 3655 * valid in both the original array and the copy, the two arrays will 3656 * contain identical values. For any indices that are valid in the 3657 * copy but not the original, the copy will contain {@code (byte)0}. 3658 * Such indices will exist if and only if the specified length 3659 * is greater than that of the original array. 3660 * 3661 * @param original the array to be copied 3662 * @param newLength the length of the copy to be returned 3663 * @return a copy of the original array, truncated or padded with zeros 3664 * to obtain the specified length 3665 * @throws NegativeArraySizeException if {@code newLength} is negative 3666 * @throws NullPointerException if {@code original} is null 3667 * @since 1.6 3668 */ copyOf(byte[] original, int newLength)3669 public static byte[] copyOf(byte[] original, int newLength) { 3670 byte[] copy = new byte[newLength]; 3671 System.arraycopy(original, 0, copy, 0, 3672 Math.min(original.length, newLength)); 3673 return copy; 3674 } 3675 3676 /** 3677 * Copies the specified array, truncating or padding with zeros (if necessary) 3678 * so the copy has the specified length. For all indices that are 3679 * valid in both the original array and the copy, the two arrays will 3680 * contain identical values. For any indices that are valid in the 3681 * copy but not the original, the copy will contain {@code (short)0}. 3682 * Such indices will exist if and only if the specified length 3683 * is greater than that of the original array. 3684 * 3685 * @param original the array to be copied 3686 * @param newLength the length of the copy to be returned 3687 * @return a copy of the original array, truncated or padded with zeros 3688 * to obtain the specified length 3689 * @throws NegativeArraySizeException if {@code newLength} is negative 3690 * @throws NullPointerException if {@code original} is null 3691 * @since 1.6 3692 */ copyOf(short[] original, int newLength)3693 public static short[] copyOf(short[] original, int newLength) { 3694 short[] copy = new short[newLength]; 3695 System.arraycopy(original, 0, copy, 0, 3696 Math.min(original.length, newLength)); 3697 return copy; 3698 } 3699 3700 /** 3701 * Copies the specified array, truncating or padding with zeros (if necessary) 3702 * so the copy has the specified length. For all indices that are 3703 * valid in both the original array and the copy, the two arrays will 3704 * contain identical values. For any indices that are valid in the 3705 * copy but not the original, the copy will contain {@code 0}. 3706 * Such indices will exist if and only if the specified length 3707 * is greater than that of the original array. 3708 * 3709 * @param original the array to be copied 3710 * @param newLength the length of the copy to be returned 3711 * @return a copy of the original array, truncated or padded with zeros 3712 * to obtain the specified length 3713 * @throws NegativeArraySizeException if {@code newLength} is negative 3714 * @throws NullPointerException if {@code original} is null 3715 * @since 1.6 3716 */ copyOf(int[] original, int newLength)3717 public static int[] copyOf(int[] original, int newLength) { 3718 int[] copy = new int[newLength]; 3719 System.arraycopy(original, 0, copy, 0, 3720 Math.min(original.length, newLength)); 3721 return copy; 3722 } 3723 3724 /** 3725 * Copies the specified array, truncating or padding with zeros (if necessary) 3726 * so the copy has the specified length. For all indices that are 3727 * valid in both the original array and the copy, the two arrays will 3728 * contain identical values. For any indices that are valid in the 3729 * copy but not the original, the copy will contain {@code 0L}. 3730 * Such indices will exist if and only if the specified length 3731 * is greater than that of the original array. 3732 * 3733 * @param original the array to be copied 3734 * @param newLength the length of the copy to be returned 3735 * @return a copy of the original array, truncated or padded with zeros 3736 * to obtain the specified length 3737 * @throws NegativeArraySizeException if {@code newLength} is negative 3738 * @throws NullPointerException if {@code original} is null 3739 * @since 1.6 3740 */ copyOf(long[] original, int newLength)3741 public static long[] copyOf(long[] original, int newLength) { 3742 long[] copy = new long[newLength]; 3743 System.arraycopy(original, 0, copy, 0, 3744 Math.min(original.length, newLength)); 3745 return copy; 3746 } 3747 3748 /** 3749 * Copies the specified array, truncating or padding with null characters (if necessary) 3750 * so the copy has the specified length. For all indices that are valid 3751 * in both the original array and the copy, the two arrays will contain 3752 * identical values. For any indices that are valid in the copy but not 3753 * the original, the copy will contain {@code '\\u000'}. Such indices 3754 * will exist if and only if the specified length is greater than that of 3755 * the original array. 3756 * 3757 * @param original the array to be copied 3758 * @param newLength the length of the copy to be returned 3759 * @return a copy of the original array, truncated or padded with null characters 3760 * to obtain the specified length 3761 * @throws NegativeArraySizeException if {@code newLength} is negative 3762 * @throws NullPointerException if {@code original} is null 3763 * @since 1.6 3764 */ copyOf(char[] original, int newLength)3765 public static char[] copyOf(char[] original, int newLength) { 3766 char[] copy = new char[newLength]; 3767 System.arraycopy(original, 0, copy, 0, 3768 Math.min(original.length, newLength)); 3769 return copy; 3770 } 3771 3772 /** 3773 * Copies the specified array, truncating or padding with zeros (if necessary) 3774 * so the copy has the specified length. For all indices that are 3775 * valid in both the original array and the copy, the two arrays will 3776 * contain identical values. For any indices that are valid in the 3777 * copy but not the original, the copy will contain {@code 0f}. 3778 * Such indices will exist if and only if the specified length 3779 * is greater than that of the original array. 3780 * 3781 * @param original the array to be copied 3782 * @param newLength the length of the copy to be returned 3783 * @return a copy of the original array, truncated or padded with zeros 3784 * to obtain the specified length 3785 * @throws NegativeArraySizeException if {@code newLength} is negative 3786 * @throws NullPointerException if {@code original} is null 3787 * @since 1.6 3788 */ copyOf(float[] original, int newLength)3789 public static float[] copyOf(float[] original, int newLength) { 3790 float[] copy = new float[newLength]; 3791 System.arraycopy(original, 0, copy, 0, 3792 Math.min(original.length, newLength)); 3793 return copy; 3794 } 3795 3796 /** 3797 * Copies the specified array, truncating or padding with zeros (if necessary) 3798 * so the copy has the specified length. For all indices that are 3799 * valid in both the original array and the copy, the two arrays will 3800 * contain identical values. For any indices that are valid in the 3801 * copy but not the original, the copy will contain {@code 0d}. 3802 * Such indices will exist if and only if the specified length 3803 * is greater than that of the original array. 3804 * 3805 * @param original the array to be copied 3806 * @param newLength the length of the copy to be returned 3807 * @return a copy of the original array, truncated or padded with zeros 3808 * to obtain the specified length 3809 * @throws NegativeArraySizeException if {@code newLength} is negative 3810 * @throws NullPointerException if {@code original} is null 3811 * @since 1.6 3812 */ copyOf(double[] original, int newLength)3813 public static double[] copyOf(double[] original, int newLength) { 3814 double[] copy = new double[newLength]; 3815 System.arraycopy(original, 0, copy, 0, 3816 Math.min(original.length, newLength)); 3817 return copy; 3818 } 3819 3820 /** 3821 * Copies the specified array, truncating or padding with {@code false} (if necessary) 3822 * so the copy has the specified length. For all indices that are 3823 * valid in both the original array and the copy, the two arrays will 3824 * contain identical values. For any indices that are valid in the 3825 * copy but not the original, the copy will contain {@code false}. 3826 * Such indices will exist if and only if the specified length 3827 * is greater than that of the original array. 3828 * 3829 * @param original the array to be copied 3830 * @param newLength the length of the copy to be returned 3831 * @return a copy of the original array, truncated or padded with false elements 3832 * to obtain the specified length 3833 * @throws NegativeArraySizeException if {@code newLength} is negative 3834 * @throws NullPointerException if {@code original} is null 3835 * @since 1.6 3836 */ copyOf(boolean[] original, int newLength)3837 public static boolean[] copyOf(boolean[] original, int newLength) { 3838 boolean[] copy = new boolean[newLength]; 3839 System.arraycopy(original, 0, copy, 0, 3840 Math.min(original.length, newLength)); 3841 return copy; 3842 } 3843 3844 /** 3845 * Copies the specified range of the specified array into a new array. 3846 * The initial index of the range ({@code from}) must lie between zero 3847 * and {@code original.length}, inclusive. The value at 3848 * {@code original[from]} is placed into the initial element of the copy 3849 * (unless {@code from == original.length} or {@code from == to}). 3850 * Values from subsequent elements in the original array are placed into 3851 * subsequent elements in the copy. The final index of the range 3852 * ({@code to}), which must be greater than or equal to {@code from}, 3853 * may be greater than {@code original.length}, in which case 3854 * {@code null} is placed in all elements of the copy whose index is 3855 * greater than or equal to {@code original.length - from}. The length 3856 * of the returned array will be {@code to - from}. 3857 * <p> 3858 * The resulting array is of exactly the same class as the original array. 3859 * 3860 * @param <T> the class of the objects in the array 3861 * @param original the array from which a range is to be copied 3862 * @param from the initial index of the range to be copied, inclusive 3863 * @param to the final index of the range to be copied, exclusive. 3864 * (This index may lie outside the array.) 3865 * @return a new array containing the specified range from the original array, 3866 * truncated or padded with nulls to obtain the required length 3867 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3868 * or {@code from > original.length} 3869 * @throws IllegalArgumentException if {@code from > to} 3870 * @throws NullPointerException if {@code original} is null 3871 * @since 1.6 3872 */ 3873 @SuppressWarnings("unchecked") copyOfRange(T[] original, int from, int to)3874 public static <T> T[] copyOfRange(T[] original, int from, int to) { 3875 return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass()); 3876 } 3877 3878 /** 3879 * Copies the specified range of the specified array into a new array. 3880 * The initial index of the range ({@code from}) must lie between zero 3881 * and {@code original.length}, inclusive. The value at 3882 * {@code original[from]} is placed into the initial element of the copy 3883 * (unless {@code from == original.length} or {@code from == to}). 3884 * Values from subsequent elements in the original array are placed into 3885 * subsequent elements in the copy. The final index of the range 3886 * ({@code to}), which must be greater than or equal to {@code from}, 3887 * may be greater than {@code original.length}, in which case 3888 * {@code null} is placed in all elements of the copy whose index is 3889 * greater than or equal to {@code original.length - from}. The length 3890 * of the returned array will be {@code to - from}. 3891 * The resulting array is of the class {@code newType}. 3892 * 3893 * @param <U> the class of the objects in the original array 3894 * @param <T> the class of the objects in the returned array 3895 * @param original the array from which a range is to be copied 3896 * @param from the initial index of the range to be copied, inclusive 3897 * @param to the final index of the range to be copied, exclusive. 3898 * (This index may lie outside the array.) 3899 * @param newType the class of the copy to be returned 3900 * @return a new array containing the specified range from the original array, 3901 * truncated or padded with nulls to obtain the required length 3902 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3903 * or {@code from > original.length} 3904 * @throws IllegalArgumentException if {@code from > to} 3905 * @throws NullPointerException if {@code original} is null 3906 * @throws ArrayStoreException if an element copied from 3907 * {@code original} is not of a runtime type that can be stored in 3908 * an array of class {@code newType}. 3909 * @since 1.6 3910 */ 3911 @HotSpotIntrinsicCandidate copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)3912 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { 3913 int newLength = to - from; 3914 if (newLength < 0) 3915 throw new IllegalArgumentException(from + " > " + to); 3916 @SuppressWarnings("unchecked") 3917 T[] copy = ((Object)newType == (Object)Object[].class) 3918 ? (T[]) new Object[newLength] 3919 : (T[]) Array.newInstance(newType.getComponentType(), newLength); 3920 System.arraycopy(original, from, copy, 0, 3921 Math.min(original.length - from, newLength)); 3922 return copy; 3923 } 3924 3925 /** 3926 * Copies the specified range of the specified array into a new array. 3927 * The initial index of the range ({@code from}) must lie between zero 3928 * and {@code original.length}, inclusive. The value at 3929 * {@code original[from]} is placed into the initial element of the copy 3930 * (unless {@code from == original.length} or {@code from == to}). 3931 * Values from subsequent elements in the original array are placed into 3932 * subsequent elements in the copy. The final index of the range 3933 * ({@code to}), which must be greater than or equal to {@code from}, 3934 * may be greater than {@code original.length}, in which case 3935 * {@code (byte)0} is placed in all elements of the copy whose index is 3936 * greater than or equal to {@code original.length - from}. The length 3937 * of the returned array will be {@code to - from}. 3938 * 3939 * @param original the array from which a range is to be copied 3940 * @param from the initial index of the range to be copied, inclusive 3941 * @param to the final index of the range to be copied, exclusive. 3942 * (This index may lie outside the array.) 3943 * @return a new array containing the specified range from the original array, 3944 * truncated or padded with zeros to obtain the required length 3945 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3946 * or {@code from > original.length} 3947 * @throws IllegalArgumentException if {@code from > to} 3948 * @throws NullPointerException if {@code original} is null 3949 * @since 1.6 3950 */ copyOfRange(byte[] original, int from, int to)3951 public static byte[] copyOfRange(byte[] original, int from, int to) { 3952 int newLength = to - from; 3953 if (newLength < 0) 3954 throw new IllegalArgumentException(from + " > " + to); 3955 byte[] copy = new byte[newLength]; 3956 System.arraycopy(original, from, copy, 0, 3957 Math.min(original.length - from, newLength)); 3958 return copy; 3959 } 3960 3961 /** 3962 * Copies the specified range of the specified array into a new array. 3963 * The initial index of the range ({@code from}) must lie between zero 3964 * and {@code original.length}, inclusive. The value at 3965 * {@code original[from]} is placed into the initial element of the copy 3966 * (unless {@code from == original.length} or {@code from == to}). 3967 * Values from subsequent elements in the original array are placed into 3968 * subsequent elements in the copy. The final index of the range 3969 * ({@code to}), which must be greater than or equal to {@code from}, 3970 * may be greater than {@code original.length}, in which case 3971 * {@code (short)0} is placed in all elements of the copy whose index is 3972 * greater than or equal to {@code original.length - from}. The length 3973 * of the returned array will be {@code to - from}. 3974 * 3975 * @param original the array from which a range is to be copied 3976 * @param from the initial index of the range to be copied, inclusive 3977 * @param to the final index of the range to be copied, exclusive. 3978 * (This index may lie outside the array.) 3979 * @return a new array containing the specified range from the original array, 3980 * truncated or padded with zeros to obtain the required length 3981 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3982 * or {@code from > original.length} 3983 * @throws IllegalArgumentException if {@code from > to} 3984 * @throws NullPointerException if {@code original} is null 3985 * @since 1.6 3986 */ copyOfRange(short[] original, int from, int to)3987 public static short[] copyOfRange(short[] original, int from, int to) { 3988 int newLength = to - from; 3989 if (newLength < 0) 3990 throw new IllegalArgumentException(from + " > " + to); 3991 short[] copy = new short[newLength]; 3992 System.arraycopy(original, from, copy, 0, 3993 Math.min(original.length - from, newLength)); 3994 return copy; 3995 } 3996 3997 /** 3998 * Copies the specified range of the specified array into a new array. 3999 * The initial index of the range ({@code from}) must lie between zero 4000 * and {@code original.length}, inclusive. The value at 4001 * {@code original[from]} is placed into the initial element of the copy 4002 * (unless {@code from == original.length} or {@code from == to}). 4003 * Values from subsequent elements in the original array are placed into 4004 * subsequent elements in the copy. The final index of the range 4005 * ({@code to}), which must be greater than or equal to {@code from}, 4006 * may be greater than {@code original.length}, in which case 4007 * {@code 0} is placed in all elements of the copy whose index is 4008 * greater than or equal to {@code original.length - from}. The length 4009 * of the returned array will be {@code to - from}. 4010 * 4011 * @param original the array from which a range is to be copied 4012 * @param from the initial index of the range to be copied, inclusive 4013 * @param to the final index of the range to be copied, exclusive. 4014 * (This index may lie outside the array.) 4015 * @return a new array containing the specified range from the original array, 4016 * truncated or padded with zeros to obtain the required length 4017 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4018 * or {@code from > original.length} 4019 * @throws IllegalArgumentException if {@code from > to} 4020 * @throws NullPointerException if {@code original} is null 4021 * @since 1.6 4022 */ copyOfRange(int[] original, int from, int to)4023 public static int[] copyOfRange(int[] original, int from, int to) { 4024 int newLength = to - from; 4025 if (newLength < 0) 4026 throw new IllegalArgumentException(from + " > " + to); 4027 int[] copy = new int[newLength]; 4028 System.arraycopy(original, from, copy, 0, 4029 Math.min(original.length - from, newLength)); 4030 return copy; 4031 } 4032 4033 /** 4034 * Copies the specified range of the specified array into a new array. 4035 * The initial index of the range ({@code from}) must lie between zero 4036 * and {@code original.length}, inclusive. The value at 4037 * {@code original[from]} is placed into the initial element of the copy 4038 * (unless {@code from == original.length} or {@code from == to}). 4039 * Values from subsequent elements in the original array are placed into 4040 * subsequent elements in the copy. The final index of the range 4041 * ({@code to}), which must be greater than or equal to {@code from}, 4042 * may be greater than {@code original.length}, in which case 4043 * {@code 0L} is placed in all elements of the copy whose index is 4044 * greater than or equal to {@code original.length - from}. The length 4045 * of the returned array will be {@code to - from}. 4046 * 4047 * @param original the array from which a range is to be copied 4048 * @param from the initial index of the range to be copied, inclusive 4049 * @param to the final index of the range to be copied, exclusive. 4050 * (This index may lie outside the array.) 4051 * @return a new array containing the specified range from the original array, 4052 * truncated or padded with zeros to obtain the required length 4053 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4054 * or {@code from > original.length} 4055 * @throws IllegalArgumentException if {@code from > to} 4056 * @throws NullPointerException if {@code original} is null 4057 * @since 1.6 4058 */ copyOfRange(long[] original, int from, int to)4059 public static long[] copyOfRange(long[] original, int from, int to) { 4060 int newLength = to - from; 4061 if (newLength < 0) 4062 throw new IllegalArgumentException(from + " > " + to); 4063 long[] copy = new long[newLength]; 4064 System.arraycopy(original, from, copy, 0, 4065 Math.min(original.length - from, newLength)); 4066 return copy; 4067 } 4068 4069 /** 4070 * Copies the specified range of the specified array into a new array. 4071 * The initial index of the range ({@code from}) must lie between zero 4072 * and {@code original.length}, inclusive. The value at 4073 * {@code original[from]} is placed into the initial element of the copy 4074 * (unless {@code from == original.length} or {@code from == to}). 4075 * Values from subsequent elements in the original array are placed into 4076 * subsequent elements in the copy. The final index of the range 4077 * ({@code to}), which must be greater than or equal to {@code from}, 4078 * may be greater than {@code original.length}, in which case 4079 * {@code '\\u000'} is placed in all elements of the copy whose index is 4080 * greater than or equal to {@code original.length - from}. The length 4081 * of the returned array will be {@code to - from}. 4082 * 4083 * @param original the array from which a range is to be copied 4084 * @param from the initial index of the range to be copied, inclusive 4085 * @param to the final index of the range to be copied, exclusive. 4086 * (This index may lie outside the array.) 4087 * @return a new array containing the specified range from the original array, 4088 * truncated or padded with null characters to obtain the required length 4089 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4090 * or {@code from > original.length} 4091 * @throws IllegalArgumentException if {@code from > to} 4092 * @throws NullPointerException if {@code original} is null 4093 * @since 1.6 4094 */ copyOfRange(char[] original, int from, int to)4095 public static char[] copyOfRange(char[] original, int from, int to) { 4096 int newLength = to - from; 4097 if (newLength < 0) 4098 throw new IllegalArgumentException(from + " > " + to); 4099 char[] copy = new char[newLength]; 4100 System.arraycopy(original, from, copy, 0, 4101 Math.min(original.length - from, newLength)); 4102 return copy; 4103 } 4104 4105 /** 4106 * Copies the specified range of the specified array into a new array. 4107 * The initial index of the range ({@code from}) must lie between zero 4108 * and {@code original.length}, inclusive. The value at 4109 * {@code original[from]} is placed into the initial element of the copy 4110 * (unless {@code from == original.length} or {@code from == to}). 4111 * Values from subsequent elements in the original array are placed into 4112 * subsequent elements in the copy. The final index of the range 4113 * ({@code to}), which must be greater than or equal to {@code from}, 4114 * may be greater than {@code original.length}, in which case 4115 * {@code 0f} is placed in all elements of the copy whose index is 4116 * greater than or equal to {@code original.length - from}. The length 4117 * of the returned array will be {@code to - from}. 4118 * 4119 * @param original the array from which a range is to be copied 4120 * @param from the initial index of the range to be copied, inclusive 4121 * @param to the final index of the range to be copied, exclusive. 4122 * (This index may lie outside the array.) 4123 * @return a new array containing the specified range from the original array, 4124 * truncated or padded with zeros to obtain the required length 4125 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4126 * or {@code from > original.length} 4127 * @throws IllegalArgumentException if {@code from > to} 4128 * @throws NullPointerException if {@code original} is null 4129 * @since 1.6 4130 */ copyOfRange(float[] original, int from, int to)4131 public static float[] copyOfRange(float[] original, int from, int to) { 4132 int newLength = to - from; 4133 if (newLength < 0) 4134 throw new IllegalArgumentException(from + " > " + to); 4135 float[] copy = new float[newLength]; 4136 System.arraycopy(original, from, copy, 0, 4137 Math.min(original.length - from, newLength)); 4138 return copy; 4139 } 4140 4141 /** 4142 * Copies the specified range of the specified array into a new array. 4143 * The initial index of the range ({@code from}) must lie between zero 4144 * and {@code original.length}, inclusive. The value at 4145 * {@code original[from]} is placed into the initial element of the copy 4146 * (unless {@code from == original.length} or {@code from == to}). 4147 * Values from subsequent elements in the original array are placed into 4148 * subsequent elements in the copy. The final index of the range 4149 * ({@code to}), which must be greater than or equal to {@code from}, 4150 * may be greater than {@code original.length}, in which case 4151 * {@code 0d} is placed in all elements of the copy whose index is 4152 * greater than or equal to {@code original.length - from}. The length 4153 * of the returned array will be {@code to - from}. 4154 * 4155 * @param original the array from which a range is to be copied 4156 * @param from the initial index of the range to be copied, inclusive 4157 * @param to the final index of the range to be copied, exclusive. 4158 * (This index may lie outside the array.) 4159 * @return a new array containing the specified range from the original array, 4160 * truncated or padded with zeros to obtain the required length 4161 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4162 * or {@code from > original.length} 4163 * @throws IllegalArgumentException if {@code from > to} 4164 * @throws NullPointerException if {@code original} is null 4165 * @since 1.6 4166 */ copyOfRange(double[] original, int from, int to)4167 public static double[] copyOfRange(double[] original, int from, int to) { 4168 int newLength = to - from; 4169 if (newLength < 0) 4170 throw new IllegalArgumentException(from + " > " + to); 4171 double[] copy = new double[newLength]; 4172 System.arraycopy(original, from, copy, 0, 4173 Math.min(original.length - from, newLength)); 4174 return copy; 4175 } 4176 4177 /** 4178 * Copies the specified range of the specified array into a new array. 4179 * The initial index of the range ({@code from}) must lie between zero 4180 * and {@code original.length}, inclusive. The value at 4181 * {@code original[from]} is placed into the initial element of the copy 4182 * (unless {@code from == original.length} or {@code from == to}). 4183 * Values from subsequent elements in the original array are placed into 4184 * subsequent elements in the copy. The final index of the range 4185 * ({@code to}), which must be greater than or equal to {@code from}, 4186 * may be greater than {@code original.length}, in which case 4187 * {@code false} is placed in all elements of the copy whose index is 4188 * greater than or equal to {@code original.length - from}. The length 4189 * of the returned array will be {@code to - from}. 4190 * 4191 * @param original the array from which a range is to be copied 4192 * @param from the initial index of the range to be copied, inclusive 4193 * @param to the final index of the range to be copied, exclusive. 4194 * (This index may lie outside the array.) 4195 * @return a new array containing the specified range from the original array, 4196 * truncated or padded with false elements to obtain the required length 4197 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 4198 * or {@code from > original.length} 4199 * @throws IllegalArgumentException if {@code from > to} 4200 * @throws NullPointerException if {@code original} is null 4201 * @since 1.6 4202 */ copyOfRange(boolean[] original, int from, int to)4203 public static boolean[] copyOfRange(boolean[] original, int from, int to) { 4204 int newLength = to - from; 4205 if (newLength < 0) 4206 throw new IllegalArgumentException(from + " > " + to); 4207 boolean[] copy = new boolean[newLength]; 4208 System.arraycopy(original, from, copy, 0, 4209 Math.min(original.length - from, newLength)); 4210 return copy; 4211 } 4212 4213 // Misc 4214 4215 /** 4216 * Returns a fixed-size list backed by the specified array. (Changes to 4217 * the returned list "write through" to the array.) This method acts 4218 * as bridge between array-based and collection-based APIs, in 4219 * combination with {@link Collection#toArray}. The returned list is 4220 * serializable and implements {@link RandomAccess}. 4221 * 4222 * <p>This method also provides a convenient way to create a fixed-size 4223 * list initialized to contain several elements: 4224 * <pre> 4225 * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 4226 * </pre> 4227 * 4228 * @param <T> the class of the objects in the array 4229 * @param a the array by which the list will be backed 4230 * @return a list view of the specified array 4231 */ 4232 @SafeVarargs 4233 @SuppressWarnings("varargs") asList(T... a)4234 public static <T> List<T> asList(T... a) { 4235 return new ArrayList<>(a); 4236 } 4237 4238 /** 4239 * @serial include 4240 */ 4241 private static class ArrayList<E> extends AbstractList<E> 4242 implements RandomAccess, java.io.Serializable 4243 { 4244 private static final long serialVersionUID = -2764017481108945198L; 4245 private final E[] a; 4246 ArrayList(E[] array)4247 ArrayList(E[] array) { 4248 a = Objects.requireNonNull(array); 4249 } 4250 4251 @Override size()4252 public int size() { 4253 return a.length; 4254 } 4255 4256 @Override toArray()4257 public Object[] toArray() { 4258 // Android-changed: there are applications which expect this method 4259 // to return array with component type E, not just Object. 4260 // Keeping pre-Java 9 behaviour for compatibility sake. 4261 // See b/204397945. 4262 // return Arrays.copyOf(a, a.length, Object[].class); 4263 return a.clone(); 4264 } 4265 4266 @Override 4267 @SuppressWarnings("unchecked") toArray(T[] a)4268 public <T> T[] toArray(T[] a) { 4269 int size = size(); 4270 if (a.length < size) 4271 return Arrays.copyOf(this.a, size, 4272 (Class<? extends T[]>) a.getClass()); 4273 System.arraycopy(this.a, 0, a, 0, size); 4274 if (a.length > size) 4275 a[size] = null; 4276 return a; 4277 } 4278 4279 @Override get(int index)4280 public E get(int index) { 4281 return a[index]; 4282 } 4283 4284 @Override set(int index, E element)4285 public E set(int index, E element) { 4286 E oldValue = a[index]; 4287 a[index] = element; 4288 return oldValue; 4289 } 4290 4291 @Override indexOf(Object o)4292 public int indexOf(Object o) { 4293 E[] a = this.a; 4294 if (o == null) { 4295 for (int i = 0; i < a.length; i++) 4296 if (a[i] == null) 4297 return i; 4298 } else { 4299 for (int i = 0; i < a.length; i++) 4300 if (o.equals(a[i])) 4301 return i; 4302 } 4303 return -1; 4304 } 4305 4306 @Override contains(Object o)4307 public boolean contains(Object o) { 4308 return indexOf(o) >= 0; 4309 } 4310 4311 @Override spliterator()4312 public Spliterator<E> spliterator() { 4313 return Spliterators.spliterator(a, Spliterator.ORDERED); 4314 } 4315 4316 @Override forEach(Consumer<? super E> action)4317 public void forEach(Consumer<? super E> action) { 4318 Objects.requireNonNull(action); 4319 for (E e : a) { 4320 action.accept(e); 4321 } 4322 } 4323 4324 @Override replaceAll(UnaryOperator<E> operator)4325 public void replaceAll(UnaryOperator<E> operator) { 4326 Objects.requireNonNull(operator); 4327 E[] a = this.a; 4328 for (int i = 0; i < a.length; i++) { 4329 a[i] = operator.apply(a[i]); 4330 } 4331 } 4332 4333 @Override sort(Comparator<? super E> c)4334 public void sort(Comparator<? super E> c) { 4335 Arrays.sort(a, c); 4336 } 4337 4338 @Override iterator()4339 public Iterator<E> iterator() { 4340 return new ArrayItr<>(a); 4341 } 4342 } 4343 4344 private static class ArrayItr<E> implements Iterator<E> { 4345 private int cursor; 4346 private final E[] a; 4347 ArrayItr(E[] a)4348 ArrayItr(E[] a) { 4349 this.a = a; 4350 } 4351 4352 @Override hasNext()4353 public boolean hasNext() { 4354 return cursor < a.length; 4355 } 4356 4357 @Override next()4358 public E next() { 4359 int i = cursor; 4360 if (i >= a.length) { 4361 throw new NoSuchElementException(); 4362 } 4363 cursor = i + 1; 4364 return a[i]; 4365 } 4366 } 4367 4368 /** 4369 * Returns a hash code based on the contents of the specified array. 4370 * For any two {@code long} arrays {@code a} and {@code b} 4371 * such that {@code Arrays.equals(a, b)}, it is also the case that 4372 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4373 * 4374 * <p>The value returned by this method is the same value that would be 4375 * obtained by invoking the {@link List#hashCode() hashCode} 4376 * method on a {@link List} containing a sequence of {@link Long} 4377 * instances representing the elements of {@code a} in the same order. 4378 * If {@code a} is {@code null}, this method returns 0. 4379 * 4380 * @param a the array whose hash value to compute 4381 * @return a content-based hash code for {@code a} 4382 * @since 1.5 4383 */ hashCode(long a[])4384 public static int hashCode(long a[]) { 4385 if (a == null) 4386 return 0; 4387 4388 int result = 1; 4389 for (long element : a) { 4390 int elementHash = (int)(element ^ (element >>> 32)); 4391 result = 31 * result + elementHash; 4392 } 4393 4394 return result; 4395 } 4396 4397 /** 4398 * Returns a hash code based on the contents of the specified array. 4399 * For any two non-null {@code int} arrays {@code a} and {@code b} 4400 * such that {@code Arrays.equals(a, b)}, it is also the case that 4401 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4402 * 4403 * <p>The value returned by this method is the same value that would be 4404 * obtained by invoking the {@link List#hashCode() hashCode} 4405 * method on a {@link List} containing a sequence of {@link Integer} 4406 * instances representing the elements of {@code a} in the same order. 4407 * If {@code a} is {@code null}, this method returns 0. 4408 * 4409 * @param a the array whose hash value to compute 4410 * @return a content-based hash code for {@code a} 4411 * @since 1.5 4412 */ hashCode(int a[])4413 public static int hashCode(int a[]) { 4414 if (a == null) 4415 return 0; 4416 4417 int result = 1; 4418 for (int element : a) 4419 result = 31 * result + element; 4420 4421 return result; 4422 } 4423 4424 /** 4425 * Returns a hash code based on the contents of the specified array. 4426 * For any two {@code short} arrays {@code a} and {@code b} 4427 * such that {@code Arrays.equals(a, b)}, it is also the case that 4428 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4429 * 4430 * <p>The value returned by this method is the same value that would be 4431 * obtained by invoking the {@link List#hashCode() hashCode} 4432 * method on a {@link List} containing a sequence of {@link Short} 4433 * instances representing the elements of {@code a} in the same order. 4434 * If {@code a} is {@code null}, this method returns 0. 4435 * 4436 * @param a the array whose hash value to compute 4437 * @return a content-based hash code for {@code a} 4438 * @since 1.5 4439 */ hashCode(short a[])4440 public static int hashCode(short a[]) { 4441 if (a == null) 4442 return 0; 4443 4444 int result = 1; 4445 for (short element : a) 4446 result = 31 * result + element; 4447 4448 return result; 4449 } 4450 4451 /** 4452 * Returns a hash code based on the contents of the specified array. 4453 * For any two {@code char} arrays {@code a} and {@code b} 4454 * such that {@code Arrays.equals(a, b)}, it is also the case that 4455 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4456 * 4457 * <p>The value returned by this method is the same value that would be 4458 * obtained by invoking the {@link List#hashCode() hashCode} 4459 * method on a {@link List} containing a sequence of {@link Character} 4460 * instances representing the elements of {@code a} in the same order. 4461 * If {@code a} is {@code null}, this method returns 0. 4462 * 4463 * @param a the array whose hash value to compute 4464 * @return a content-based hash code for {@code a} 4465 * @since 1.5 4466 */ hashCode(char a[])4467 public static int hashCode(char a[]) { 4468 if (a == null) 4469 return 0; 4470 4471 int result = 1; 4472 for (char element : a) 4473 result = 31 * result + element; 4474 4475 return result; 4476 } 4477 4478 /** 4479 * Returns a hash code based on the contents of the specified array. 4480 * For any two {@code byte} arrays {@code a} and {@code b} 4481 * such that {@code Arrays.equals(a, b)}, it is also the case that 4482 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4483 * 4484 * <p>The value returned by this method is the same value that would be 4485 * obtained by invoking the {@link List#hashCode() hashCode} 4486 * method on a {@link List} containing a sequence of {@link Byte} 4487 * instances representing the elements of {@code a} in the same order. 4488 * If {@code a} is {@code null}, this method returns 0. 4489 * 4490 * @param a the array whose hash value to compute 4491 * @return a content-based hash code for {@code a} 4492 * @since 1.5 4493 */ hashCode(byte a[])4494 public static int hashCode(byte a[]) { 4495 if (a == null) 4496 return 0; 4497 4498 int result = 1; 4499 for (byte element : a) 4500 result = 31 * result + element; 4501 4502 return result; 4503 } 4504 4505 /** 4506 * Returns a hash code based on the contents of the specified array. 4507 * For any two {@code boolean} arrays {@code a} and {@code b} 4508 * such that {@code Arrays.equals(a, b)}, it is also the case that 4509 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4510 * 4511 * <p>The value returned by this method is the same value that would be 4512 * obtained by invoking the {@link List#hashCode() hashCode} 4513 * method on a {@link List} containing a sequence of {@link Boolean} 4514 * instances representing the elements of {@code a} in the same order. 4515 * If {@code a} is {@code null}, this method returns 0. 4516 * 4517 * @param a the array whose hash value to compute 4518 * @return a content-based hash code for {@code a} 4519 * @since 1.5 4520 */ hashCode(boolean a[])4521 public static int hashCode(boolean a[]) { 4522 if (a == null) 4523 return 0; 4524 4525 int result = 1; 4526 for (boolean element : a) 4527 result = 31 * result + (element ? 1231 : 1237); 4528 4529 return result; 4530 } 4531 4532 /** 4533 * Returns a hash code based on the contents of the specified array. 4534 * For any two {@code float} arrays {@code a} and {@code b} 4535 * such that {@code Arrays.equals(a, b)}, it is also the case that 4536 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4537 * 4538 * <p>The value returned by this method is the same value that would be 4539 * obtained by invoking the {@link List#hashCode() hashCode} 4540 * method on a {@link List} containing a sequence of {@link Float} 4541 * instances representing the elements of {@code a} in the same order. 4542 * If {@code a} is {@code null}, this method returns 0. 4543 * 4544 * @param a the array whose hash value to compute 4545 * @return a content-based hash code for {@code a} 4546 * @since 1.5 4547 */ hashCode(float a[])4548 public static int hashCode(float a[]) { 4549 if (a == null) 4550 return 0; 4551 4552 int result = 1; 4553 for (float element : a) 4554 result = 31 * result + Float.floatToIntBits(element); 4555 4556 return result; 4557 } 4558 4559 /** 4560 * Returns a hash code based on the contents of the specified array. 4561 * For any two {@code double} arrays {@code a} and {@code b} 4562 * such that {@code Arrays.equals(a, b)}, it is also the case that 4563 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4564 * 4565 * <p>The value returned by this method is the same value that would be 4566 * obtained by invoking the {@link List#hashCode() hashCode} 4567 * method on a {@link List} containing a sequence of {@link Double} 4568 * instances representing the elements of {@code a} in the same order. 4569 * If {@code a} is {@code null}, this method returns 0. 4570 * 4571 * @param a the array whose hash value to compute 4572 * @return a content-based hash code for {@code a} 4573 * @since 1.5 4574 */ hashCode(double a[])4575 public static int hashCode(double a[]) { 4576 if (a == null) 4577 return 0; 4578 4579 int result = 1; 4580 for (double element : a) { 4581 long bits = Double.doubleToLongBits(element); 4582 result = 31 * result + (int)(bits ^ (bits >>> 32)); 4583 } 4584 return result; 4585 } 4586 4587 /** 4588 * Returns a hash code based on the contents of the specified array. If 4589 * the array contains other arrays as elements, the hash code is based on 4590 * their identities rather than their contents. It is therefore 4591 * acceptable to invoke this method on an array that contains itself as an 4592 * element, either directly or indirectly through one or more levels of 4593 * arrays. 4594 * 4595 * <p>For any two arrays {@code a} and {@code b} such that 4596 * {@code Arrays.equals(a, b)}, it is also the case that 4597 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}. 4598 * 4599 * <p>The value returned by this method is equal to the value that would 4600 * be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a} 4601 * is {@code null}, in which case {@code 0} is returned. 4602 * 4603 * @param a the array whose content-based hash code to compute 4604 * @return a content-based hash code for {@code a} 4605 * @see #deepHashCode(Object[]) 4606 * @since 1.5 4607 */ hashCode(Object a[])4608 public static int hashCode(Object a[]) { 4609 if (a == null) 4610 return 0; 4611 4612 int result = 1; 4613 4614 for (Object element : a) 4615 result = 31 * result + (element == null ? 0 : element.hashCode()); 4616 4617 return result; 4618 } 4619 4620 /** 4621 * Returns a hash code based on the "deep contents" of the specified 4622 * array. If the array contains other arrays as elements, the 4623 * hash code is based on their contents and so on, ad infinitum. 4624 * It is therefore unacceptable to invoke this method on an array that 4625 * contains itself as an element, either directly or indirectly through 4626 * one or more levels of arrays. The behavior of such an invocation is 4627 * undefined. 4628 * 4629 * <p>For any two arrays {@code a} and {@code b} such that 4630 * {@code Arrays.deepEquals(a, b)}, it is also the case that 4631 * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}. 4632 * 4633 * <p>The computation of the value returned by this method is similar to 4634 * that of the value returned by {@link List#hashCode()} on a list 4635 * containing the same elements as {@code a} in the same order, with one 4636 * difference: If an element {@code e} of {@code a} is itself an array, 4637 * its hash code is computed not by calling {@code e.hashCode()}, but as 4638 * by calling the appropriate overloading of {@code Arrays.hashCode(e)} 4639 * if {@code e} is an array of a primitive type, or as by calling 4640 * {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array 4641 * of a reference type. If {@code a} is {@code null}, this method 4642 * returns 0. 4643 * 4644 * @param a the array whose deep-content-based hash code to compute 4645 * @return a deep-content-based hash code for {@code a} 4646 * @see #hashCode(Object[]) 4647 * @since 1.5 4648 */ deepHashCode(Object a[])4649 public static int deepHashCode(Object a[]) { 4650 if (a == null) 4651 return 0; 4652 4653 int result = 1; 4654 4655 for (Object element : a) { 4656 final int elementHash; 4657 final Class<?> cl; 4658 if (element == null) 4659 elementHash = 0; 4660 else if ((cl = element.getClass().getComponentType()) == null) 4661 elementHash = element.hashCode(); 4662 else if (element instanceof Object[]) 4663 elementHash = deepHashCode((Object[]) element); 4664 else 4665 elementHash = primitiveArrayHashCode(element, cl); 4666 4667 result = 31 * result + elementHash; 4668 } 4669 4670 return result; 4671 } 4672 primitiveArrayHashCode(Object a, Class<?> cl)4673 private static int primitiveArrayHashCode(Object a, Class<?> cl) { 4674 return 4675 (cl == byte.class) ? hashCode((byte[]) a) : 4676 (cl == int.class) ? hashCode((int[]) a) : 4677 (cl == long.class) ? hashCode((long[]) a) : 4678 (cl == char.class) ? hashCode((char[]) a) : 4679 (cl == short.class) ? hashCode((short[]) a) : 4680 (cl == boolean.class) ? hashCode((boolean[]) a) : 4681 (cl == double.class) ? hashCode((double[]) a) : 4682 // If new primitive types are ever added, this method must be 4683 // expanded or we will fail here with ClassCastException. 4684 hashCode((float[]) a); 4685 } 4686 4687 /** 4688 * Returns {@code true} if the two specified arrays are <i>deeply 4689 * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} 4690 * method, this method is appropriate for use with nested arrays of 4691 * arbitrary depth. 4692 * 4693 * <p>Two array references are considered deeply equal if both 4694 * are {@code null}, or if they refer to arrays that contain the same 4695 * number of elements and all corresponding pairs of elements in the two 4696 * arrays are deeply equal. 4697 * 4698 * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are 4699 * deeply equal if any of the following conditions hold: 4700 * <ul> 4701 * <li> {@code e1} and {@code e2} are both arrays of object reference 4702 * types, and {@code Arrays.deepEquals(e1, e2) would return true} 4703 * <li> {@code e1} and {@code e2} are arrays of the same primitive 4704 * type, and the appropriate overloading of 4705 * {@code Arrays.equals(e1, e2)} would return true. 4706 * <li> {@code e1 == e2} 4707 * <li> {@code e1.equals(e2)} would return true. 4708 * </ul> 4709 * Note that this definition permits {@code null} elements at any depth. 4710 * 4711 * <p>If either of the specified arrays contain themselves as elements 4712 * either directly or indirectly through one or more levels of arrays, 4713 * the behavior of this method is undefined. 4714 * 4715 * @param a1 one array to be tested for equality 4716 * @param a2 the other array to be tested for equality 4717 * @return {@code true} if the two arrays are equal 4718 * @see #equals(Object[],Object[]) 4719 * @see Objects#deepEquals(Object, Object) 4720 * @since 1.5 4721 */ deepEquals(Object[] a1, Object[] a2)4722 public static boolean deepEquals(Object[] a1, Object[] a2) { 4723 if (a1 == a2) 4724 return true; 4725 if (a1 == null || a2==null) 4726 return false; 4727 int length = a1.length; 4728 if (a2.length != length) 4729 return false; 4730 4731 for (int i = 0; i < length; i++) { 4732 Object e1 = a1[i]; 4733 Object e2 = a2[i]; 4734 4735 if (e1 == e2) 4736 continue; 4737 if (e1 == null) 4738 return false; 4739 4740 // Figure out whether the two elements are equal 4741 boolean eq = deepEquals0(e1, e2); 4742 4743 if (!eq) 4744 return false; 4745 } 4746 return true; 4747 } 4748 deepEquals0(Object e1, Object e2)4749 static boolean deepEquals0(Object e1, Object e2) { 4750 assert e1 != null; 4751 boolean eq; 4752 if (e1 instanceof Object[] && e2 instanceof Object[]) 4753 eq = deepEquals ((Object[]) e1, (Object[]) e2); 4754 else if (e1 instanceof byte[] && e2 instanceof byte[]) 4755 eq = equals((byte[]) e1, (byte[]) e2); 4756 else if (e1 instanceof short[] && e2 instanceof short[]) 4757 eq = equals((short[]) e1, (short[]) e2); 4758 else if (e1 instanceof int[] && e2 instanceof int[]) 4759 eq = equals((int[]) e1, (int[]) e2); 4760 else if (e1 instanceof long[] && e2 instanceof long[]) 4761 eq = equals((long[]) e1, (long[]) e2); 4762 else if (e1 instanceof char[] && e2 instanceof char[]) 4763 eq = equals((char[]) e1, (char[]) e2); 4764 else if (e1 instanceof float[] && e2 instanceof float[]) 4765 eq = equals((float[]) e1, (float[]) e2); 4766 else if (e1 instanceof double[] && e2 instanceof double[]) 4767 eq = equals((double[]) e1, (double[]) e2); 4768 else if (e1 instanceof boolean[] && e2 instanceof boolean[]) 4769 eq = equals((boolean[]) e1, (boolean[]) e2); 4770 else 4771 eq = e1.equals(e2); 4772 return eq; 4773 } 4774 4775 /** 4776 * Returns a string representation of the contents of the specified array. 4777 * The string representation consists of a list of the array's elements, 4778 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4779 * separated by the characters {@code ", "} (a comma followed by a 4780 * space). Elements are converted to strings as by 4781 * {@code String.valueOf(long)}. Returns {@code "null"} if {@code a} 4782 * is {@code null}. 4783 * 4784 * @param a the array whose string representation to return 4785 * @return a string representation of {@code a} 4786 * @since 1.5 4787 */ toString(long[] a)4788 public static String toString(long[] a) { 4789 if (a == null) 4790 return "null"; 4791 int iMax = a.length - 1; 4792 if (iMax == -1) 4793 return "[]"; 4794 4795 StringBuilder b = new StringBuilder(); 4796 b.append('['); 4797 for (int i = 0; ; i++) { 4798 b.append(a[i]); 4799 if (i == iMax) 4800 return b.append(']').toString(); 4801 b.append(", "); 4802 } 4803 } 4804 4805 /** 4806 * Returns a string representation of the contents of the specified array. 4807 * The string representation consists of a list of the array's elements, 4808 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4809 * separated by the characters {@code ", "} (a comma followed by a 4810 * space). Elements are converted to strings as by 4811 * {@code String.valueOf(int)}. Returns {@code "null"} if {@code a} is 4812 * {@code null}. 4813 * 4814 * @param a the array whose string representation to return 4815 * @return a string representation of {@code a} 4816 * @since 1.5 4817 */ toString(int[] a)4818 public static String toString(int[] a) { 4819 if (a == null) 4820 return "null"; 4821 int iMax = a.length - 1; 4822 if (iMax == -1) 4823 return "[]"; 4824 4825 StringBuilder b = new StringBuilder(); 4826 b.append('['); 4827 for (int i = 0; ; i++) { 4828 b.append(a[i]); 4829 if (i == iMax) 4830 return b.append(']').toString(); 4831 b.append(", "); 4832 } 4833 } 4834 4835 /** 4836 * Returns a string representation of the contents of the specified array. 4837 * The string representation consists of a list of the array's elements, 4838 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4839 * separated by the characters {@code ", "} (a comma followed by a 4840 * space). Elements are converted to strings as by 4841 * {@code String.valueOf(short)}. Returns {@code "null"} if {@code a} 4842 * is {@code null}. 4843 * 4844 * @param a the array whose string representation to return 4845 * @return a string representation of {@code a} 4846 * @since 1.5 4847 */ toString(short[] a)4848 public static String toString(short[] a) { 4849 if (a == null) 4850 return "null"; 4851 int iMax = a.length - 1; 4852 if (iMax == -1) 4853 return "[]"; 4854 4855 StringBuilder b = new StringBuilder(); 4856 b.append('['); 4857 for (int i = 0; ; i++) { 4858 b.append(a[i]); 4859 if (i == iMax) 4860 return b.append(']').toString(); 4861 b.append(", "); 4862 } 4863 } 4864 4865 /** 4866 * Returns a string representation of the contents of the specified array. 4867 * The string representation consists of a list of the array's elements, 4868 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4869 * separated by the characters {@code ", "} (a comma followed by a 4870 * space). Elements are converted to strings as by 4871 * {@code String.valueOf(char)}. Returns {@code "null"} if {@code a} 4872 * is {@code null}. 4873 * 4874 * @param a the array whose string representation to return 4875 * @return a string representation of {@code a} 4876 * @since 1.5 4877 */ toString(char[] a)4878 public static String toString(char[] a) { 4879 if (a == null) 4880 return "null"; 4881 int iMax = a.length - 1; 4882 if (iMax == -1) 4883 return "[]"; 4884 4885 StringBuilder b = new StringBuilder(); 4886 b.append('['); 4887 for (int i = 0; ; i++) { 4888 b.append(a[i]); 4889 if (i == iMax) 4890 return b.append(']').toString(); 4891 b.append(", "); 4892 } 4893 } 4894 4895 /** 4896 * Returns a string representation of the contents of the specified array. 4897 * The string representation consists of a list of the array's elements, 4898 * enclosed in square brackets ({@code "[]"}). Adjacent elements 4899 * are separated by the characters {@code ", "} (a comma followed 4900 * by a space). Elements are converted to strings as by 4901 * {@code String.valueOf(byte)}. Returns {@code "null"} if 4902 * {@code a} is {@code null}. 4903 * 4904 * @param a the array whose string representation to return 4905 * @return a string representation of {@code a} 4906 * @since 1.5 4907 */ toString(byte[] a)4908 public static String toString(byte[] a) { 4909 if (a == null) 4910 return "null"; 4911 int iMax = a.length - 1; 4912 if (iMax == -1) 4913 return "[]"; 4914 4915 StringBuilder b = new StringBuilder(); 4916 b.append('['); 4917 for (int i = 0; ; i++) { 4918 b.append(a[i]); 4919 if (i == iMax) 4920 return b.append(']').toString(); 4921 b.append(", "); 4922 } 4923 } 4924 4925 /** 4926 * Returns a string representation of the contents of the specified array. 4927 * The string representation consists of a list of the array's elements, 4928 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4929 * separated by the characters {@code ", "} (a comma followed by a 4930 * space). Elements are converted to strings as by 4931 * {@code String.valueOf(boolean)}. Returns {@code "null"} if 4932 * {@code a} is {@code null}. 4933 * 4934 * @param a the array whose string representation to return 4935 * @return a string representation of {@code a} 4936 * @since 1.5 4937 */ toString(boolean[] a)4938 public static String toString(boolean[] a) { 4939 if (a == null) 4940 return "null"; 4941 int iMax = a.length - 1; 4942 if (iMax == -1) 4943 return "[]"; 4944 4945 StringBuilder b = new StringBuilder(); 4946 b.append('['); 4947 for (int i = 0; ; i++) { 4948 b.append(a[i]); 4949 if (i == iMax) 4950 return b.append(']').toString(); 4951 b.append(", "); 4952 } 4953 } 4954 4955 /** 4956 * Returns a string representation of the contents of the specified array. 4957 * The string representation consists of a list of the array's elements, 4958 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4959 * separated by the characters {@code ", "} (a comma followed by a 4960 * space). Elements are converted to strings as by 4961 * {@code String.valueOf(float)}. Returns {@code "null"} if {@code a} 4962 * is {@code null}. 4963 * 4964 * @param a the array whose string representation to return 4965 * @return a string representation of {@code a} 4966 * @since 1.5 4967 */ toString(float[] a)4968 public static String toString(float[] a) { 4969 if (a == null) 4970 return "null"; 4971 4972 int iMax = a.length - 1; 4973 if (iMax == -1) 4974 return "[]"; 4975 4976 StringBuilder b = new StringBuilder(); 4977 b.append('['); 4978 for (int i = 0; ; i++) { 4979 b.append(a[i]); 4980 if (i == iMax) 4981 return b.append(']').toString(); 4982 b.append(", "); 4983 } 4984 } 4985 4986 /** 4987 * Returns a string representation of the contents of the specified array. 4988 * The string representation consists of a list of the array's elements, 4989 * enclosed in square brackets ({@code "[]"}). Adjacent elements are 4990 * separated by the characters {@code ", "} (a comma followed by a 4991 * space). Elements are converted to strings as by 4992 * {@code String.valueOf(double)}. Returns {@code "null"} if {@code a} 4993 * is {@code null}. 4994 * 4995 * @param a the array whose string representation to return 4996 * @return a string representation of {@code a} 4997 * @since 1.5 4998 */ toString(double[] a)4999 public static String toString(double[] a) { 5000 if (a == null) 5001 return "null"; 5002 int iMax = a.length - 1; 5003 if (iMax == -1) 5004 return "[]"; 5005 5006 StringBuilder b = new StringBuilder(); 5007 b.append('['); 5008 for (int i = 0; ; i++) { 5009 b.append(a[i]); 5010 if (i == iMax) 5011 return b.append(']').toString(); 5012 b.append(", "); 5013 } 5014 } 5015 5016 /** 5017 * Returns a string representation of the contents of the specified array. 5018 * If the array contains other arrays as elements, they are converted to 5019 * strings by the {@link Object#toString} method inherited from 5020 * {@code Object}, which describes their <i>identities</i> rather than 5021 * their contents. 5022 * 5023 * <p>The value returned by this method is equal to the value that would 5024 * be returned by {@code Arrays.asList(a).toString()}, unless {@code a} 5025 * is {@code null}, in which case {@code "null"} is returned. 5026 * 5027 * @param a the array whose string representation to return 5028 * @return a string representation of {@code a} 5029 * @see #deepToString(Object[]) 5030 * @since 1.5 5031 */ toString(Object[] a)5032 public static String toString(Object[] a) { 5033 if (a == null) 5034 return "null"; 5035 5036 int iMax = a.length - 1; 5037 if (iMax == -1) 5038 return "[]"; 5039 5040 StringBuilder b = new StringBuilder(); 5041 b.append('['); 5042 for (int i = 0; ; i++) { 5043 b.append(String.valueOf(a[i])); 5044 if (i == iMax) 5045 return b.append(']').toString(); 5046 b.append(", "); 5047 } 5048 } 5049 5050 /** 5051 * Returns a string representation of the "deep contents" of the specified 5052 * array. If the array contains other arrays as elements, the string 5053 * representation contains their contents and so on. This method is 5054 * designed for converting multidimensional arrays to strings. 5055 * 5056 * <p>The string representation consists of a list of the array's 5057 * elements, enclosed in square brackets ({@code "[]"}). Adjacent 5058 * elements are separated by the characters {@code ", "} (a comma 5059 * followed by a space). Elements are converted to strings as by 5060 * {@code String.valueOf(Object)}, unless they are themselves 5061 * arrays. 5062 * 5063 * <p>If an element {@code e} is an array of a primitive type, it is 5064 * converted to a string as by invoking the appropriate overloading of 5065 * {@code Arrays.toString(e)}. If an element {@code e} is an array of a 5066 * reference type, it is converted to a string as by invoking 5067 * this method recursively. 5068 * 5069 * <p>To avoid infinite recursion, if the specified array contains itself 5070 * as an element, or contains an indirect reference to itself through one 5071 * or more levels of arrays, the self-reference is converted to the string 5072 * {@code "[...]"}. For example, an array containing only a reference 5073 * to itself would be rendered as {@code "[[...]]"}. 5074 * 5075 * <p>This method returns {@code "null"} if the specified array 5076 * is {@code null}. 5077 * 5078 * @param a the array whose string representation to return 5079 * @return a string representation of {@code a} 5080 * @see #toString(Object[]) 5081 * @since 1.5 5082 */ deepToString(Object[] a)5083 public static String deepToString(Object[] a) { 5084 if (a == null) 5085 return "null"; 5086 5087 int bufLen = 20 * a.length; 5088 if (a.length != 0 && bufLen <= 0) 5089 bufLen = Integer.MAX_VALUE; 5090 StringBuilder buf = new StringBuilder(bufLen); 5091 deepToString(a, buf, new HashSet<>()); 5092 return buf.toString(); 5093 } 5094 deepToString(Object[] a, StringBuilder buf, Set<Object[]> dejaVu)5095 private static void deepToString(Object[] a, StringBuilder buf, 5096 Set<Object[]> dejaVu) { 5097 if (a == null) { 5098 buf.append("null"); 5099 return; 5100 } 5101 int iMax = a.length - 1; 5102 if (iMax == -1) { 5103 buf.append("[]"); 5104 return; 5105 } 5106 5107 dejaVu.add(a); 5108 buf.append('['); 5109 for (int i = 0; ; i++) { 5110 5111 Object element = a[i]; 5112 if (element == null) { 5113 buf.append("null"); 5114 } else { 5115 Class<?> eClass = element.getClass(); 5116 5117 if (eClass.isArray()) { 5118 if (eClass == byte[].class) 5119 buf.append(toString((byte[]) element)); 5120 else if (eClass == short[].class) 5121 buf.append(toString((short[]) element)); 5122 else if (eClass == int[].class) 5123 buf.append(toString((int[]) element)); 5124 else if (eClass == long[].class) 5125 buf.append(toString((long[]) element)); 5126 else if (eClass == char[].class) 5127 buf.append(toString((char[]) element)); 5128 else if (eClass == float[].class) 5129 buf.append(toString((float[]) element)); 5130 else if (eClass == double[].class) 5131 buf.append(toString((double[]) element)); 5132 else if (eClass == boolean[].class) 5133 buf.append(toString((boolean[]) element)); 5134 else { // element is an array of object references 5135 if (dejaVu.contains(element)) 5136 buf.append("[...]"); 5137 else 5138 deepToString((Object[])element, buf, dejaVu); 5139 } 5140 } else { // element is non-null and not an array 5141 buf.append(element.toString()); 5142 } 5143 } 5144 if (i == iMax) 5145 break; 5146 buf.append(", "); 5147 } 5148 buf.append(']'); 5149 dejaVu.remove(a); 5150 } 5151 5152 5153 /** 5154 * Set all elements of the specified array, using the provided 5155 * generator function to compute each element. 5156 * 5157 * <p>If the generator function throws an exception, it is relayed to 5158 * the caller and the array is left in an indeterminate state. 5159 * 5160 * @apiNote 5161 * Setting a subrange of an array, using a generator function to compute 5162 * each element, can be written as follows: 5163 * <pre>{@code 5164 * IntStream.range(startInclusive, endExclusive) 5165 * .forEach(i -> array[i] = generator.apply(i)); 5166 * }</pre> 5167 * 5168 * @param <T> type of elements of the array 5169 * @param array array to be initialized 5170 * @param generator a function accepting an index and producing the desired 5171 * value for that position 5172 * @throws NullPointerException if the generator is null 5173 * @since 1.8 5174 */ setAll(T[] array, IntFunction<? extends T> generator)5175 public static <T> void setAll(T[] array, IntFunction<? extends T> generator) { 5176 Objects.requireNonNull(generator); 5177 for (int i = 0; i < array.length; i++) 5178 array[i] = generator.apply(i); 5179 } 5180 5181 /** 5182 * Set all elements of the specified array, in parallel, using the 5183 * provided generator function to compute each element. 5184 * 5185 * <p>If the generator function throws an exception, an unchecked exception 5186 * is thrown from {@code parallelSetAll} and the array is left in an 5187 * indeterminate state. 5188 * 5189 * @apiNote 5190 * Setting a subrange of an array, in parallel, using a generator function 5191 * to compute each element, can be written as follows: 5192 * <pre>{@code 5193 * IntStream.range(startInclusive, endExclusive) 5194 * .parallel() 5195 * .forEach(i -> array[i] = generator.apply(i)); 5196 * }</pre> 5197 * 5198 * @param <T> type of elements of the array 5199 * @param array array to be initialized 5200 * @param generator a function accepting an index and producing the desired 5201 * value for that position 5202 * @throws NullPointerException if the generator is null 5203 * @since 1.8 5204 */ parallelSetAll(T[] array, IntFunction<? extends T> generator)5205 public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) { 5206 Objects.requireNonNull(generator); 5207 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); }); 5208 } 5209 5210 /** 5211 * Set all elements of the specified array, using the provided 5212 * generator function to compute each element. 5213 * 5214 * <p>If the generator function throws an exception, it is relayed to 5215 * the caller and the array is left in an indeterminate state. 5216 * 5217 * @apiNote 5218 * Setting a subrange of an array, using a generator function to compute 5219 * each element, can be written as follows: 5220 * <pre>{@code 5221 * IntStream.range(startInclusive, endExclusive) 5222 * .forEach(i -> array[i] = generator.applyAsInt(i)); 5223 * }</pre> 5224 * 5225 * @param array array to be initialized 5226 * @param generator a function accepting an index and producing the desired 5227 * value for that position 5228 * @throws NullPointerException if the generator is null 5229 * @since 1.8 5230 */ setAll(int[] array, IntUnaryOperator generator)5231 public static void setAll(int[] array, IntUnaryOperator generator) { 5232 Objects.requireNonNull(generator); 5233 for (int i = 0; i < array.length; i++) 5234 array[i] = generator.applyAsInt(i); 5235 } 5236 5237 /** 5238 * Set all elements of the specified array, in parallel, using the 5239 * provided generator function to compute each element. 5240 * 5241 * <p>If the generator function throws an exception, an unchecked exception 5242 * is thrown from {@code parallelSetAll} and the array is left in an 5243 * indeterminate state. 5244 * 5245 * @apiNote 5246 * Setting a subrange of an array, in parallel, using a generator function 5247 * to compute each element, can be written as follows: 5248 * <pre>{@code 5249 * IntStream.range(startInclusive, endExclusive) 5250 * .parallel() 5251 * .forEach(i -> array[i] = generator.applyAsInt(i)); 5252 * }</pre> 5253 * 5254 * @param array array to be initialized 5255 * @param generator a function accepting an index and producing the desired 5256 * value for that position 5257 * @throws NullPointerException if the generator is null 5258 * @since 1.8 5259 */ parallelSetAll(int[] array, IntUnaryOperator generator)5260 public static void parallelSetAll(int[] array, IntUnaryOperator generator) { 5261 Objects.requireNonNull(generator); 5262 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); }); 5263 } 5264 5265 /** 5266 * Set all elements of the specified array, using the provided 5267 * generator function to compute each element. 5268 * 5269 * <p>If the generator function throws an exception, it is relayed to 5270 * the caller and the array is left in an indeterminate state. 5271 * 5272 * @apiNote 5273 * Setting a subrange of an array, using a generator function to compute 5274 * each element, can be written as follows: 5275 * <pre>{@code 5276 * IntStream.range(startInclusive, endExclusive) 5277 * .forEach(i -> array[i] = generator.applyAsLong(i)); 5278 * }</pre> 5279 * 5280 * @param array array to be initialized 5281 * @param generator a function accepting an index and producing the desired 5282 * value for that position 5283 * @throws NullPointerException if the generator is null 5284 * @since 1.8 5285 */ setAll(long[] array, IntToLongFunction generator)5286 public static void setAll(long[] array, IntToLongFunction generator) { 5287 Objects.requireNonNull(generator); 5288 for (int i = 0; i < array.length; i++) 5289 array[i] = generator.applyAsLong(i); 5290 } 5291 5292 /** 5293 * Set all elements of the specified array, in parallel, using the 5294 * provided generator function to compute each element. 5295 * 5296 * <p>If the generator function throws an exception, an unchecked exception 5297 * is thrown from {@code parallelSetAll} and the array is left in an 5298 * indeterminate state. 5299 * 5300 * @apiNote 5301 * Setting a subrange of an array, in parallel, using a generator function 5302 * to compute each element, can be written as follows: 5303 * <pre>{@code 5304 * IntStream.range(startInclusive, endExclusive) 5305 * .parallel() 5306 * .forEach(i -> array[i] = generator.applyAsLong(i)); 5307 * }</pre> 5308 * 5309 * @param array array to be initialized 5310 * @param generator a function accepting an index and producing the desired 5311 * value for that position 5312 * @throws NullPointerException if the generator is null 5313 * @since 1.8 5314 */ parallelSetAll(long[] array, IntToLongFunction generator)5315 public static void parallelSetAll(long[] array, IntToLongFunction generator) { 5316 Objects.requireNonNull(generator); 5317 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); }); 5318 } 5319 5320 /** 5321 * Set all elements of the specified array, using the provided 5322 * generator function to compute each element. 5323 * 5324 * <p>If the generator function throws an exception, it is relayed to 5325 * the caller and the array is left in an indeterminate state. 5326 * 5327 * @apiNote 5328 * Setting a subrange of an array, using a generator function to compute 5329 * each element, can be written as follows: 5330 * <pre>{@code 5331 * IntStream.range(startInclusive, endExclusive) 5332 * .forEach(i -> array[i] = generator.applyAsDouble(i)); 5333 * }</pre> 5334 * 5335 * @param array array to be initialized 5336 * @param generator a function accepting an index and producing the desired 5337 * value for that position 5338 * @throws NullPointerException if the generator is null 5339 * @since 1.8 5340 */ setAll(double[] array, IntToDoubleFunction generator)5341 public static void setAll(double[] array, IntToDoubleFunction generator) { 5342 Objects.requireNonNull(generator); 5343 for (int i = 0; i < array.length; i++) 5344 array[i] = generator.applyAsDouble(i); 5345 } 5346 5347 /** 5348 * Set all elements of the specified array, in parallel, using the 5349 * provided generator function to compute each element. 5350 * 5351 * <p>If the generator function throws an exception, an unchecked exception 5352 * is thrown from {@code parallelSetAll} and the array is left in an 5353 * indeterminate state. 5354 * 5355 * @apiNote 5356 * Setting a subrange of an array, in parallel, using a generator function 5357 * to compute each element, can be written as follows: 5358 * <pre>{@code 5359 * IntStream.range(startInclusive, endExclusive) 5360 * .parallel() 5361 * .forEach(i -> array[i] = generator.applyAsDouble(i)); 5362 * }</pre> 5363 * 5364 * @param array array to be initialized 5365 * @param generator a function accepting an index and producing the desired 5366 * value for that position 5367 * @throws NullPointerException if the generator is null 5368 * @since 1.8 5369 */ parallelSetAll(double[] array, IntToDoubleFunction generator)5370 public static void parallelSetAll(double[] array, IntToDoubleFunction generator) { 5371 Objects.requireNonNull(generator); 5372 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); }); 5373 } 5374 5375 /** 5376 * Returns a {@link Spliterator} covering all of the specified array. 5377 * 5378 * <p>The spliterator reports {@link Spliterator#SIZED}, 5379 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5380 * {@link Spliterator#IMMUTABLE}. 5381 * 5382 * @param <T> type of elements 5383 * @param array the array, assumed to be unmodified during use 5384 * @return a spliterator for the array elements 5385 * @since 1.8 5386 */ spliterator(T[] array)5387 public static <T> Spliterator<T> spliterator(T[] array) { 5388 return Spliterators.spliterator(array, 5389 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5390 } 5391 5392 /** 5393 * Returns a {@link Spliterator} covering the specified range of the 5394 * specified array. 5395 * 5396 * <p>The spliterator reports {@link Spliterator#SIZED}, 5397 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5398 * {@link Spliterator#IMMUTABLE}. 5399 * 5400 * @param <T> type of elements 5401 * @param array the array, assumed to be unmodified during use 5402 * @param startInclusive the first index to cover, inclusive 5403 * @param endExclusive index immediately past the last index to cover 5404 * @return a spliterator for the array elements 5405 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5406 * negative, {@code endExclusive} is less than 5407 * {@code startInclusive}, or {@code endExclusive} is greater than 5408 * the array size 5409 * @since 1.8 5410 */ spliterator(T[] array, int startInclusive, int endExclusive)5411 public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) { 5412 return Spliterators.spliterator(array, startInclusive, endExclusive, 5413 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5414 } 5415 5416 /** 5417 * Returns a {@link Spliterator.OfInt} covering all of the specified array. 5418 * 5419 * <p>The spliterator reports {@link Spliterator#SIZED}, 5420 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5421 * {@link Spliterator#IMMUTABLE}. 5422 * 5423 * @param array the array, assumed to be unmodified during use 5424 * @return a spliterator for the array elements 5425 * @since 1.8 5426 */ spliterator(int[] array)5427 public static Spliterator.OfInt spliterator(int[] array) { 5428 return Spliterators.spliterator(array, 5429 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5430 } 5431 5432 /** 5433 * Returns a {@link Spliterator.OfInt} covering the specified range of the 5434 * specified array. 5435 * 5436 * <p>The spliterator reports {@link Spliterator#SIZED}, 5437 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5438 * {@link Spliterator#IMMUTABLE}. 5439 * 5440 * @param array the array, assumed to be unmodified during use 5441 * @param startInclusive the first index to cover, inclusive 5442 * @param endExclusive index immediately past the last index to cover 5443 * @return a spliterator for the array elements 5444 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5445 * negative, {@code endExclusive} is less than 5446 * {@code startInclusive}, or {@code endExclusive} is greater than 5447 * the array size 5448 * @since 1.8 5449 */ spliterator(int[] array, int startInclusive, int endExclusive)5450 public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) { 5451 return Spliterators.spliterator(array, startInclusive, endExclusive, 5452 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5453 } 5454 5455 /** 5456 * Returns a {@link Spliterator.OfLong} covering all of the specified array. 5457 * 5458 * <p>The spliterator reports {@link Spliterator#SIZED}, 5459 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5460 * {@link Spliterator#IMMUTABLE}. 5461 * 5462 * @param array the array, assumed to be unmodified during use 5463 * @return the spliterator for the array elements 5464 * @since 1.8 5465 */ spliterator(long[] array)5466 public static Spliterator.OfLong spliterator(long[] array) { 5467 return Spliterators.spliterator(array, 5468 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5469 } 5470 5471 /** 5472 * Returns a {@link Spliterator.OfLong} covering the specified range of the 5473 * specified array. 5474 * 5475 * <p>The spliterator reports {@link Spliterator#SIZED}, 5476 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5477 * {@link Spliterator#IMMUTABLE}. 5478 * 5479 * @param array the array, assumed to be unmodified during use 5480 * @param startInclusive the first index to cover, inclusive 5481 * @param endExclusive index immediately past the last index to cover 5482 * @return a spliterator for the array elements 5483 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5484 * negative, {@code endExclusive} is less than 5485 * {@code startInclusive}, or {@code endExclusive} is greater than 5486 * the array size 5487 * @since 1.8 5488 */ spliterator(long[] array, int startInclusive, int endExclusive)5489 public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) { 5490 return Spliterators.spliterator(array, startInclusive, endExclusive, 5491 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5492 } 5493 5494 /** 5495 * Returns a {@link Spliterator.OfDouble} covering all of the specified 5496 * array. 5497 * 5498 * <p>The spliterator reports {@link Spliterator#SIZED}, 5499 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5500 * {@link Spliterator#IMMUTABLE}. 5501 * 5502 * @param array the array, assumed to be unmodified during use 5503 * @return a spliterator for the array elements 5504 * @since 1.8 5505 */ spliterator(double[] array)5506 public static Spliterator.OfDouble spliterator(double[] array) { 5507 return Spliterators.spliterator(array, 5508 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5509 } 5510 5511 /** 5512 * Returns a {@link Spliterator.OfDouble} covering the specified range of 5513 * the specified array. 5514 * 5515 * <p>The spliterator reports {@link Spliterator#SIZED}, 5516 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5517 * {@link Spliterator#IMMUTABLE}. 5518 * 5519 * @param array the array, assumed to be unmodified during use 5520 * @param startInclusive the first index to cover, inclusive 5521 * @param endExclusive index immediately past the last index to cover 5522 * @return a spliterator for the array elements 5523 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5524 * negative, {@code endExclusive} is less than 5525 * {@code startInclusive}, or {@code endExclusive} is greater than 5526 * the array size 5527 * @since 1.8 5528 */ spliterator(double[] array, int startInclusive, int endExclusive)5529 public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) { 5530 return Spliterators.spliterator(array, startInclusive, endExclusive, 5531 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5532 } 5533 5534 /** 5535 * Returns a sequential {@link Stream} with the specified array as its 5536 * source. 5537 * 5538 * @param <T> The type of the array elements 5539 * @param array The array, assumed to be unmodified during use 5540 * @return a {@code Stream} for the array 5541 * @since 1.8 5542 */ stream(T[] array)5543 public static <T> Stream<T> stream(T[] array) { 5544 return stream(array, 0, array.length); 5545 } 5546 5547 /** 5548 * Returns a sequential {@link Stream} with the specified range of the 5549 * specified array as its source. 5550 * 5551 * @param <T> the type of the array elements 5552 * @param array the array, assumed to be unmodified during use 5553 * @param startInclusive the first index to cover, inclusive 5554 * @param endExclusive index immediately past the last index to cover 5555 * @return a {@code Stream} for the array range 5556 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5557 * negative, {@code endExclusive} is less than 5558 * {@code startInclusive}, or {@code endExclusive} is greater than 5559 * the array size 5560 * @since 1.8 5561 */ stream(T[] array, int startInclusive, int endExclusive)5562 public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) { 5563 return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false); 5564 } 5565 5566 /** 5567 * Returns a sequential {@link IntStream} with the specified array as its 5568 * source. 5569 * 5570 * @param array the array, assumed to be unmodified during use 5571 * @return an {@code IntStream} for the array 5572 * @since 1.8 5573 */ stream(int[] array)5574 public static IntStream stream(int[] array) { 5575 return stream(array, 0, array.length); 5576 } 5577 5578 /** 5579 * Returns a sequential {@link IntStream} with the specified range of the 5580 * specified array as its source. 5581 * 5582 * @param array the array, assumed to be unmodified during use 5583 * @param startInclusive the first index to cover, inclusive 5584 * @param endExclusive index immediately past the last index to cover 5585 * @return an {@code IntStream} for the array range 5586 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5587 * negative, {@code endExclusive} is less than 5588 * {@code startInclusive}, or {@code endExclusive} is greater than 5589 * the array size 5590 * @since 1.8 5591 */ stream(int[] array, int startInclusive, int endExclusive)5592 public static IntStream stream(int[] array, int startInclusive, int endExclusive) { 5593 return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false); 5594 } 5595 5596 /** 5597 * Returns a sequential {@link LongStream} with the specified array as its 5598 * source. 5599 * 5600 * @param array the array, assumed to be unmodified during use 5601 * @return a {@code LongStream} for the array 5602 * @since 1.8 5603 */ stream(long[] array)5604 public static LongStream stream(long[] array) { 5605 return stream(array, 0, array.length); 5606 } 5607 5608 /** 5609 * Returns a sequential {@link LongStream} with the specified range of the 5610 * specified array as its source. 5611 * 5612 * @param array the array, assumed to be unmodified during use 5613 * @param startInclusive the first index to cover, inclusive 5614 * @param endExclusive index immediately past the last index to cover 5615 * @return a {@code LongStream} for the array range 5616 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5617 * negative, {@code endExclusive} is less than 5618 * {@code startInclusive}, or {@code endExclusive} is greater than 5619 * the array size 5620 * @since 1.8 5621 */ stream(long[] array, int startInclusive, int endExclusive)5622 public static LongStream stream(long[] array, int startInclusive, int endExclusive) { 5623 return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false); 5624 } 5625 5626 /** 5627 * Returns a sequential {@link DoubleStream} with the specified array as its 5628 * source. 5629 * 5630 * @param array the array, assumed to be unmodified during use 5631 * @return a {@code DoubleStream} for the array 5632 * @since 1.8 5633 */ stream(double[] array)5634 public static DoubleStream stream(double[] array) { 5635 return stream(array, 0, array.length); 5636 } 5637 5638 /** 5639 * Returns a sequential {@link DoubleStream} with the specified range of the 5640 * specified array as its source. 5641 * 5642 * @param array the array, assumed to be unmodified during use 5643 * @param startInclusive the first index to cover, inclusive 5644 * @param endExclusive index immediately past the last index to cover 5645 * @return a {@code DoubleStream} for the array range 5646 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5647 * negative, {@code endExclusive} is less than 5648 * {@code startInclusive}, or {@code endExclusive} is greater than 5649 * the array size 5650 * @since 1.8 5651 */ stream(double[] array, int startInclusive, int endExclusive)5652 public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) { 5653 return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false); 5654 } 5655 5656 5657 // Comparison methods 5658 5659 // Compare boolean 5660 5661 /** 5662 * Compares two {@code boolean} arrays lexicographically. 5663 * 5664 * <p>If the two arrays share a common prefix then the lexicographic 5665 * comparison is the result of comparing two elements, as if by 5666 * {@link Boolean#compare(boolean, boolean)}, at an index within the 5667 * respective arrays that is the prefix length. 5668 * Otherwise, one array is a proper prefix of the other and, lexicographic 5669 * comparison is the result of comparing the two array lengths. 5670 * (See {@link #mismatch(boolean[], boolean[])} for the definition of a 5671 * common and proper prefix.) 5672 * 5673 * <p>A {@code null} array reference is considered lexicographically less 5674 * than a non-{@code null} array reference. Two {@code null} array 5675 * references are considered equal. 5676 * 5677 * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals}, 5678 * more specifically the following holds for arrays {@code a} and {@code b}: 5679 * <pre>{@code 5680 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 5681 * }</pre> 5682 * 5683 * @apiNote 5684 * <p>This method behaves as if (for non-{@code null} array references): 5685 * <pre>{@code 5686 * int i = Arrays.mismatch(a, b); 5687 * if (i >= 0 && i < Math.min(a.length, b.length)) 5688 * return Boolean.compare(a[i], b[i]); 5689 * return a.length - b.length; 5690 * }</pre> 5691 * 5692 * @param a the first array to compare 5693 * @param b the second array to compare 5694 * @return the value {@code 0} if the first and second array are equal and 5695 * contain the same elements in the same order; 5696 * a value less than {@code 0} if the first array is 5697 * lexicographically less than the second array; and 5698 * a value greater than {@code 0} if the first array is 5699 * lexicographically greater than the second array 5700 * @since 9 5701 */ compare(boolean[] a, boolean[] b)5702 public static int compare(boolean[] a, boolean[] b) { 5703 if (a == b) 5704 return 0; 5705 if (a == null || b == null) 5706 return a == null ? -1 : 1; 5707 5708 int i = ArraysSupport.mismatch(a, b, 5709 Math.min(a.length, b.length)); 5710 if (i >= 0) { 5711 return Boolean.compare(a[i], b[i]); 5712 } 5713 5714 return a.length - b.length; 5715 } 5716 5717 /** 5718 * Compares two {@code boolean} arrays lexicographically over the specified 5719 * ranges. 5720 * 5721 * <p>If the two arrays, over the specified ranges, share a common prefix 5722 * then the lexicographic comparison is the result of comparing two 5723 * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a 5724 * relative index within the respective arrays that is the length of the 5725 * prefix. 5726 * Otherwise, one array is a proper prefix of the other and, lexicographic 5727 * comparison is the result of comparing the two range lengths. 5728 * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the 5729 * definition of a common and proper prefix.) 5730 * 5731 * <p>The comparison is consistent with 5732 * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more 5733 * specifically the following holds for arrays {@code a} and {@code b} with 5734 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 5735 * [{@code bFromIndex}, {@code btoIndex}) respectively: 5736 * <pre>{@code 5737 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 5738 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 5739 * }</pre> 5740 * 5741 * @apiNote 5742 * <p>This method behaves as if: 5743 * <pre>{@code 5744 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 5745 * b, bFromIndex, bToIndex); 5746 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 5747 * return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]); 5748 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 5749 * }</pre> 5750 * 5751 * @param a the first array to compare 5752 * @param aFromIndex the index (inclusive) of the first element in the 5753 * first array to be compared 5754 * @param aToIndex the index (exclusive) of the last element in the 5755 * first array to be compared 5756 * @param b the second array to compare 5757 * @param bFromIndex the index (inclusive) of the first element in the 5758 * second array to be compared 5759 * @param bToIndex the index (exclusive) of the last element in the 5760 * second array to be compared 5761 * @return the value {@code 0} if, over the specified ranges, the first and 5762 * second array are equal and contain the same elements in the same 5763 * order; 5764 * a value less than {@code 0} if, over the specified ranges, the 5765 * first array is lexicographically less than the second array; and 5766 * a value greater than {@code 0} if, over the specified ranges, the 5767 * first array is lexicographically greater than the second array 5768 * @throws IllegalArgumentException 5769 * if {@code aFromIndex > aToIndex} or 5770 * if {@code bFromIndex > bToIndex} 5771 * @throws ArrayIndexOutOfBoundsException 5772 * if {@code aFromIndex < 0 or aToIndex > a.length} or 5773 * if {@code bFromIndex < 0 or bToIndex > b.length} 5774 * @throws NullPointerException 5775 * if either array is {@code null} 5776 * @since 9 5777 */ compare(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)5778 public static int compare(boolean[] a, int aFromIndex, int aToIndex, 5779 boolean[] b, int bFromIndex, int bToIndex) { 5780 rangeCheck(a.length, aFromIndex, aToIndex); 5781 rangeCheck(b.length, bFromIndex, bToIndex); 5782 5783 int aLength = aToIndex - aFromIndex; 5784 int bLength = bToIndex - bFromIndex; 5785 int i = ArraysSupport.mismatch(a, aFromIndex, 5786 b, bFromIndex, 5787 Math.min(aLength, bLength)); 5788 if (i >= 0) { 5789 return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]); 5790 } 5791 5792 return aLength - bLength; 5793 } 5794 5795 // Compare byte 5796 5797 /** 5798 * Compares two {@code byte} arrays lexicographically. 5799 * 5800 * <p>If the two arrays share a common prefix then the lexicographic 5801 * comparison is the result of comparing two elements, as if by 5802 * {@link Byte#compare(byte, byte)}, at an index within the respective 5803 * arrays that is the prefix length. 5804 * Otherwise, one array is a proper prefix of the other and, lexicographic 5805 * comparison is the result of comparing the two array lengths. 5806 * (See {@link #mismatch(byte[], byte[])} for the definition of a common and 5807 * proper prefix.) 5808 * 5809 * <p>A {@code null} array reference is considered lexicographically less 5810 * than a non-{@code null} array reference. Two {@code null} array 5811 * references are considered equal. 5812 * 5813 * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals}, 5814 * more specifically the following holds for arrays {@code a} and {@code b}: 5815 * <pre>{@code 5816 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 5817 * }</pre> 5818 * 5819 * @apiNote 5820 * <p>This method behaves as if (for non-{@code null} array references): 5821 * <pre>{@code 5822 * int i = Arrays.mismatch(a, b); 5823 * if (i >= 0 && i < Math.min(a.length, b.length)) 5824 * return Byte.compare(a[i], b[i]); 5825 * return a.length - b.length; 5826 * }</pre> 5827 * 5828 * @param a the first array to compare 5829 * @param b the second array to compare 5830 * @return the value {@code 0} if the first and second array are equal and 5831 * contain the same elements in the same order; 5832 * a value less than {@code 0} if the first array is 5833 * lexicographically less than the second array; and 5834 * a value greater than {@code 0} if the first array is 5835 * lexicographically greater than the second array 5836 * @since 9 5837 */ compare(byte[] a, byte[] b)5838 public static int compare(byte[] a, byte[] b) { 5839 if (a == b) 5840 return 0; 5841 if (a == null || b == null) 5842 return a == null ? -1 : 1; 5843 5844 int i = ArraysSupport.mismatch(a, b, 5845 Math.min(a.length, b.length)); 5846 if (i >= 0) { 5847 return Byte.compare(a[i], b[i]); 5848 } 5849 5850 return a.length - b.length; 5851 } 5852 5853 /** 5854 * Compares two {@code byte} arrays lexicographically over the specified 5855 * ranges. 5856 * 5857 * <p>If the two arrays, over the specified ranges, share a common prefix 5858 * then the lexicographic comparison is the result of comparing two 5859 * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index 5860 * within the respective arrays that is the length of the prefix. 5861 * Otherwise, one array is a proper prefix of the other and, lexicographic 5862 * comparison is the result of comparing the two range lengths. 5863 * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the 5864 * definition of a common and proper prefix.) 5865 * 5866 * <p>The comparison is consistent with 5867 * {@link #equals(byte[], int, int, byte[], int, int) equals}, more 5868 * specifically the following holds for arrays {@code a} and {@code b} with 5869 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 5870 * [{@code bFromIndex}, {@code btoIndex}) respectively: 5871 * <pre>{@code 5872 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 5873 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 5874 * }</pre> 5875 * 5876 * @apiNote 5877 * <p>This method behaves as if: 5878 * <pre>{@code 5879 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 5880 * b, bFromIndex, bToIndex); 5881 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 5882 * return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]); 5883 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 5884 * }</pre> 5885 * 5886 * @param a the first array to compare 5887 * @param aFromIndex the index (inclusive) of the first element in the 5888 * first array to be compared 5889 * @param aToIndex the index (exclusive) of the last element in the 5890 * first array to be compared 5891 * @param b the second array to compare 5892 * @param bFromIndex the index (inclusive) of the first element in the 5893 * second array to be compared 5894 * @param bToIndex the index (exclusive) of the last element in the 5895 * second array to be compared 5896 * @return the value {@code 0} if, over the specified ranges, the first and 5897 * second array are equal and contain the same elements in the same 5898 * order; 5899 * a value less than {@code 0} if, over the specified ranges, the 5900 * first array is lexicographically less than the second array; and 5901 * a value greater than {@code 0} if, over the specified ranges, the 5902 * first array is lexicographically greater than the second array 5903 * @throws IllegalArgumentException 5904 * if {@code aFromIndex > aToIndex} or 5905 * if {@code bFromIndex > bToIndex} 5906 * @throws ArrayIndexOutOfBoundsException 5907 * if {@code aFromIndex < 0 or aToIndex > a.length} or 5908 * if {@code bFromIndex < 0 or bToIndex > b.length} 5909 * @throws NullPointerException 5910 * if either array is {@code null} 5911 * @since 9 5912 */ compare(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)5913 public static int compare(byte[] a, int aFromIndex, int aToIndex, 5914 byte[] b, int bFromIndex, int bToIndex) { 5915 rangeCheck(a.length, aFromIndex, aToIndex); 5916 rangeCheck(b.length, bFromIndex, bToIndex); 5917 5918 int aLength = aToIndex - aFromIndex; 5919 int bLength = bToIndex - bFromIndex; 5920 int i = ArraysSupport.mismatch(a, aFromIndex, 5921 b, bFromIndex, 5922 Math.min(aLength, bLength)); 5923 if (i >= 0) { 5924 return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]); 5925 } 5926 5927 return aLength - bLength; 5928 } 5929 5930 /** 5931 * Compares two {@code byte} arrays lexicographically, numerically treating 5932 * elements as unsigned. 5933 * 5934 * <p>If the two arrays share a common prefix then the lexicographic 5935 * comparison is the result of comparing two elements, as if by 5936 * {@link Byte#compareUnsigned(byte, byte)}, at an index within the 5937 * respective arrays that is the prefix length. 5938 * Otherwise, one array is a proper prefix of the other and, lexicographic 5939 * comparison is the result of comparing the two array lengths. 5940 * (See {@link #mismatch(byte[], byte[])} for the definition of a common 5941 * and proper prefix.) 5942 * 5943 * <p>A {@code null} array reference is considered lexicographically less 5944 * than a non-{@code null} array reference. Two {@code null} array 5945 * references are considered equal. 5946 * 5947 * @apiNote 5948 * <p>This method behaves as if (for non-{@code null} array references): 5949 * <pre>{@code 5950 * int i = Arrays.mismatch(a, b); 5951 * if (i >= 0 && i < Math.min(a.length, b.length)) 5952 * return Byte.compareUnsigned(a[i], b[i]); 5953 * return a.length - b.length; 5954 * }</pre> 5955 * 5956 * @param a the first array to compare 5957 * @param b the second array to compare 5958 * @return the value {@code 0} if the first and second array are 5959 * equal and contain the same elements in the same order; 5960 * a value less than {@code 0} if the first array is 5961 * lexicographically less than the second array; and 5962 * a value greater than {@code 0} if the first array is 5963 * lexicographically greater than the second array 5964 * @since 9 5965 */ compareUnsigned(byte[] a, byte[] b)5966 public static int compareUnsigned(byte[] a, byte[] b) { 5967 if (a == b) 5968 return 0; 5969 if (a == null || b == null) 5970 return a == null ? -1 : 1; 5971 5972 int i = ArraysSupport.mismatch(a, b, 5973 Math.min(a.length, b.length)); 5974 if (i >= 0) { 5975 return Byte.compareUnsigned(a[i], b[i]); 5976 } 5977 5978 return a.length - b.length; 5979 } 5980 5981 5982 /** 5983 * Compares two {@code byte} arrays lexicographically over the specified 5984 * ranges, numerically treating elements as unsigned. 5985 * 5986 * <p>If the two arrays, over the specified ranges, share a common prefix 5987 * then the lexicographic comparison is the result of comparing two 5988 * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a 5989 * relative index within the respective arrays that is the length of the 5990 * prefix. 5991 * Otherwise, one array is a proper prefix of the other and, lexicographic 5992 * comparison is the result of comparing the two range lengths. 5993 * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the 5994 * definition of a common and proper prefix.) 5995 * 5996 * @apiNote 5997 * <p>This method behaves as if: 5998 * <pre>{@code 5999 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6000 * b, bFromIndex, bToIndex); 6001 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6002 * return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6003 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6004 * }</pre> 6005 * 6006 * @param a the first array to compare 6007 * @param aFromIndex the index (inclusive) of the first element in the 6008 * first array to be compared 6009 * @param aToIndex the index (exclusive) of the last element in the 6010 * first array to be compared 6011 * @param b the second array to compare 6012 * @param bFromIndex the index (inclusive) of the first element in the 6013 * second array to be compared 6014 * @param bToIndex the index (exclusive) of the last element in the 6015 * second array to be compared 6016 * @return the value {@code 0} if, over the specified ranges, the first and 6017 * second array are equal and contain the same elements in the same 6018 * order; 6019 * a value less than {@code 0} if, over the specified ranges, the 6020 * first array is lexicographically less than the second array; and 6021 * a value greater than {@code 0} if, over the specified ranges, the 6022 * first array is lexicographically greater than the second array 6023 * @throws IllegalArgumentException 6024 * if {@code aFromIndex > aToIndex} or 6025 * if {@code bFromIndex > bToIndex} 6026 * @throws ArrayIndexOutOfBoundsException 6027 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6028 * if {@code bFromIndex < 0 or bToIndex > b.length} 6029 * @throws NullPointerException 6030 * if either array is null 6031 * @since 9 6032 */ compareUnsigned(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)6033 public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex, 6034 byte[] b, int bFromIndex, int bToIndex) { 6035 rangeCheck(a.length, aFromIndex, aToIndex); 6036 rangeCheck(b.length, bFromIndex, bToIndex); 6037 6038 int aLength = aToIndex - aFromIndex; 6039 int bLength = bToIndex - bFromIndex; 6040 int i = ArraysSupport.mismatch(a, aFromIndex, 6041 b, bFromIndex, 6042 Math.min(aLength, bLength)); 6043 if (i >= 0) { 6044 return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6045 } 6046 6047 return aLength - bLength; 6048 } 6049 6050 // Compare short 6051 6052 /** 6053 * Compares two {@code short} arrays lexicographically. 6054 * 6055 * <p>If the two arrays share a common prefix then the lexicographic 6056 * comparison is the result of comparing two elements, as if by 6057 * {@link Short#compare(short, short)}, at an index within the respective 6058 * arrays that is the prefix length. 6059 * Otherwise, one array is a proper prefix of the other and, lexicographic 6060 * comparison is the result of comparing the two array lengths. 6061 * (See {@link #mismatch(short[], short[])} for the definition of a common 6062 * and proper prefix.) 6063 * 6064 * <p>A {@code null} array reference is considered lexicographically less 6065 * than a non-{@code null} array reference. Two {@code null} array 6066 * references are considered equal. 6067 * 6068 * <p>The comparison is consistent with {@link #equals(short[], short[]) equals}, 6069 * more specifically the following holds for arrays {@code a} and {@code b}: 6070 * <pre>{@code 6071 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 6072 * }</pre> 6073 * 6074 * @apiNote 6075 * <p>This method behaves as if (for non-{@code null} array references): 6076 * <pre>{@code 6077 * int i = Arrays.mismatch(a, b); 6078 * if (i >= 0 && i < Math.min(a.length, b.length)) 6079 * return Short.compare(a[i], b[i]); 6080 * return a.length - b.length; 6081 * }</pre> 6082 * 6083 * @param a the first array to compare 6084 * @param b the second array to compare 6085 * @return the value {@code 0} if the first and second array are equal and 6086 * contain the same elements in the same order; 6087 * a value less than {@code 0} if the first array is 6088 * lexicographically less than the second array; and 6089 * a value greater than {@code 0} if the first array is 6090 * lexicographically greater than the second array 6091 * @since 9 6092 */ compare(short[] a, short[] b)6093 public static int compare(short[] a, short[] b) { 6094 if (a == b) 6095 return 0; 6096 if (a == null || b == null) 6097 return a == null ? -1 : 1; 6098 6099 int i = ArraysSupport.mismatch(a, b, 6100 Math.min(a.length, b.length)); 6101 if (i >= 0) { 6102 return Short.compare(a[i], b[i]); 6103 } 6104 6105 return a.length - b.length; 6106 } 6107 6108 /** 6109 * Compares two {@code short} arrays lexicographically over the specified 6110 * ranges. 6111 * 6112 * <p>If the two arrays, over the specified ranges, share a common prefix 6113 * then the lexicographic comparison is the result of comparing two 6114 * elements, as if by {@link Short#compare(short, short)}, at a relative 6115 * index within the respective arrays that is the length of the prefix. 6116 * Otherwise, one array is a proper prefix of the other and, lexicographic 6117 * comparison is the result of comparing the two range lengths. 6118 * (See {@link #mismatch(short[], int, int, short[], int, int)} for the 6119 * definition of a common and proper prefix.) 6120 * 6121 * <p>The comparison is consistent with 6122 * {@link #equals(short[], int, int, short[], int, int) equals}, more 6123 * specifically the following holds for arrays {@code a} and {@code b} with 6124 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 6125 * [{@code bFromIndex}, {@code btoIndex}) respectively: 6126 * <pre>{@code 6127 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 6128 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 6129 * }</pre> 6130 * 6131 * @apiNote 6132 * <p>This method behaves as if: 6133 * <pre>{@code 6134 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6135 * b, bFromIndex, bToIndex); 6136 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6137 * return Short.compare(a[aFromIndex + i], b[bFromIndex + i]); 6138 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6139 * }</pre> 6140 * 6141 * @param a the first array to compare 6142 * @param aFromIndex the index (inclusive) of the first element in the 6143 * first array to be compared 6144 * @param aToIndex the index (exclusive) of the last element in the 6145 * first array to be compared 6146 * @param b the second array to compare 6147 * @param bFromIndex the index (inclusive) of the first element in the 6148 * second array to be compared 6149 * @param bToIndex the index (exclusive) of the last element in the 6150 * second array to be compared 6151 * @return the value {@code 0} if, over the specified ranges, the first and 6152 * second array are equal and contain the same elements in the same 6153 * order; 6154 * a value less than {@code 0} if, over the specified ranges, the 6155 * first array is lexicographically less than the second array; and 6156 * a value greater than {@code 0} if, over the specified ranges, the 6157 * first array is lexicographically greater than the second array 6158 * @throws IllegalArgumentException 6159 * if {@code aFromIndex > aToIndex} or 6160 * if {@code bFromIndex > bToIndex} 6161 * @throws ArrayIndexOutOfBoundsException 6162 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6163 * if {@code bFromIndex < 0 or bToIndex > b.length} 6164 * @throws NullPointerException 6165 * if either array is {@code null} 6166 * @since 9 6167 */ compare(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)6168 public static int compare(short[] a, int aFromIndex, int aToIndex, 6169 short[] b, int bFromIndex, int bToIndex) { 6170 rangeCheck(a.length, aFromIndex, aToIndex); 6171 rangeCheck(b.length, bFromIndex, bToIndex); 6172 6173 int aLength = aToIndex - aFromIndex; 6174 int bLength = bToIndex - bFromIndex; 6175 int i = ArraysSupport.mismatch(a, aFromIndex, 6176 b, bFromIndex, 6177 Math.min(aLength, bLength)); 6178 if (i >= 0) { 6179 return Short.compare(a[aFromIndex + i], b[bFromIndex + i]); 6180 } 6181 6182 return aLength - bLength; 6183 } 6184 6185 /** 6186 * Compares two {@code short} arrays lexicographically, numerically treating 6187 * elements as unsigned. 6188 * 6189 * <p>If the two arrays share a common prefix then the lexicographic 6190 * comparison is the result of comparing two elements, as if by 6191 * {@link Short#compareUnsigned(short, short)}, at an index within the 6192 * respective arrays that is the prefix length. 6193 * Otherwise, one array is a proper prefix of the other and, lexicographic 6194 * comparison is the result of comparing the two array lengths. 6195 * (See {@link #mismatch(short[], short[])} for the definition of a common 6196 * and proper prefix.) 6197 * 6198 * <p>A {@code null} array reference is considered lexicographically less 6199 * than a non-{@code null} array reference. Two {@code null} array 6200 * references are considered equal. 6201 * 6202 * @apiNote 6203 * <p>This method behaves as if (for non-{@code null} array references): 6204 * <pre>{@code 6205 * int i = Arrays.mismatch(a, b); 6206 * if (i >= 0 && i < Math.min(a.length, b.length)) 6207 * return Short.compareUnsigned(a[i], b[i]); 6208 * return a.length - b.length; 6209 * }</pre> 6210 * 6211 * @param a the first array to compare 6212 * @param b the second array to compare 6213 * @return the value {@code 0} if the first and second array are 6214 * equal and contain the same elements in the same order; 6215 * a value less than {@code 0} if the first array is 6216 * lexicographically less than the second array; and 6217 * a value greater than {@code 0} if the first array is 6218 * lexicographically greater than the second array 6219 * @since 9 6220 */ compareUnsigned(short[] a, short[] b)6221 public static int compareUnsigned(short[] a, short[] b) { 6222 if (a == b) 6223 return 0; 6224 if (a == null || b == null) 6225 return a == null ? -1 : 1; 6226 6227 int i = ArraysSupport.mismatch(a, b, 6228 Math.min(a.length, b.length)); 6229 if (i >= 0) { 6230 return Short.compareUnsigned(a[i], b[i]); 6231 } 6232 6233 return a.length - b.length; 6234 } 6235 6236 /** 6237 * Compares two {@code short} arrays lexicographically over the specified 6238 * ranges, numerically treating elements as unsigned. 6239 * 6240 * <p>If the two arrays, over the specified ranges, share a common prefix 6241 * then the lexicographic comparison is the result of comparing two 6242 * elements, as if by {@link Short#compareUnsigned(short, short)}, at a 6243 * relative index within the respective arrays that is the length of the 6244 * prefix. 6245 * Otherwise, one array is a proper prefix of the other and, lexicographic 6246 * comparison is the result of comparing the two range lengths. 6247 * (See {@link #mismatch(short[], int, int, short[], int, int)} for the 6248 * definition of a common and proper prefix.) 6249 * 6250 * @apiNote 6251 * <p>This method behaves as if: 6252 * <pre>{@code 6253 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6254 * b, bFromIndex, bToIndex); 6255 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6256 * return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6257 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6258 * }</pre> 6259 * 6260 * @param a the first array to compare 6261 * @param aFromIndex the index (inclusive) of the first element in the 6262 * first array to be compared 6263 * @param aToIndex the index (exclusive) of the last element in the 6264 * first array to be compared 6265 * @param b the second array to compare 6266 * @param bFromIndex the index (inclusive) of the first element in the 6267 * second array to be compared 6268 * @param bToIndex the index (exclusive) of the last element in the 6269 * second array to be compared 6270 * @return the value {@code 0} if, over the specified ranges, the first and 6271 * second array are equal and contain the same elements in the same 6272 * order; 6273 * a value less than {@code 0} if, over the specified ranges, the 6274 * first array is lexicographically less than the second array; and 6275 * a value greater than {@code 0} if, over the specified ranges, the 6276 * first array is lexicographically greater than the second array 6277 * @throws IllegalArgumentException 6278 * if {@code aFromIndex > aToIndex} or 6279 * if {@code bFromIndex > bToIndex} 6280 * @throws ArrayIndexOutOfBoundsException 6281 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6282 * if {@code bFromIndex < 0 or bToIndex > b.length} 6283 * @throws NullPointerException 6284 * if either array is null 6285 * @since 9 6286 */ compareUnsigned(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)6287 public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex, 6288 short[] b, int bFromIndex, int bToIndex) { 6289 rangeCheck(a.length, aFromIndex, aToIndex); 6290 rangeCheck(b.length, bFromIndex, bToIndex); 6291 6292 int aLength = aToIndex - aFromIndex; 6293 int bLength = bToIndex - bFromIndex; 6294 int i = ArraysSupport.mismatch(a, aFromIndex, 6295 b, bFromIndex, 6296 Math.min(aLength, bLength)); 6297 if (i >= 0) { 6298 return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6299 } 6300 6301 return aLength - bLength; 6302 } 6303 6304 // Compare char 6305 6306 /** 6307 * Compares two {@code char} arrays lexicographically. 6308 * 6309 * <p>If the two arrays share a common prefix then the lexicographic 6310 * comparison is the result of comparing two elements, as if by 6311 * {@link Character#compare(char, char)}, at an index within the respective 6312 * arrays that is the prefix length. 6313 * Otherwise, one array is a proper prefix of the other and, lexicographic 6314 * comparison is the result of comparing the two array lengths. 6315 * (See {@link #mismatch(char[], char[])} for the definition of a common and 6316 * proper prefix.) 6317 * 6318 * <p>A {@code null} array reference is considered lexicographically less 6319 * than a non-{@code null} array reference. Two {@code null} array 6320 * references are considered equal. 6321 * 6322 * <p>The comparison is consistent with {@link #equals(char[], char[]) equals}, 6323 * more specifically the following holds for arrays {@code a} and {@code b}: 6324 * <pre>{@code 6325 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 6326 * }</pre> 6327 * 6328 * @apiNote 6329 * <p>This method behaves as if (for non-{@code null} array references): 6330 * <pre>{@code 6331 * int i = Arrays.mismatch(a, b); 6332 * if (i >= 0 && i < Math.min(a.length, b.length)) 6333 * return Character.compare(a[i], b[i]); 6334 * return a.length - b.length; 6335 * }</pre> 6336 * 6337 * @param a the first array to compare 6338 * @param b the second array to compare 6339 * @return the value {@code 0} if the first and second array are equal and 6340 * contain the same elements in the same order; 6341 * a value less than {@code 0} if the first array is 6342 * lexicographically less than the second array; and 6343 * a value greater than {@code 0} if the first array is 6344 * lexicographically greater than the second array 6345 * @since 9 6346 */ compare(char[] a, char[] b)6347 public static int compare(char[] a, char[] b) { 6348 if (a == b) 6349 return 0; 6350 if (a == null || b == null) 6351 return a == null ? -1 : 1; 6352 6353 int i = ArraysSupport.mismatch(a, b, 6354 Math.min(a.length, b.length)); 6355 if (i >= 0) { 6356 return Character.compare(a[i], b[i]); 6357 } 6358 6359 return a.length - b.length; 6360 } 6361 6362 /** 6363 * Compares two {@code char} arrays lexicographically over the specified 6364 * ranges. 6365 * 6366 * <p>If the two arrays, over the specified ranges, share a common prefix 6367 * then the lexicographic comparison is the result of comparing two 6368 * elements, as if by {@link Character#compare(char, char)}, at a relative 6369 * index within the respective arrays that is the length of the prefix. 6370 * Otherwise, one array is a proper prefix of the other and, lexicographic 6371 * comparison is the result of comparing the two range lengths. 6372 * (See {@link #mismatch(char[], int, int, char[], int, int)} for the 6373 * definition of a common and proper prefix.) 6374 * 6375 * <p>The comparison is consistent with 6376 * {@link #equals(char[], int, int, char[], int, int) equals}, more 6377 * specifically the following holds for arrays {@code a} and {@code b} with 6378 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 6379 * [{@code bFromIndex}, {@code btoIndex}) respectively: 6380 * <pre>{@code 6381 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 6382 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 6383 * }</pre> 6384 * 6385 * @apiNote 6386 * <p>This method behaves as if: 6387 * <pre>{@code 6388 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6389 * b, bFromIndex, bToIndex); 6390 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6391 * return Character.compare(a[aFromIndex + i], b[bFromIndex + i]); 6392 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6393 * }</pre> 6394 * 6395 * @param a the first array to compare 6396 * @param aFromIndex the index (inclusive) of the first element in the 6397 * first array to be compared 6398 * @param aToIndex the index (exclusive) of the last element in the 6399 * first array to be compared 6400 * @param b the second array to compare 6401 * @param bFromIndex the index (inclusive) of the first element in the 6402 * second array to be compared 6403 * @param bToIndex the index (exclusive) of the last element in the 6404 * second array to be compared 6405 * @return the value {@code 0} if, over the specified ranges, the first and 6406 * second array are equal and contain the same elements in the same 6407 * order; 6408 * a value less than {@code 0} if, over the specified ranges, the 6409 * first array is lexicographically less than the second array; and 6410 * a value greater than {@code 0} if, over the specified ranges, the 6411 * first array is lexicographically greater than the second array 6412 * @throws IllegalArgumentException 6413 * if {@code aFromIndex > aToIndex} or 6414 * if {@code bFromIndex > bToIndex} 6415 * @throws ArrayIndexOutOfBoundsException 6416 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6417 * if {@code bFromIndex < 0 or bToIndex > b.length} 6418 * @throws NullPointerException 6419 * if either array is {@code null} 6420 * @since 9 6421 */ compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)6422 public static int compare(char[] a, int aFromIndex, int aToIndex, 6423 char[] b, int bFromIndex, int bToIndex) { 6424 rangeCheck(a.length, aFromIndex, aToIndex); 6425 rangeCheck(b.length, bFromIndex, bToIndex); 6426 6427 int aLength = aToIndex - aFromIndex; 6428 int bLength = bToIndex - bFromIndex; 6429 int i = ArraysSupport.mismatch(a, aFromIndex, 6430 b, bFromIndex, 6431 Math.min(aLength, bLength)); 6432 if (i >= 0) { 6433 return Character.compare(a[aFromIndex + i], b[bFromIndex + i]); 6434 } 6435 6436 return aLength - bLength; 6437 } 6438 6439 // Compare int 6440 6441 /** 6442 * Compares two {@code int} arrays lexicographically. 6443 * 6444 * <p>If the two arrays share a common prefix then the lexicographic 6445 * comparison is the result of comparing two elements, as if by 6446 * {@link Integer#compare(int, int)}, at an index within the respective 6447 * arrays that is the prefix length. 6448 * Otherwise, one array is a proper prefix of the other and, lexicographic 6449 * comparison is the result of comparing the two array lengths. 6450 * (See {@link #mismatch(int[], int[])} for the definition of a common and 6451 * proper prefix.) 6452 * 6453 * <p>A {@code null} array reference is considered lexicographically less 6454 * than a non-{@code null} array reference. Two {@code null} array 6455 * references are considered equal. 6456 * 6457 * <p>The comparison is consistent with {@link #equals(int[], int[]) equals}, 6458 * more specifically the following holds for arrays {@code a} and {@code b}: 6459 * <pre>{@code 6460 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 6461 * }</pre> 6462 * 6463 * @apiNote 6464 * <p>This method behaves as if (for non-{@code null} array references): 6465 * <pre>{@code 6466 * int i = Arrays.mismatch(a, b); 6467 * if (i >= 0 && i < Math.min(a.length, b.length)) 6468 * return Integer.compare(a[i], b[i]); 6469 * return a.length - b.length; 6470 * }</pre> 6471 * 6472 * @param a the first array to compare 6473 * @param b the second array to compare 6474 * @return the value {@code 0} if the first and second array are equal and 6475 * contain the same elements in the same order; 6476 * a value less than {@code 0} if the first array is 6477 * lexicographically less than the second array; and 6478 * a value greater than {@code 0} if the first array is 6479 * lexicographically greater than the second array 6480 * @since 9 6481 */ compare(int[] a, int[] b)6482 public static int compare(int[] a, int[] b) { 6483 if (a == b) 6484 return 0; 6485 if (a == null || b == null) 6486 return a == null ? -1 : 1; 6487 6488 int i = ArraysSupport.mismatch(a, b, 6489 Math.min(a.length, b.length)); 6490 if (i >= 0) { 6491 return Integer.compare(a[i], b[i]); 6492 } 6493 6494 return a.length - b.length; 6495 } 6496 6497 /** 6498 * Compares two {@code int} arrays lexicographically over the specified 6499 * ranges. 6500 * 6501 * <p>If the two arrays, over the specified ranges, share a common prefix 6502 * then the lexicographic comparison is the result of comparing two 6503 * elements, as if by {@link Integer#compare(int, int)}, at a relative index 6504 * within the respective arrays that is the length of the prefix. 6505 * Otherwise, one array is a proper prefix of the other and, lexicographic 6506 * comparison is the result of comparing the two range lengths. 6507 * (See {@link #mismatch(int[], int, int, int[], int, int)} for the 6508 * definition of a common and proper prefix.) 6509 * 6510 * <p>The comparison is consistent with 6511 * {@link #equals(int[], int, int, int[], int, int) equals}, more 6512 * specifically the following holds for arrays {@code a} and {@code b} with 6513 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 6514 * [{@code bFromIndex}, {@code btoIndex}) respectively: 6515 * <pre>{@code 6516 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 6517 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 6518 * }</pre> 6519 * 6520 * @apiNote 6521 * <p>This method behaves as if: 6522 * <pre>{@code 6523 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6524 * b, bFromIndex, bToIndex); 6525 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6526 * return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]); 6527 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6528 * }</pre> 6529 * 6530 * @param a the first array to compare 6531 * @param aFromIndex the index (inclusive) of the first element in the 6532 * first array to be compared 6533 * @param aToIndex the index (exclusive) of the last element in the 6534 * first array to be compared 6535 * @param b the second array to compare 6536 * @param bFromIndex the index (inclusive) of the first element in the 6537 * second array to be compared 6538 * @param bToIndex the index (exclusive) of the last element in the 6539 * second array to be compared 6540 * @return the value {@code 0} if, over the specified ranges, the first and 6541 * second array are equal and contain the same elements in the same 6542 * order; 6543 * a value less than {@code 0} if, over the specified ranges, the 6544 * first array is lexicographically less than the second array; and 6545 * a value greater than {@code 0} if, over the specified ranges, the 6546 * first array is lexicographically greater than the second array 6547 * @throws IllegalArgumentException 6548 * if {@code aFromIndex > aToIndex} or 6549 * if {@code bFromIndex > bToIndex} 6550 * @throws ArrayIndexOutOfBoundsException 6551 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6552 * if {@code bFromIndex < 0 or bToIndex > b.length} 6553 * @throws NullPointerException 6554 * if either array is {@code null} 6555 * @since 9 6556 */ compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)6557 public static int compare(int[] a, int aFromIndex, int aToIndex, 6558 int[] b, int bFromIndex, int bToIndex) { 6559 rangeCheck(a.length, aFromIndex, aToIndex); 6560 rangeCheck(b.length, bFromIndex, bToIndex); 6561 6562 int aLength = aToIndex - aFromIndex; 6563 int bLength = bToIndex - bFromIndex; 6564 int i = ArraysSupport.mismatch(a, aFromIndex, 6565 b, bFromIndex, 6566 Math.min(aLength, bLength)); 6567 if (i >= 0) { 6568 return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]); 6569 } 6570 6571 return aLength - bLength; 6572 } 6573 6574 /** 6575 * Compares two {@code int} arrays lexicographically, numerically treating 6576 * elements as unsigned. 6577 * 6578 * <p>If the two arrays share a common prefix then the lexicographic 6579 * comparison is the result of comparing two elements, as if by 6580 * {@link Integer#compareUnsigned(int, int)}, at an index within the 6581 * respective arrays that is the prefix length. 6582 * Otherwise, one array is a proper prefix of the other and, lexicographic 6583 * comparison is the result of comparing the two array lengths. 6584 * (See {@link #mismatch(int[], int[])} for the definition of a common 6585 * and proper prefix.) 6586 * 6587 * <p>A {@code null} array reference is considered lexicographically less 6588 * than a non-{@code null} array reference. Two {@code null} array 6589 * references are considered equal. 6590 * 6591 * @apiNote 6592 * <p>This method behaves as if (for non-{@code null} array references): 6593 * <pre>{@code 6594 * int i = Arrays.mismatch(a, b); 6595 * if (i >= 0 && i < Math.min(a.length, b.length)) 6596 * return Integer.compareUnsigned(a[i], b[i]); 6597 * return a.length - b.length; 6598 * }</pre> 6599 * 6600 * @param a the first array to compare 6601 * @param b the second array to compare 6602 * @return the value {@code 0} if the first and second array are 6603 * equal and contain the same elements in the same order; 6604 * a value less than {@code 0} if the first array is 6605 * lexicographically less than the second array; and 6606 * a value greater than {@code 0} if the first array is 6607 * lexicographically greater than the second array 6608 * @since 9 6609 */ compareUnsigned(int[] a, int[] b)6610 public static int compareUnsigned(int[] a, int[] b) { 6611 if (a == b) 6612 return 0; 6613 if (a == null || b == null) 6614 return a == null ? -1 : 1; 6615 6616 int i = ArraysSupport.mismatch(a, b, 6617 Math.min(a.length, b.length)); 6618 if (i >= 0) { 6619 return Integer.compareUnsigned(a[i], b[i]); 6620 } 6621 6622 return a.length - b.length; 6623 } 6624 6625 /** 6626 * Compares two {@code int} arrays lexicographically over the specified 6627 * ranges, numerically treating elements as unsigned. 6628 * 6629 * <p>If the two arrays, over the specified ranges, share a common prefix 6630 * then the lexicographic comparison is the result of comparing two 6631 * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a 6632 * relative index within the respective arrays that is the length of the 6633 * prefix. 6634 * Otherwise, one array is a proper prefix of the other and, lexicographic 6635 * comparison is the result of comparing the two range lengths. 6636 * (See {@link #mismatch(int[], int, int, int[], int, int)} for the 6637 * definition of a common and proper prefix.) 6638 * 6639 * @apiNote 6640 * <p>This method behaves as if: 6641 * <pre>{@code 6642 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6643 * b, bFromIndex, bToIndex); 6644 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6645 * return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6646 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6647 * }</pre> 6648 * 6649 * @param a the first array to compare 6650 * @param aFromIndex the index (inclusive) of the first element in the 6651 * first array to be compared 6652 * @param aToIndex the index (exclusive) of the last element in the 6653 * first array to be compared 6654 * @param b the second array to compare 6655 * @param bFromIndex the index (inclusive) of the first element in the 6656 * second array to be compared 6657 * @param bToIndex the index (exclusive) of the last element in the 6658 * second array to be compared 6659 * @return the value {@code 0} if, over the specified ranges, the first and 6660 * second array are equal and contain the same elements in the same 6661 * order; 6662 * a value less than {@code 0} if, over the specified ranges, the 6663 * first array is lexicographically less than the second array; and 6664 * a value greater than {@code 0} if, over the specified ranges, the 6665 * first array is lexicographically greater than the second array 6666 * @throws IllegalArgumentException 6667 * if {@code aFromIndex > aToIndex} or 6668 * if {@code bFromIndex > bToIndex} 6669 * @throws ArrayIndexOutOfBoundsException 6670 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6671 * if {@code bFromIndex < 0 or bToIndex > b.length} 6672 * @throws NullPointerException 6673 * if either array is null 6674 * @since 9 6675 */ compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)6676 public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex, 6677 int[] b, int bFromIndex, int bToIndex) { 6678 rangeCheck(a.length, aFromIndex, aToIndex); 6679 rangeCheck(b.length, bFromIndex, bToIndex); 6680 6681 int aLength = aToIndex - aFromIndex; 6682 int bLength = bToIndex - bFromIndex; 6683 int i = ArraysSupport.mismatch(a, aFromIndex, 6684 b, bFromIndex, 6685 Math.min(aLength, bLength)); 6686 if (i >= 0) { 6687 return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6688 } 6689 6690 return aLength - bLength; 6691 } 6692 6693 // Compare long 6694 6695 /** 6696 * Compares two {@code long} arrays lexicographically. 6697 * 6698 * <p>If the two arrays share a common prefix then the lexicographic 6699 * comparison is the result of comparing two elements, as if by 6700 * {@link Long#compare(long, long)}, at an index within the respective 6701 * arrays that is the prefix length. 6702 * Otherwise, one array is a proper prefix of the other and, lexicographic 6703 * comparison is the result of comparing the two array lengths. 6704 * (See {@link #mismatch(long[], long[])} for the definition of a common and 6705 * proper prefix.) 6706 * 6707 * <p>A {@code null} array reference is considered lexicographically less 6708 * than a non-{@code null} array reference. Two {@code null} array 6709 * references are considered equal. 6710 * 6711 * <p>The comparison is consistent with {@link #equals(long[], long[]) equals}, 6712 * more specifically the following holds for arrays {@code a} and {@code b}: 6713 * <pre>{@code 6714 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 6715 * }</pre> 6716 * 6717 * @apiNote 6718 * <p>This method behaves as if (for non-{@code null} array references): 6719 * <pre>{@code 6720 * int i = Arrays.mismatch(a, b); 6721 * if (i >= 0 && i < Math.min(a.length, b.length)) 6722 * return Long.compare(a[i], b[i]); 6723 * return a.length - b.length; 6724 * }</pre> 6725 * 6726 * @param a the first array to compare 6727 * @param b the second array to compare 6728 * @return the value {@code 0} if the first and second array are equal and 6729 * contain the same elements in the same order; 6730 * a value less than {@code 0} if the first array is 6731 * lexicographically less than the second array; and 6732 * a value greater than {@code 0} if the first array is 6733 * lexicographically greater than the second array 6734 * @since 9 6735 */ compare(long[] a, long[] b)6736 public static int compare(long[] a, long[] b) { 6737 if (a == b) 6738 return 0; 6739 if (a == null || b == null) 6740 return a == null ? -1 : 1; 6741 6742 int i = ArraysSupport.mismatch(a, b, 6743 Math.min(a.length, b.length)); 6744 if (i >= 0) { 6745 return Long.compare(a[i], b[i]); 6746 } 6747 6748 return a.length - b.length; 6749 } 6750 6751 /** 6752 * Compares two {@code long} arrays lexicographically over the specified 6753 * ranges. 6754 * 6755 * <p>If the two arrays, over the specified ranges, share a common prefix 6756 * then the lexicographic comparison is the result of comparing two 6757 * elements, as if by {@link Long#compare(long, long)}, at a relative index 6758 * within the respective arrays that is the length of the prefix. 6759 * Otherwise, one array is a proper prefix of the other and, lexicographic 6760 * comparison is the result of comparing the two range lengths. 6761 * (See {@link #mismatch(long[], int, int, long[], int, int)} for the 6762 * definition of a common and proper prefix.) 6763 * 6764 * <p>The comparison is consistent with 6765 * {@link #equals(long[], int, int, long[], int, int) equals}, more 6766 * specifically the following holds for arrays {@code a} and {@code b} with 6767 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 6768 * [{@code bFromIndex}, {@code btoIndex}) respectively: 6769 * <pre>{@code 6770 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 6771 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 6772 * }</pre> 6773 * 6774 * @apiNote 6775 * <p>This method behaves as if: 6776 * <pre>{@code 6777 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6778 * b, bFromIndex, bToIndex); 6779 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6780 * return Long.compare(a[aFromIndex + i], b[bFromIndex + i]); 6781 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6782 * }</pre> 6783 * 6784 * @param a the first array to compare 6785 * @param aFromIndex the index (inclusive) of the first element in the 6786 * first array to be compared 6787 * @param aToIndex the index (exclusive) of the last element in the 6788 * first array to be compared 6789 * @param b the second array to compare 6790 * @param bFromIndex the index (inclusive) of the first element in the 6791 * second array to be compared 6792 * @param bToIndex the index (exclusive) of the last element in the 6793 * second array to be compared 6794 * @return the value {@code 0} if, over the specified ranges, the first and 6795 * second array are equal and contain the same elements in the same 6796 * order; 6797 * a value less than {@code 0} if, over the specified ranges, the 6798 * first array is lexicographically less than the second array; and 6799 * a value greater than {@code 0} if, over the specified ranges, the 6800 * first array is lexicographically greater than the second array 6801 * @throws IllegalArgumentException 6802 * if {@code aFromIndex > aToIndex} or 6803 * if {@code bFromIndex > bToIndex} 6804 * @throws ArrayIndexOutOfBoundsException 6805 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6806 * if {@code bFromIndex < 0 or bToIndex > b.length} 6807 * @throws NullPointerException 6808 * if either array is {@code null} 6809 * @since 9 6810 */ compare(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)6811 public static int compare(long[] a, int aFromIndex, int aToIndex, 6812 long[] b, int bFromIndex, int bToIndex) { 6813 rangeCheck(a.length, aFromIndex, aToIndex); 6814 rangeCheck(b.length, bFromIndex, bToIndex); 6815 6816 int aLength = aToIndex - aFromIndex; 6817 int bLength = bToIndex - bFromIndex; 6818 int i = ArraysSupport.mismatch(a, aFromIndex, 6819 b, bFromIndex, 6820 Math.min(aLength, bLength)); 6821 if (i >= 0) { 6822 return Long.compare(a[aFromIndex + i], b[bFromIndex + i]); 6823 } 6824 6825 return aLength - bLength; 6826 } 6827 6828 /** 6829 * Compares two {@code long} arrays lexicographically, numerically treating 6830 * elements as unsigned. 6831 * 6832 * <p>If the two arrays share a common prefix then the lexicographic 6833 * comparison is the result of comparing two elements, as if by 6834 * {@link Long#compareUnsigned(long, long)}, at an index within the 6835 * respective arrays that is the prefix length. 6836 * Otherwise, one array is a proper prefix of the other and, lexicographic 6837 * comparison is the result of comparing the two array lengths. 6838 * (See {@link #mismatch(long[], long[])} for the definition of a common 6839 * and proper prefix.) 6840 * 6841 * <p>A {@code null} array reference is considered lexicographically less 6842 * than a non-{@code null} array reference. Two {@code null} array 6843 * references are considered equal. 6844 * 6845 * @apiNote 6846 * <p>This method behaves as if (for non-{@code null} array references): 6847 * <pre>{@code 6848 * int i = Arrays.mismatch(a, b); 6849 * if (i >= 0 && i < Math.min(a.length, b.length)) 6850 * return Long.compareUnsigned(a[i], b[i]); 6851 * return a.length - b.length; 6852 * }</pre> 6853 * 6854 * @param a the first array to compare 6855 * @param b the second array to compare 6856 * @return the value {@code 0} if the first and second array are 6857 * equal and contain the same elements in the same order; 6858 * a value less than {@code 0} if the first array is 6859 * lexicographically less than the second array; and 6860 * a value greater than {@code 0} if the first array is 6861 * lexicographically greater than the second array 6862 * @since 9 6863 */ compareUnsigned(long[] a, long[] b)6864 public static int compareUnsigned(long[] a, long[] b) { 6865 if (a == b) 6866 return 0; 6867 if (a == null || b == null) 6868 return a == null ? -1 : 1; 6869 6870 int i = ArraysSupport.mismatch(a, b, 6871 Math.min(a.length, b.length)); 6872 if (i >= 0) { 6873 return Long.compareUnsigned(a[i], b[i]); 6874 } 6875 6876 return a.length - b.length; 6877 } 6878 6879 /** 6880 * Compares two {@code long} arrays lexicographically over the specified 6881 * ranges, numerically treating elements as unsigned. 6882 * 6883 * <p>If the two arrays, over the specified ranges, share a common prefix 6884 * then the lexicographic comparison is the result of comparing two 6885 * elements, as if by {@link Long#compareUnsigned(long, long)}, at a 6886 * relative index within the respective arrays that is the length of the 6887 * prefix. 6888 * Otherwise, one array is a proper prefix of the other and, lexicographic 6889 * comparison is the result of comparing the two range lengths. 6890 * (See {@link #mismatch(long[], int, int, long[], int, int)} for the 6891 * definition of a common and proper prefix.) 6892 * 6893 * @apiNote 6894 * <p>This method behaves as if: 6895 * <pre>{@code 6896 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 6897 * b, bFromIndex, bToIndex); 6898 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 6899 * return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6900 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 6901 * }</pre> 6902 * 6903 * @param a the first array to compare 6904 * @param aFromIndex the index (inclusive) of the first element in the 6905 * first array to be compared 6906 * @param aToIndex the index (exclusive) of the last element in the 6907 * first array to be compared 6908 * @param b the second array to compare 6909 * @param bFromIndex the index (inclusive) of the first element in the 6910 * second array to be compared 6911 * @param bToIndex the index (exclusive) of the last element in the 6912 * second array to be compared 6913 * @return the value {@code 0} if, over the specified ranges, the first and 6914 * second array are equal and contain the same elements in the same 6915 * order; 6916 * a value less than {@code 0} if, over the specified ranges, the 6917 * first array is lexicographically less than the second array; and 6918 * a value greater than {@code 0} if, over the specified ranges, the 6919 * first array is lexicographically greater than the second array 6920 * @throws IllegalArgumentException 6921 * if {@code aFromIndex > aToIndex} or 6922 * if {@code bFromIndex > bToIndex} 6923 * @throws ArrayIndexOutOfBoundsException 6924 * if {@code aFromIndex < 0 or aToIndex > a.length} or 6925 * if {@code bFromIndex < 0 or bToIndex > b.length} 6926 * @throws NullPointerException 6927 * if either array is null 6928 * @since 9 6929 */ compareUnsigned(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)6930 public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex, 6931 long[] b, int bFromIndex, int bToIndex) { 6932 rangeCheck(a.length, aFromIndex, aToIndex); 6933 rangeCheck(b.length, bFromIndex, bToIndex); 6934 6935 int aLength = aToIndex - aFromIndex; 6936 int bLength = bToIndex - bFromIndex; 6937 int i = ArraysSupport.mismatch(a, aFromIndex, 6938 b, bFromIndex, 6939 Math.min(aLength, bLength)); 6940 if (i >= 0) { 6941 return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]); 6942 } 6943 6944 return aLength - bLength; 6945 } 6946 6947 // Compare float 6948 6949 /** 6950 * Compares two {@code float} arrays lexicographically. 6951 * 6952 * <p>If the two arrays share a common prefix then the lexicographic 6953 * comparison is the result of comparing two elements, as if by 6954 * {@link Float#compare(float, float)}, at an index within the respective 6955 * arrays that is the prefix length. 6956 * Otherwise, one array is a proper prefix of the other and, lexicographic 6957 * comparison is the result of comparing the two array lengths. 6958 * (See {@link #mismatch(float[], float[])} for the definition of a common 6959 * and proper prefix.) 6960 * 6961 * <p>A {@code null} array reference is considered lexicographically less 6962 * than a non-{@code null} array reference. Two {@code null} array 6963 * references are considered equal. 6964 * 6965 * <p>The comparison is consistent with {@link #equals(float[], float[]) equals}, 6966 * more specifically the following holds for arrays {@code a} and {@code b}: 6967 * <pre>{@code 6968 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 6969 * }</pre> 6970 * 6971 * @apiNote 6972 * <p>This method behaves as if (for non-{@code null} array references): 6973 * <pre>{@code 6974 * int i = Arrays.mismatch(a, b); 6975 * if (i >= 0 && i < Math.min(a.length, b.length)) 6976 * return Float.compare(a[i], b[i]); 6977 * return a.length - b.length; 6978 * }</pre> 6979 * 6980 * @param a the first array to compare 6981 * @param b the second array to compare 6982 * @return the value {@code 0} if the first and second array are equal and 6983 * contain the same elements in the same order; 6984 * a value less than {@code 0} if the first array is 6985 * lexicographically less than the second array; and 6986 * a value greater than {@code 0} if the first array is 6987 * lexicographically greater than the second array 6988 * @since 9 6989 */ compare(float[] a, float[] b)6990 public static int compare(float[] a, float[] b) { 6991 if (a == b) 6992 return 0; 6993 if (a == null || b == null) 6994 return a == null ? -1 : 1; 6995 6996 int i = ArraysSupport.mismatch(a, b, 6997 Math.min(a.length, b.length)); 6998 if (i >= 0) { 6999 return Float.compare(a[i], b[i]); 7000 } 7001 7002 return a.length - b.length; 7003 } 7004 7005 /** 7006 * Compares two {@code float} arrays lexicographically over the specified 7007 * ranges. 7008 * 7009 * <p>If the two arrays, over the specified ranges, share a common prefix 7010 * then the lexicographic comparison is the result of comparing two 7011 * elements, as if by {@link Float#compare(float, float)}, at a relative 7012 * index within the respective arrays that is the length of the prefix. 7013 * Otherwise, one array is a proper prefix of the other and, lexicographic 7014 * comparison is the result of comparing the two range lengths. 7015 * (See {@link #mismatch(float[], int, int, float[], int, int)} for the 7016 * definition of a common and proper prefix.) 7017 * 7018 * <p>The comparison is consistent with 7019 * {@link #equals(float[], int, int, float[], int, int) equals}, more 7020 * specifically the following holds for arrays {@code a} and {@code b} with 7021 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 7022 * [{@code bFromIndex}, {@code btoIndex}) respectively: 7023 * <pre>{@code 7024 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 7025 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 7026 * }</pre> 7027 * 7028 * @apiNote 7029 * <p>This method behaves as if: 7030 * <pre>{@code 7031 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 7032 * b, bFromIndex, bToIndex); 7033 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7034 * return Float.compare(a[aFromIndex + i], b[bFromIndex + i]); 7035 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 7036 * }</pre> 7037 * 7038 * @param a the first array to compare 7039 * @param aFromIndex the index (inclusive) of the first element in the 7040 * first array to be compared 7041 * @param aToIndex the index (exclusive) of the last element in the 7042 * first array to be compared 7043 * @param b the second array to compare 7044 * @param bFromIndex the index (inclusive) of the first element in the 7045 * second array to be compared 7046 * @param bToIndex the index (exclusive) of the last element in the 7047 * second array to be compared 7048 * @return the value {@code 0} if, over the specified ranges, the first and 7049 * second array are equal and contain the same elements in the same 7050 * order; 7051 * a value less than {@code 0} if, over the specified ranges, the 7052 * first array is lexicographically less than the second array; and 7053 * a value greater than {@code 0} if, over the specified ranges, the 7054 * first array is lexicographically greater than the second array 7055 * @throws IllegalArgumentException 7056 * if {@code aFromIndex > aToIndex} or 7057 * if {@code bFromIndex > bToIndex} 7058 * @throws ArrayIndexOutOfBoundsException 7059 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7060 * if {@code bFromIndex < 0 or bToIndex > b.length} 7061 * @throws NullPointerException 7062 * if either array is {@code null} 7063 * @since 9 7064 */ compare(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)7065 public static int compare(float[] a, int aFromIndex, int aToIndex, 7066 float[] b, int bFromIndex, int bToIndex) { 7067 rangeCheck(a.length, aFromIndex, aToIndex); 7068 rangeCheck(b.length, bFromIndex, bToIndex); 7069 7070 int aLength = aToIndex - aFromIndex; 7071 int bLength = bToIndex - bFromIndex; 7072 int i = ArraysSupport.mismatch(a, aFromIndex, 7073 b, bFromIndex, 7074 Math.min(aLength, bLength)); 7075 if (i >= 0) { 7076 return Float.compare(a[aFromIndex + i], b[bFromIndex + i]); 7077 } 7078 7079 return aLength - bLength; 7080 } 7081 7082 // Compare double 7083 7084 /** 7085 * Compares two {@code double} arrays lexicographically. 7086 * 7087 * <p>If the two arrays share a common prefix then the lexicographic 7088 * comparison is the result of comparing two elements, as if by 7089 * {@link Double#compare(double, double)}, at an index within the respective 7090 * arrays that is the prefix length. 7091 * Otherwise, one array is a proper prefix of the other and, lexicographic 7092 * comparison is the result of comparing the two array lengths. 7093 * (See {@link #mismatch(double[], double[])} for the definition of a common 7094 * and proper prefix.) 7095 * 7096 * <p>A {@code null} array reference is considered lexicographically less 7097 * than a non-{@code null} array reference. Two {@code null} array 7098 * references are considered equal. 7099 * 7100 * <p>The comparison is consistent with {@link #equals(double[], double[]) equals}, 7101 * more specifically the following holds for arrays {@code a} and {@code b}: 7102 * <pre>{@code 7103 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 7104 * }</pre> 7105 * 7106 * @apiNote 7107 * <p>This method behaves as if (for non-{@code null} array references): 7108 * <pre>{@code 7109 * int i = Arrays.mismatch(a, b); 7110 * if (i >= 0 && i < Math.min(a.length, b.length)) 7111 * return Double.compare(a[i], b[i]); 7112 * return a.length - b.length; 7113 * }</pre> 7114 * 7115 * @param a the first array to compare 7116 * @param b the second array to compare 7117 * @return the value {@code 0} if the first and second array are equal and 7118 * contain the same elements in the same order; 7119 * a value less than {@code 0} if the first array is 7120 * lexicographically less than the second array; and 7121 * a value greater than {@code 0} if the first array is 7122 * lexicographically greater than the second array 7123 * @since 9 7124 */ compare(double[] a, double[] b)7125 public static int compare(double[] a, double[] b) { 7126 if (a == b) 7127 return 0; 7128 if (a == null || b == null) 7129 return a == null ? -1 : 1; 7130 7131 int i = ArraysSupport.mismatch(a, b, 7132 Math.min(a.length, b.length)); 7133 if (i >= 0) { 7134 return Double.compare(a[i], b[i]); 7135 } 7136 7137 return a.length - b.length; 7138 } 7139 7140 /** 7141 * Compares two {@code double} arrays lexicographically over the specified 7142 * ranges. 7143 * 7144 * <p>If the two arrays, over the specified ranges, share a common prefix 7145 * then the lexicographic comparison is the result of comparing two 7146 * elements, as if by {@link Double#compare(double, double)}, at a relative 7147 * index within the respective arrays that is the length of the prefix. 7148 * Otherwise, one array is a proper prefix of the other and, lexicographic 7149 * comparison is the result of comparing the two range lengths. 7150 * (See {@link #mismatch(double[], int, int, double[], int, int)} for the 7151 * definition of a common and proper prefix.) 7152 * 7153 * <p>The comparison is consistent with 7154 * {@link #equals(double[], int, int, double[], int, int) equals}, more 7155 * specifically the following holds for arrays {@code a} and {@code b} with 7156 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 7157 * [{@code bFromIndex}, {@code btoIndex}) respectively: 7158 * <pre>{@code 7159 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 7160 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 7161 * }</pre> 7162 * 7163 * @apiNote 7164 * <p>This method behaves as if: 7165 * <pre>{@code 7166 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 7167 * b, bFromIndex, bToIndex); 7168 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7169 * return Double.compare(a[aFromIndex + i], b[bFromIndex + i]); 7170 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 7171 * }</pre> 7172 * 7173 * @param a the first array to compare 7174 * @param aFromIndex the index (inclusive) of the first element in the 7175 * first array to be compared 7176 * @param aToIndex the index (exclusive) of the last element in the 7177 * first array to be compared 7178 * @param b the second array to compare 7179 * @param bFromIndex the index (inclusive) of the first element in the 7180 * second array to be compared 7181 * @param bToIndex the index (exclusive) of the last element in the 7182 * second array to be compared 7183 * @return the value {@code 0} if, over the specified ranges, the first and 7184 * second array are equal and contain the same elements in the same 7185 * order; 7186 * a value less than {@code 0} if, over the specified ranges, the 7187 * first array is lexicographically less than the second array; and 7188 * a value greater than {@code 0} if, over the specified ranges, the 7189 * first array is lexicographically greater than the second array 7190 * @throws IllegalArgumentException 7191 * if {@code aFromIndex > aToIndex} or 7192 * if {@code bFromIndex > bToIndex} 7193 * @throws ArrayIndexOutOfBoundsException 7194 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7195 * if {@code bFromIndex < 0 or bToIndex > b.length} 7196 * @throws NullPointerException 7197 * if either array is {@code null} 7198 * @since 9 7199 */ compare(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)7200 public static int compare(double[] a, int aFromIndex, int aToIndex, 7201 double[] b, int bFromIndex, int bToIndex) { 7202 rangeCheck(a.length, aFromIndex, aToIndex); 7203 rangeCheck(b.length, bFromIndex, bToIndex); 7204 7205 int aLength = aToIndex - aFromIndex; 7206 int bLength = bToIndex - bFromIndex; 7207 int i = ArraysSupport.mismatch(a, aFromIndex, 7208 b, bFromIndex, 7209 Math.min(aLength, bLength)); 7210 if (i >= 0) { 7211 return Double.compare(a[aFromIndex + i], b[bFromIndex + i]); 7212 } 7213 7214 return aLength - bLength; 7215 } 7216 7217 // Compare objects 7218 7219 /** 7220 * Compares two {@code Object} arrays, within comparable elements, 7221 * lexicographically. 7222 * 7223 * <p>If the two arrays share a common prefix then the lexicographic 7224 * comparison is the result of comparing two elements of type {@code T} at 7225 * an index {@code i} within the respective arrays that is the prefix 7226 * length, as if by: 7227 * <pre>{@code 7228 * Comparator.nullsFirst(Comparator.<T>naturalOrder()). 7229 * compare(a[i], b[i]) 7230 * }</pre> 7231 * Otherwise, one array is a proper prefix of the other and, lexicographic 7232 * comparison is the result of comparing the two array lengths. 7233 * (See {@link #mismatch(Object[], Object[])} for the definition of a common 7234 * and proper prefix.) 7235 * 7236 * <p>A {@code null} array reference is considered lexicographically less 7237 * than a non-{@code null} array reference. Two {@code null} array 7238 * references are considered equal. 7239 * A {@code null} array element is considered lexicographically than a 7240 * non-{@code null} array element. Two {@code null} array elements are 7241 * considered equal. 7242 * 7243 * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals}, 7244 * more specifically the following holds for arrays {@code a} and {@code b}: 7245 * <pre>{@code 7246 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0) 7247 * }</pre> 7248 * 7249 * @apiNote 7250 * <p>This method behaves as if (for non-{@code null} array references 7251 * and elements): 7252 * <pre>{@code 7253 * int i = Arrays.mismatch(a, b); 7254 * if (i >= 0 && i < Math.min(a.length, b.length)) 7255 * return a[i].compareTo(b[i]); 7256 * return a.length - b.length; 7257 * }</pre> 7258 * 7259 * @param a the first array to compare 7260 * @param b the second array to compare 7261 * @param <T> the type of comparable array elements 7262 * @return the value {@code 0} if the first and second array are equal and 7263 * contain the same elements in the same order; 7264 * a value less than {@code 0} if the first array is 7265 * lexicographically less than the second array; and 7266 * a value greater than {@code 0} if the first array is 7267 * lexicographically greater than the second array 7268 * @since 9 7269 */ compare(T[] a, T[] b)7270 public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) { 7271 if (a == b) 7272 return 0; 7273 // A null array is less than a non-null array 7274 if (a == null || b == null) 7275 return a == null ? -1 : 1; 7276 7277 int length = Math.min(a.length, b.length); 7278 for (int i = 0; i < length; i++) { 7279 T oa = a[i]; 7280 T ob = b[i]; 7281 if (oa != ob) { 7282 // A null element is less than a non-null element 7283 if (oa == null || ob == null) 7284 return oa == null ? -1 : 1; 7285 int v = oa.compareTo(ob); 7286 if (v != 0) { 7287 return v; 7288 } 7289 } 7290 } 7291 7292 return a.length - b.length; 7293 } 7294 7295 /** 7296 * Compares two {@code Object} arrays lexicographically over the specified 7297 * ranges. 7298 * 7299 * <p>If the two arrays, over the specified ranges, share a common prefix 7300 * then the lexicographic comparison is the result of comparing two 7301 * elements of type {@code T} at a relative index {@code i} within the 7302 * respective arrays that is the prefix length, as if by: 7303 * <pre>{@code 7304 * Comparator.nullsFirst(Comparator.<T>naturalOrder()). 7305 * compare(a[aFromIndex + i, b[bFromIndex + i]) 7306 * }</pre> 7307 * Otherwise, one array is a proper prefix of the other and, lexicographic 7308 * comparison is the result of comparing the two range lengths. 7309 * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the 7310 * definition of a common and proper prefix.) 7311 * 7312 * <p>The comparison is consistent with 7313 * {@link #equals(Object[], int, int, Object[], int, int) equals}, more 7314 * specifically the following holds for arrays {@code a} and {@code b} with 7315 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and 7316 * [{@code bFromIndex}, {@code btoIndex}) respectively: 7317 * <pre>{@code 7318 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 7319 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0) 7320 * }</pre> 7321 * 7322 * @apiNote 7323 * <p>This method behaves as if (for non-{@code null} array elements): 7324 * <pre>{@code 7325 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 7326 * b, bFromIndex, bToIndex); 7327 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7328 * return a[aFromIndex + i].compareTo(b[bFromIndex + i]); 7329 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 7330 * }</pre> 7331 * 7332 * @param a the first array to compare 7333 * @param aFromIndex the index (inclusive) of the first element in the 7334 * first array to be compared 7335 * @param aToIndex the index (exclusive) of the last element in the 7336 * first array to be compared 7337 * @param b the second array to compare 7338 * @param bFromIndex the index (inclusive) of the first element in the 7339 * second array to be compared 7340 * @param bToIndex the index (exclusive) of the last element in the 7341 * second array to be compared 7342 * @param <T> the type of comparable array elements 7343 * @return the value {@code 0} if, over the specified ranges, the first and 7344 * second array are equal and contain the same elements in the same 7345 * order; 7346 * a value less than {@code 0} if, over the specified ranges, the 7347 * first array is lexicographically less than the second array; and 7348 * a value greater than {@code 0} if, over the specified ranges, the 7349 * first array is lexicographically greater than the second array 7350 * @throws IllegalArgumentException 7351 * if {@code aFromIndex > aToIndex} or 7352 * if {@code bFromIndex > bToIndex} 7353 * @throws ArrayIndexOutOfBoundsException 7354 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7355 * if {@code bFromIndex < 0 or bToIndex > b.length} 7356 * @throws NullPointerException 7357 * if either array is {@code null} 7358 * @since 9 7359 */ compare( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)7360 public static <T extends Comparable<? super T>> int compare( 7361 T[] a, int aFromIndex, int aToIndex, 7362 T[] b, int bFromIndex, int bToIndex) { 7363 rangeCheck(a.length, aFromIndex, aToIndex); 7364 rangeCheck(b.length, bFromIndex, bToIndex); 7365 7366 int aLength = aToIndex - aFromIndex; 7367 int bLength = bToIndex - bFromIndex; 7368 int length = Math.min(aLength, bLength); 7369 for (int i = 0; i < length; i++) { 7370 T oa = a[aFromIndex++]; 7371 T ob = b[bFromIndex++]; 7372 if (oa != ob) { 7373 if (oa == null || ob == null) 7374 return oa == null ? -1 : 1; 7375 int v = oa.compareTo(ob); 7376 if (v != 0) { 7377 return v; 7378 } 7379 } 7380 } 7381 7382 return aLength - bLength; 7383 } 7384 7385 /** 7386 * Compares two {@code Object} arrays lexicographically using a specified 7387 * comparator. 7388 * 7389 * <p>If the two arrays share a common prefix then the lexicographic 7390 * comparison is the result of comparing with the specified comparator two 7391 * elements at an index within the respective arrays that is the prefix 7392 * length. 7393 * Otherwise, one array is a proper prefix of the other and, lexicographic 7394 * comparison is the result of comparing the two array lengths. 7395 * (See {@link #mismatch(Object[], Object[])} for the definition of a common 7396 * and proper prefix.) 7397 * 7398 * <p>A {@code null} array reference is considered lexicographically less 7399 * than a non-{@code null} array reference. Two {@code null} array 7400 * references are considered equal. 7401 * 7402 * @apiNote 7403 * <p>This method behaves as if (for non-{@code null} array references): 7404 * <pre>{@code 7405 * int i = Arrays.mismatch(a, b, cmp); 7406 * if (i >= 0 && i < Math.min(a.length, b.length)) 7407 * return cmp.compare(a[i], b[i]); 7408 * return a.length - b.length; 7409 * }</pre> 7410 * 7411 * @param a the first array to compare 7412 * @param b the second array to compare 7413 * @param cmp the comparator to compare array elements 7414 * @param <T> the type of array elements 7415 * @return the value {@code 0} if the first and second array are equal and 7416 * contain the same elements in the same order; 7417 * a value less than {@code 0} if the first array is 7418 * lexicographically less than the second array; and 7419 * a value greater than {@code 0} if the first array is 7420 * lexicographically greater than the second array 7421 * @throws NullPointerException if the comparator is {@code null} 7422 * @since 9 7423 */ compare(T[] a, T[] b, Comparator<? super T> cmp)7424 public static <T> int compare(T[] a, T[] b, 7425 Comparator<? super T> cmp) { 7426 Objects.requireNonNull(cmp); 7427 if (a == b) 7428 return 0; 7429 if (a == null || b == null) 7430 return a == null ? -1 : 1; 7431 7432 int length = Math.min(a.length, b.length); 7433 for (int i = 0; i < length; i++) { 7434 T oa = a[i]; 7435 T ob = b[i]; 7436 if (oa != ob) { 7437 // Null-value comparison is deferred to the comparator 7438 int v = cmp.compare(oa, ob); 7439 if (v != 0) { 7440 return v; 7441 } 7442 } 7443 } 7444 7445 return a.length - b.length; 7446 } 7447 7448 /** 7449 * Compares two {@code Object} arrays lexicographically over the specified 7450 * ranges. 7451 * 7452 * <p>If the two arrays, over the specified ranges, share a common prefix 7453 * then the lexicographic comparison is the result of comparing with the 7454 * specified comparator two elements at a relative index within the 7455 * respective arrays that is the prefix length. 7456 * Otherwise, one array is a proper prefix of the other and, lexicographic 7457 * comparison is the result of comparing the two range lengths. 7458 * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the 7459 * definition of a common and proper prefix.) 7460 * 7461 * @apiNote 7462 * <p>This method behaves as if (for non-{@code null} array elements): 7463 * <pre>{@code 7464 * int i = Arrays.mismatch(a, aFromIndex, aToIndex, 7465 * b, bFromIndex, bToIndex, cmp); 7466 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7467 * return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]); 7468 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex); 7469 * }</pre> 7470 * 7471 * @param a the first array to compare 7472 * @param aFromIndex the index (inclusive) of the first element in the 7473 * first array to be compared 7474 * @param aToIndex the index (exclusive) of the last element in the 7475 * first array to be compared 7476 * @param b the second array to compare 7477 * @param bFromIndex the index (inclusive) of the first element in the 7478 * second array to be compared 7479 * @param bToIndex the index (exclusive) of the last element in the 7480 * second array to be compared 7481 * @param cmp the comparator to compare array elements 7482 * @param <T> the type of array elements 7483 * @return the value {@code 0} if, over the specified ranges, the first and 7484 * second array are equal and contain the same elements in the same 7485 * order; 7486 * a value less than {@code 0} if, over the specified ranges, the 7487 * first array is lexicographically less than the second array; and 7488 * a value greater than {@code 0} if, over the specified ranges, the 7489 * first array is lexicographically greater than the second array 7490 * @throws IllegalArgumentException 7491 * if {@code aFromIndex > aToIndex} or 7492 * if {@code bFromIndex > bToIndex} 7493 * @throws ArrayIndexOutOfBoundsException 7494 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7495 * if {@code bFromIndex < 0 or bToIndex > b.length} 7496 * @throws NullPointerException 7497 * if either array or the comparator is {@code null} 7498 * @since 9 7499 */ compare( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)7500 public static <T> int compare( 7501 T[] a, int aFromIndex, int aToIndex, 7502 T[] b, int bFromIndex, int bToIndex, 7503 Comparator<? super T> cmp) { 7504 Objects.requireNonNull(cmp); 7505 rangeCheck(a.length, aFromIndex, aToIndex); 7506 rangeCheck(b.length, bFromIndex, bToIndex); 7507 7508 int aLength = aToIndex - aFromIndex; 7509 int bLength = bToIndex - bFromIndex; 7510 int length = Math.min(aLength, bLength); 7511 for (int i = 0; i < length; i++) { 7512 T oa = a[aFromIndex++]; 7513 T ob = b[bFromIndex++]; 7514 if (oa != ob) { 7515 // Null-value comparison is deferred to the comparator 7516 int v = cmp.compare(oa, ob); 7517 if (v != 0) { 7518 return v; 7519 } 7520 } 7521 } 7522 7523 return aLength - bLength; 7524 } 7525 7526 7527 // Mismatch methods 7528 7529 // Mismatch boolean 7530 7531 /** 7532 * Finds and returns the index of the first mismatch between two 7533 * {@code boolean} arrays, otherwise return -1 if no mismatch is found. The 7534 * index will be in the range of 0 (inclusive) up to the length (inclusive) 7535 * of the smaller array. 7536 * 7537 * <p>If the two arrays share a common prefix then the returned index is the 7538 * length of the common prefix and it follows that there is a mismatch 7539 * between the two elements at that index within the respective arrays. 7540 * If one array is a proper prefix of the other then the returned index is 7541 * the length of the smaller array and it follows that the index is only 7542 * valid for the larger array. 7543 * Otherwise, there is no mismatch. 7544 * 7545 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 7546 * prefix of length {@code pl} if the following expression is true: 7547 * <pre>{@code 7548 * pl >= 0 && 7549 * pl < Math.min(a.length, b.length) && 7550 * Arrays.equals(a, 0, pl, b, 0, pl) && 7551 * a[pl] != b[pl] 7552 * }</pre> 7553 * Note that a common prefix length of {@code 0} indicates that the first 7554 * elements from each array mismatch. 7555 * 7556 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 7557 * prefix if the following expression is true: 7558 * <pre>{@code 7559 * a.length != b.length && 7560 * Arrays.equals(a, 0, Math.min(a.length, b.length), 7561 * b, 0, Math.min(a.length, b.length)) 7562 * }</pre> 7563 * 7564 * @param a the first array to be tested for a mismatch 7565 * @param b the second array to be tested for a mismatch 7566 * @return the index of the first mismatch between the two arrays, 7567 * otherwise {@code -1}. 7568 * @throws NullPointerException 7569 * if either array is {@code null} 7570 * @since 9 7571 */ mismatch(boolean[] a, boolean[] b)7572 public static int mismatch(boolean[] a, boolean[] b) { 7573 int length = Math.min(a.length, b.length); // Check null array refs 7574 if (a == b) 7575 return -1; 7576 7577 int i = ArraysSupport.mismatch(a, b, length); 7578 return (i < 0 && a.length != b.length) ? length : i; 7579 } 7580 7581 /** 7582 * Finds and returns the relative index of the first mismatch between two 7583 * {@code boolean} arrays over the specified ranges, otherwise return -1 if 7584 * no mismatch is found. The index will be in the range of 0 (inclusive) up 7585 * to the length (inclusive) of the smaller range. 7586 * 7587 * <p>If the two arrays, over the specified ranges, share a common prefix 7588 * then the returned relative index is the length of the common prefix and 7589 * it follows that there is a mismatch between the two elements at that 7590 * relative index within the respective arrays. 7591 * If one array is a proper prefix of the other, over the specified ranges, 7592 * then the returned relative index is the length of the smaller range and 7593 * it follows that the relative index is only valid for the array with the 7594 * larger range. 7595 * Otherwise, there is no mismatch. 7596 * 7597 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7598 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7599 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 7600 * prefix of length {@code pl} if the following expression is true: 7601 * <pre>{@code 7602 * pl >= 0 && 7603 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 7604 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 7605 * a[aFromIndex + pl] != b[bFromIndex + pl] 7606 * }</pre> 7607 * Note that a common prefix length of {@code 0} indicates that the first 7608 * elements from each array mismatch. 7609 * 7610 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7611 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7612 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 7613 * if the following expression is true: 7614 * <pre>{@code 7615 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 7616 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 7617 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7618 * }</pre> 7619 * 7620 * @param a the first array to be tested for a mismatch 7621 * @param aFromIndex the index (inclusive) of the first element in the 7622 * first array to be tested 7623 * @param aToIndex the index (exclusive) of the last element in the 7624 * first array to be tested 7625 * @param b the second array to be tested for a mismatch 7626 * @param bFromIndex the index (inclusive) of the first element in the 7627 * second array to be tested 7628 * @param bToIndex the index (exclusive) of the last element in the 7629 * second array to be tested 7630 * @return the relative index of the first mismatch between the two arrays 7631 * over the specified ranges, otherwise {@code -1}. 7632 * @throws IllegalArgumentException 7633 * if {@code aFromIndex > aToIndex} or 7634 * if {@code bFromIndex > bToIndex} 7635 * @throws ArrayIndexOutOfBoundsException 7636 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7637 * if {@code bFromIndex < 0 or bToIndex > b.length} 7638 * @throws NullPointerException 7639 * if either array is {@code null} 7640 * @since 9 7641 */ mismatch(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)7642 public static int mismatch(boolean[] a, int aFromIndex, int aToIndex, 7643 boolean[] b, int bFromIndex, int bToIndex) { 7644 rangeCheck(a.length, aFromIndex, aToIndex); 7645 rangeCheck(b.length, bFromIndex, bToIndex); 7646 7647 int aLength = aToIndex - aFromIndex; 7648 int bLength = bToIndex - bFromIndex; 7649 int length = Math.min(aLength, bLength); 7650 int i = ArraysSupport.mismatch(a, aFromIndex, 7651 b, bFromIndex, 7652 length); 7653 return (i < 0 && aLength != bLength) ? length : i; 7654 } 7655 7656 // Mismatch byte 7657 7658 /** 7659 * Finds and returns the index of the first mismatch between two {@code byte} 7660 * arrays, otherwise return -1 if no mismatch is found. The index will be 7661 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 7662 * array. 7663 * 7664 * <p>If the two arrays share a common prefix then the returned index is the 7665 * length of the common prefix and it follows that there is a mismatch 7666 * between the two elements at that index within the respective arrays. 7667 * If one array is a proper prefix of the other then the returned index is 7668 * the length of the smaller array and it follows that the index is only 7669 * valid for the larger array. 7670 * Otherwise, there is no mismatch. 7671 * 7672 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 7673 * prefix of length {@code pl} if the following expression is true: 7674 * <pre>{@code 7675 * pl >= 0 && 7676 * pl < Math.min(a.length, b.length) && 7677 * Arrays.equals(a, 0, pl, b, 0, pl) && 7678 * a[pl] != b[pl] 7679 * }</pre> 7680 * Note that a common prefix length of {@code 0} indicates that the first 7681 * elements from each array mismatch. 7682 * 7683 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 7684 * prefix if the following expression is true: 7685 * <pre>{@code 7686 * a.length != b.length && 7687 * Arrays.equals(a, 0, Math.min(a.length, b.length), 7688 * b, 0, Math.min(a.length, b.length)) 7689 * }</pre> 7690 * 7691 * @param a the first array to be tested for a mismatch 7692 * @param b the second array to be tested for a mismatch 7693 * @return the index of the first mismatch between the two arrays, 7694 * otherwise {@code -1}. 7695 * @throws NullPointerException 7696 * if either array is {@code null} 7697 * @since 9 7698 */ mismatch(byte[] a, byte[] b)7699 public static int mismatch(byte[] a, byte[] b) { 7700 int length = Math.min(a.length, b.length); // Check null array refs 7701 if (a == b) 7702 return -1; 7703 7704 int i = ArraysSupport.mismatch(a, b, length); 7705 return (i < 0 && a.length != b.length) ? length : i; 7706 } 7707 7708 /** 7709 * Finds and returns the relative index of the first mismatch between two 7710 * {@code byte} arrays over the specified ranges, otherwise return -1 if no 7711 * mismatch is found. The index will be in the range of 0 (inclusive) up to 7712 * the length (inclusive) of the smaller range. 7713 * 7714 * <p>If the two arrays, over the specified ranges, share a common prefix 7715 * then the returned relative index is the length of the common prefix and 7716 * it follows that there is a mismatch between the two elements at that 7717 * relative index within the respective arrays. 7718 * If one array is a proper prefix of the other, over the specified ranges, 7719 * then the returned relative index is the length of the smaller range and 7720 * it follows that the relative index is only valid for the array with the 7721 * larger range. 7722 * Otherwise, there is no mismatch. 7723 * 7724 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7725 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7726 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 7727 * prefix of length {@code pl} if the following expression is true: 7728 * <pre>{@code 7729 * pl >= 0 && 7730 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 7731 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 7732 * a[aFromIndex + pl] != b[bFromIndex + pl] 7733 * }</pre> 7734 * Note that a common prefix length of {@code 0} indicates that the first 7735 * elements from each array mismatch. 7736 * 7737 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7738 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7739 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 7740 * if the following expression is true: 7741 * <pre>{@code 7742 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 7743 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 7744 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7745 * }</pre> 7746 * 7747 * @param a the first array to be tested for a mismatch 7748 * @param aFromIndex the index (inclusive) of the first element in the 7749 * first array to be tested 7750 * @param aToIndex the index (exclusive) of the last element in the 7751 * first array to be tested 7752 * @param b the second array to be tested for a mismatch 7753 * @param bFromIndex the index (inclusive) of the first element in the 7754 * second array to be tested 7755 * @param bToIndex the index (exclusive) of the last element in the 7756 * second array to be tested 7757 * @return the relative index of the first mismatch between the two arrays 7758 * over the specified ranges, otherwise {@code -1}. 7759 * @throws IllegalArgumentException 7760 * if {@code aFromIndex > aToIndex} or 7761 * if {@code bFromIndex > bToIndex} 7762 * @throws ArrayIndexOutOfBoundsException 7763 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7764 * if {@code bFromIndex < 0 or bToIndex > b.length} 7765 * @throws NullPointerException 7766 * if either array is {@code null} 7767 * @since 9 7768 */ mismatch(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)7769 public static int mismatch(byte[] a, int aFromIndex, int aToIndex, 7770 byte[] b, int bFromIndex, int bToIndex) { 7771 rangeCheck(a.length, aFromIndex, aToIndex); 7772 rangeCheck(b.length, bFromIndex, bToIndex); 7773 7774 int aLength = aToIndex - aFromIndex; 7775 int bLength = bToIndex - bFromIndex; 7776 int length = Math.min(aLength, bLength); 7777 int i = ArraysSupport.mismatch(a, aFromIndex, 7778 b, bFromIndex, 7779 length); 7780 return (i < 0 && aLength != bLength) ? length : i; 7781 } 7782 7783 // Mismatch char 7784 7785 /** 7786 * Finds and returns the index of the first mismatch between two {@code char} 7787 * arrays, otherwise return -1 if no mismatch is found. The index will be 7788 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 7789 * array. 7790 * 7791 * <p>If the two arrays share a common prefix then the returned index is the 7792 * length of the common prefix and it follows that there is a mismatch 7793 * between the two elements at that index within the respective arrays. 7794 * If one array is a proper prefix of the other then the returned index is 7795 * the length of the smaller array and it follows that the index is only 7796 * valid for the larger array. 7797 * Otherwise, there is no mismatch. 7798 * 7799 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 7800 * prefix of length {@code pl} if the following expression is true: 7801 * <pre>{@code 7802 * pl >= 0 && 7803 * pl < Math.min(a.length, b.length) && 7804 * Arrays.equals(a, 0, pl, b, 0, pl) && 7805 * a[pl] != b[pl] 7806 * }</pre> 7807 * Note that a common prefix length of {@code 0} indicates that the first 7808 * elements from each array mismatch. 7809 * 7810 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 7811 * prefix if the following expression is true: 7812 * <pre>{@code 7813 * a.length != b.length && 7814 * Arrays.equals(a, 0, Math.min(a.length, b.length), 7815 * b, 0, Math.min(a.length, b.length)) 7816 * }</pre> 7817 * 7818 * @param a the first array to be tested for a mismatch 7819 * @param b the second array to be tested for a mismatch 7820 * @return the index of the first mismatch between the two arrays, 7821 * otherwise {@code -1}. 7822 * @throws NullPointerException 7823 * if either array is {@code null} 7824 * @since 9 7825 */ mismatch(char[] a, char[] b)7826 public static int mismatch(char[] a, char[] b) { 7827 int length = Math.min(a.length, b.length); // Check null array refs 7828 if (a == b) 7829 return -1; 7830 7831 int i = ArraysSupport.mismatch(a, b, length); 7832 return (i < 0 && a.length != b.length) ? length : i; 7833 } 7834 7835 /** 7836 * Finds and returns the relative index of the first mismatch between two 7837 * {@code char} arrays over the specified ranges, otherwise return -1 if no 7838 * mismatch is found. The index will be in the range of 0 (inclusive) up to 7839 * the length (inclusive) of the smaller range. 7840 * 7841 * <p>If the two arrays, over the specified ranges, share a common prefix 7842 * then the returned relative index is the length of the common prefix and 7843 * it follows that there is a mismatch between the two elements at that 7844 * relative index within the respective arrays. 7845 * If one array is a proper prefix of the other, over the specified ranges, 7846 * then the returned relative index is the length of the smaller range and 7847 * it follows that the relative index is only valid for the array with the 7848 * larger range. 7849 * Otherwise, there is no mismatch. 7850 * 7851 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7852 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7853 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 7854 * prefix of length {@code pl} if the following expression is true: 7855 * <pre>{@code 7856 * pl >= 0 && 7857 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 7858 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 7859 * a[aFromIndex + pl] != b[bFromIndex + pl] 7860 * }</pre> 7861 * Note that a common prefix length of {@code 0} indicates that the first 7862 * elements from each array mismatch. 7863 * 7864 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7865 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7866 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 7867 * if the following expression is true: 7868 * <pre>{@code 7869 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 7870 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 7871 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7872 * }</pre> 7873 * 7874 * @param a the first array to be tested for a mismatch 7875 * @param aFromIndex the index (inclusive) of the first element in the 7876 * first array to be tested 7877 * @param aToIndex the index (exclusive) of the last element in the 7878 * first array to be tested 7879 * @param b the second array to be tested for a mismatch 7880 * @param bFromIndex the index (inclusive) of the first element in the 7881 * second array to be tested 7882 * @param bToIndex the index (exclusive) of the last element in the 7883 * second array to be tested 7884 * @return the relative index of the first mismatch between the two arrays 7885 * over the specified ranges, otherwise {@code -1}. 7886 * @throws IllegalArgumentException 7887 * if {@code aFromIndex > aToIndex} or 7888 * if {@code bFromIndex > bToIndex} 7889 * @throws ArrayIndexOutOfBoundsException 7890 * if {@code aFromIndex < 0 or aToIndex > a.length} or 7891 * if {@code bFromIndex < 0 or bToIndex > b.length} 7892 * @throws NullPointerException 7893 * if either array is {@code null} 7894 * @since 9 7895 */ mismatch(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)7896 public static int mismatch(char[] a, int aFromIndex, int aToIndex, 7897 char[] b, int bFromIndex, int bToIndex) { 7898 rangeCheck(a.length, aFromIndex, aToIndex); 7899 rangeCheck(b.length, bFromIndex, bToIndex); 7900 7901 int aLength = aToIndex - aFromIndex; 7902 int bLength = bToIndex - bFromIndex; 7903 int length = Math.min(aLength, bLength); 7904 int i = ArraysSupport.mismatch(a, aFromIndex, 7905 b, bFromIndex, 7906 length); 7907 return (i < 0 && aLength != bLength) ? length : i; 7908 } 7909 7910 // Mismatch short 7911 7912 /** 7913 * Finds and returns the index of the first mismatch between two {@code short} 7914 * arrays, otherwise return -1 if no mismatch is found. The index will be 7915 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 7916 * array. 7917 * 7918 * <p>If the two arrays share a common prefix then the returned index is the 7919 * length of the common prefix and it follows that there is a mismatch 7920 * between the two elements at that index within the respective arrays. 7921 * If one array is a proper prefix of the other then the returned index is 7922 * the length of the smaller array and it follows that the index is only 7923 * valid for the larger array. 7924 * Otherwise, there is no mismatch. 7925 * 7926 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 7927 * prefix of length {@code pl} if the following expression is true: 7928 * <pre>{@code 7929 * pl >= 0 && 7930 * pl < Math.min(a.length, b.length) && 7931 * Arrays.equals(a, 0, pl, b, 0, pl) && 7932 * a[pl] != b[pl] 7933 * }</pre> 7934 * Note that a common prefix length of {@code 0} indicates that the first 7935 * elements from each array mismatch. 7936 * 7937 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 7938 * prefix if the following expression is true: 7939 * <pre>{@code 7940 * a.length != b.length && 7941 * Arrays.equals(a, 0, Math.min(a.length, b.length), 7942 * b, 0, Math.min(a.length, b.length)) 7943 * }</pre> 7944 * 7945 * @param a the first array to be tested for a mismatch 7946 * @param b the second array to be tested for a mismatch 7947 * @return the index of the first mismatch between the two arrays, 7948 * otherwise {@code -1}. 7949 * @throws NullPointerException 7950 * if either array is {@code null} 7951 * @since 9 7952 */ mismatch(short[] a, short[] b)7953 public static int mismatch(short[] a, short[] b) { 7954 int length = Math.min(a.length, b.length); // Check null array refs 7955 if (a == b) 7956 return -1; 7957 7958 int i = ArraysSupport.mismatch(a, b, length); 7959 return (i < 0 && a.length != b.length) ? length : i; 7960 } 7961 7962 /** 7963 * Finds and returns the relative index of the first mismatch between two 7964 * {@code short} arrays over the specified ranges, otherwise return -1 if no 7965 * mismatch is found. The index will be in the range of 0 (inclusive) up to 7966 * the length (inclusive) of the smaller range. 7967 * 7968 * <p>If the two arrays, over the specified ranges, share a common prefix 7969 * then the returned relative index is the length of the common prefix and 7970 * it follows that there is a mismatch between the two elements at that 7971 * relative index within the respective arrays. 7972 * If one array is a proper prefix of the other, over the specified ranges, 7973 * then the returned relative index is the length of the smaller range and 7974 * it follows that the relative index is only valid for the array with the 7975 * larger range. 7976 * Otherwise, there is no mismatch. 7977 * 7978 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7979 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7980 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 7981 * prefix of length {@code pl} if the following expression is true: 7982 * <pre>{@code 7983 * pl >= 0 && 7984 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 7985 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 7986 * a[aFromIndex + pl] != b[bFromIndex + pl] 7987 * }</pre> 7988 * Note that a common prefix length of {@code 0} indicates that the first 7989 * elements from each array mismatch. 7990 * 7991 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 7992 * ranges [{@code aFromIndex}, {@code atoIndex}) and 7993 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 7994 * if the following expression is true: 7995 * <pre>{@code 7996 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 7997 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 7998 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 7999 * }</pre> 8000 * 8001 * @param a the first array to be tested for a mismatch 8002 * @param aFromIndex the index (inclusive) of the first element in the 8003 * first array to be tested 8004 * @param aToIndex the index (exclusive) of the last element in the 8005 * first array to be tested 8006 * @param b the second array to be tested for a mismatch 8007 * @param bFromIndex the index (inclusive) of the first element in the 8008 * second array to be tested 8009 * @param bToIndex the index (exclusive) of the last element in the 8010 * second array to be tested 8011 * @return the relative index of the first mismatch between the two arrays 8012 * over the specified ranges, otherwise {@code -1}. 8013 * @throws IllegalArgumentException 8014 * if {@code aFromIndex > aToIndex} or 8015 * if {@code bFromIndex > bToIndex} 8016 * @throws ArrayIndexOutOfBoundsException 8017 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8018 * if {@code bFromIndex < 0 or bToIndex > b.length} 8019 * @throws NullPointerException 8020 * if either array is {@code null} 8021 * @since 9 8022 */ mismatch(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)8023 public static int mismatch(short[] a, int aFromIndex, int aToIndex, 8024 short[] b, int bFromIndex, int bToIndex) { 8025 rangeCheck(a.length, aFromIndex, aToIndex); 8026 rangeCheck(b.length, bFromIndex, bToIndex); 8027 8028 int aLength = aToIndex - aFromIndex; 8029 int bLength = bToIndex - bFromIndex; 8030 int length = Math.min(aLength, bLength); 8031 int i = ArraysSupport.mismatch(a, aFromIndex, 8032 b, bFromIndex, 8033 length); 8034 return (i < 0 && aLength != bLength) ? length : i; 8035 } 8036 8037 // Mismatch int 8038 8039 /** 8040 * Finds and returns the index of the first mismatch between two {@code int} 8041 * arrays, otherwise return -1 if no mismatch is found. The index will be 8042 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 8043 * array. 8044 * 8045 * <p>If the two arrays share a common prefix then the returned index is the 8046 * length of the common prefix and it follows that there is a mismatch 8047 * between the two elements at that index within the respective arrays. 8048 * If one array is a proper prefix of the other then the returned index is 8049 * the length of the smaller array and it follows that the index is only 8050 * valid for the larger array. 8051 * Otherwise, there is no mismatch. 8052 * 8053 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8054 * prefix of length {@code pl} if the following expression is true: 8055 * <pre>{@code 8056 * pl >= 0 && 8057 * pl < Math.min(a.length, b.length) && 8058 * Arrays.equals(a, 0, pl, b, 0, pl) && 8059 * a[pl] != b[pl] 8060 * }</pre> 8061 * Note that a common prefix length of {@code 0} indicates that the first 8062 * elements from each array mismatch. 8063 * 8064 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8065 * prefix if the following expression is true: 8066 * <pre>{@code 8067 * a.length != b.length && 8068 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8069 * b, 0, Math.min(a.length, b.length)) 8070 * }</pre> 8071 * 8072 * @param a the first array to be tested for a mismatch 8073 * @param b the second array to be tested for a mismatch 8074 * @return the index of the first mismatch between the two arrays, 8075 * otherwise {@code -1}. 8076 * @throws NullPointerException 8077 * if either array is {@code null} 8078 * @since 9 8079 */ mismatch(int[] a, int[] b)8080 public static int mismatch(int[] a, int[] b) { 8081 int length = Math.min(a.length, b.length); // Check null array refs 8082 if (a == b) 8083 return -1; 8084 8085 int i = ArraysSupport.mismatch(a, b, length); 8086 return (i < 0 && a.length != b.length) ? length : i; 8087 } 8088 8089 /** 8090 * Finds and returns the relative index of the first mismatch between two 8091 * {@code int} arrays over the specified ranges, otherwise return -1 if no 8092 * mismatch is found. The index will be in the range of 0 (inclusive) up to 8093 * the length (inclusive) of the smaller range. 8094 * 8095 * <p>If the two arrays, over the specified ranges, share a common prefix 8096 * then the returned relative index is the length of the common prefix and 8097 * it follows that there is a mismatch between the two elements at that 8098 * relative index within the respective arrays. 8099 * If one array is a proper prefix of the other, over the specified ranges, 8100 * then the returned relative index is the length of the smaller range and 8101 * it follows that the relative index is only valid for the array with the 8102 * larger range. 8103 * Otherwise, there is no mismatch. 8104 * 8105 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8106 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8107 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8108 * prefix of length {@code pl} if the following expression is true: 8109 * <pre>{@code 8110 * pl >= 0 && 8111 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8112 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 8113 * a[aFromIndex + pl] != b[bFromIndex + pl] 8114 * }</pre> 8115 * Note that a common prefix length of {@code 0} indicates that the first 8116 * elements from each array mismatch. 8117 * 8118 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8119 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8120 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8121 * if the following expression is true: 8122 * <pre>{@code 8123 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8124 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8125 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 8126 * }</pre> 8127 * 8128 * @param a the first array to be tested for a mismatch 8129 * @param aFromIndex the index (inclusive) of the first element in the 8130 * first array to be tested 8131 * @param aToIndex the index (exclusive) of the last element in the 8132 * first array to be tested 8133 * @param b the second array to be tested for a mismatch 8134 * @param bFromIndex the index (inclusive) of the first element in the 8135 * second array to be tested 8136 * @param bToIndex the index (exclusive) of the last element in the 8137 * second array to be tested 8138 * @return the relative index of the first mismatch between the two arrays 8139 * over the specified ranges, otherwise {@code -1}. 8140 * @throws IllegalArgumentException 8141 * if {@code aFromIndex > aToIndex} or 8142 * if {@code bFromIndex > bToIndex} 8143 * @throws ArrayIndexOutOfBoundsException 8144 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8145 * if {@code bFromIndex < 0 or bToIndex > b.length} 8146 * @throws NullPointerException 8147 * if either array is {@code null} 8148 * @since 9 8149 */ mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)8150 public static int mismatch(int[] a, int aFromIndex, int aToIndex, 8151 int[] b, int bFromIndex, int bToIndex) { 8152 rangeCheck(a.length, aFromIndex, aToIndex); 8153 rangeCheck(b.length, bFromIndex, bToIndex); 8154 8155 int aLength = aToIndex - aFromIndex; 8156 int bLength = bToIndex - bFromIndex; 8157 int length = Math.min(aLength, bLength); 8158 int i = ArraysSupport.mismatch(a, aFromIndex, 8159 b, bFromIndex, 8160 length); 8161 return (i < 0 && aLength != bLength) ? length : i; 8162 } 8163 8164 // Mismatch long 8165 8166 /** 8167 * Finds and returns the index of the first mismatch between two {@code long} 8168 * arrays, otherwise return -1 if no mismatch is found. The index will be 8169 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 8170 * array. 8171 * 8172 * <p>If the two arrays share a common prefix then the returned index is the 8173 * length of the common prefix and it follows that there is a mismatch 8174 * between the two elements at that index within the respective arrays. 8175 * If one array is a proper prefix of the other then the returned index is 8176 * the length of the smaller array and it follows that the index is only 8177 * valid for the larger array. 8178 * Otherwise, there is no mismatch. 8179 * 8180 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8181 * prefix of length {@code pl} if the following expression is true: 8182 * <pre>{@code 8183 * pl >= 0 && 8184 * pl < Math.min(a.length, b.length) && 8185 * Arrays.equals(a, 0, pl, b, 0, pl) && 8186 * a[pl] != b[pl] 8187 * }</pre> 8188 * Note that a common prefix length of {@code 0} indicates that the first 8189 * elements from each array mismatch. 8190 * 8191 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8192 * prefix if the following expression is true: 8193 * <pre>{@code 8194 * a.length != b.length && 8195 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8196 * b, 0, Math.min(a.length, b.length)) 8197 * }</pre> 8198 * 8199 * @param a the first array to be tested for a mismatch 8200 * @param b the second array to be tested for a mismatch 8201 * @return the index of the first mismatch between the two arrays, 8202 * otherwise {@code -1}. 8203 * @throws NullPointerException 8204 * if either array is {@code null} 8205 * @since 9 8206 */ mismatch(long[] a, long[] b)8207 public static int mismatch(long[] a, long[] b) { 8208 int length = Math.min(a.length, b.length); // Check null array refs 8209 if (a == b) 8210 return -1; 8211 8212 int i = ArraysSupport.mismatch(a, b, length); 8213 return (i < 0 && a.length != b.length) ? length : i; 8214 } 8215 8216 /** 8217 * Finds and returns the relative index of the first mismatch between two 8218 * {@code long} arrays over the specified ranges, otherwise return -1 if no 8219 * mismatch is found. The index will be in the range of 0 (inclusive) up to 8220 * the length (inclusive) of the smaller range. 8221 * 8222 * <p>If the two arrays, over the specified ranges, share a common prefix 8223 * then the returned relative index is the length of the common prefix and 8224 * it follows that there is a mismatch between the two elements at that 8225 * relative index within the respective arrays. 8226 * If one array is a proper prefix of the other, over the specified ranges, 8227 * then the returned relative index is the length of the smaller range and 8228 * it follows that the relative index is only valid for the array with the 8229 * larger range. 8230 * Otherwise, there is no mismatch. 8231 * 8232 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8233 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8234 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8235 * prefix of length {@code pl} if the following expression is true: 8236 * <pre>{@code 8237 * pl >= 0 && 8238 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8239 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 8240 * a[aFromIndex + pl] != b[bFromIndex + pl] 8241 * }</pre> 8242 * Note that a common prefix length of {@code 0} indicates that the first 8243 * elements from each array mismatch. 8244 * 8245 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8246 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8247 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8248 * if the following expression is true: 8249 * <pre>{@code 8250 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8251 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8252 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 8253 * }</pre> 8254 * 8255 * @param a the first array to be tested for a mismatch 8256 * @param aFromIndex the index (inclusive) of the first element in the 8257 * first array to be tested 8258 * @param aToIndex the index (exclusive) of the last element in the 8259 * first array to be tested 8260 * @param b the second array to be tested for a mismatch 8261 * @param bFromIndex the index (inclusive) of the first element in the 8262 * second array to be tested 8263 * @param bToIndex the index (exclusive) of the last element in the 8264 * second array to be tested 8265 * @return the relative index of the first mismatch between the two arrays 8266 * over the specified ranges, otherwise {@code -1}. 8267 * @throws IllegalArgumentException 8268 * if {@code aFromIndex > aToIndex} or 8269 * if {@code bFromIndex > bToIndex} 8270 * @throws ArrayIndexOutOfBoundsException 8271 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8272 * if {@code bFromIndex < 0 or bToIndex > b.length} 8273 * @throws NullPointerException 8274 * if either array is {@code null} 8275 * @since 9 8276 */ mismatch(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)8277 public static int mismatch(long[] a, int aFromIndex, int aToIndex, 8278 long[] b, int bFromIndex, int bToIndex) { 8279 rangeCheck(a.length, aFromIndex, aToIndex); 8280 rangeCheck(b.length, bFromIndex, bToIndex); 8281 8282 int aLength = aToIndex - aFromIndex; 8283 int bLength = bToIndex - bFromIndex; 8284 int length = Math.min(aLength, bLength); 8285 int i = ArraysSupport.mismatch(a, aFromIndex, 8286 b, bFromIndex, 8287 length); 8288 return (i < 0 && aLength != bLength) ? length : i; 8289 } 8290 8291 // Mismatch float 8292 8293 /** 8294 * Finds and returns the index of the first mismatch between two {@code float} 8295 * arrays, otherwise return -1 if no mismatch is found. The index will be 8296 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller 8297 * array. 8298 * 8299 * <p>If the two arrays share a common prefix then the returned index is the 8300 * length of the common prefix and it follows that there is a mismatch 8301 * between the two elements at that index within the respective arrays. 8302 * If one array is a proper prefix of the other then the returned index is 8303 * the length of the smaller array and it follows that the index is only 8304 * valid for the larger array. 8305 * Otherwise, there is no mismatch. 8306 * 8307 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8308 * prefix of length {@code pl} if the following expression is true: 8309 * <pre>{@code 8310 * pl >= 0 && 8311 * pl < Math.min(a.length, b.length) && 8312 * Arrays.equals(a, 0, pl, b, 0, pl) && 8313 * Float.compare(a[pl], b[pl]) != 0 8314 * }</pre> 8315 * Note that a common prefix length of {@code 0} indicates that the first 8316 * elements from each array mismatch. 8317 * 8318 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8319 * prefix if the following expression is true: 8320 * <pre>{@code 8321 * a.length != b.length && 8322 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8323 * b, 0, Math.min(a.length, b.length)) 8324 * }</pre> 8325 * 8326 * @param a the first array to be tested for a mismatch 8327 * @param b the second array to be tested for a mismatch 8328 * @return the index of the first mismatch between the two arrays, 8329 * otherwise {@code -1}. 8330 * @throws NullPointerException 8331 * if either array is {@code null} 8332 * @since 9 8333 */ mismatch(float[] a, float[] b)8334 public static int mismatch(float[] a, float[] b) { 8335 int length = Math.min(a.length, b.length); // Check null array refs 8336 if (a == b) 8337 return -1; 8338 8339 int i = ArraysSupport.mismatch(a, b, length); 8340 return (i < 0 && a.length != b.length) ? length : i; 8341 } 8342 8343 /** 8344 * Finds and returns the relative index of the first mismatch between two 8345 * {@code float} arrays over the specified ranges, otherwise return -1 if no 8346 * mismatch is found. The index will be in the range of 0 (inclusive) up to 8347 * the length (inclusive) of the smaller range. 8348 * 8349 * <p>If the two arrays, over the specified ranges, share a common prefix 8350 * then the returned relative index is the length of the common prefix and 8351 * it follows that there is a mismatch between the two elements at that 8352 * relative index within the respective arrays. 8353 * If one array is a proper prefix of the other, over the specified ranges, 8354 * then the returned relative index is the length of the smaller range and 8355 * it follows that the relative index is only valid for the array with the 8356 * larger range. 8357 * Otherwise, there is no mismatch. 8358 * 8359 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8360 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8361 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8362 * prefix of length {@code pl} if the following expression is true: 8363 * <pre>{@code 8364 * pl >= 0 && 8365 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8366 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 8367 * Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0 8368 * }</pre> 8369 * Note that a common prefix length of {@code 0} indicates that the first 8370 * elements from each array mismatch. 8371 * 8372 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8373 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8374 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8375 * if the following expression is true: 8376 * <pre>{@code 8377 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8378 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8379 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 8380 * }</pre> 8381 * 8382 * @param a the first array to be tested for a mismatch 8383 * @param aFromIndex the index (inclusive) of the first element in the 8384 * first array to be tested 8385 * @param aToIndex the index (exclusive) of the last element in the 8386 * first array to be tested 8387 * @param b the second array to be tested for a mismatch 8388 * @param bFromIndex the index (inclusive) of the first element in the 8389 * second array to be tested 8390 * @param bToIndex the index (exclusive) of the last element in the 8391 * second array to be tested 8392 * @return the relative index of the first mismatch between the two arrays 8393 * over the specified ranges, otherwise {@code -1}. 8394 * @throws IllegalArgumentException 8395 * if {@code aFromIndex > aToIndex} or 8396 * if {@code bFromIndex > bToIndex} 8397 * @throws ArrayIndexOutOfBoundsException 8398 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8399 * if {@code bFromIndex < 0 or bToIndex > b.length} 8400 * @throws NullPointerException 8401 * if either array is {@code null} 8402 * @since 9 8403 */ mismatch(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)8404 public static int mismatch(float[] a, int aFromIndex, int aToIndex, 8405 float[] b, int bFromIndex, int bToIndex) { 8406 rangeCheck(a.length, aFromIndex, aToIndex); 8407 rangeCheck(b.length, bFromIndex, bToIndex); 8408 8409 int aLength = aToIndex - aFromIndex; 8410 int bLength = bToIndex - bFromIndex; 8411 int length = Math.min(aLength, bLength); 8412 int i = ArraysSupport.mismatch(a, aFromIndex, 8413 b, bFromIndex, 8414 length); 8415 return (i < 0 && aLength != bLength) ? length : i; 8416 } 8417 8418 // Mismatch double 8419 8420 /** 8421 * Finds and returns the index of the first mismatch between two 8422 * {@code double} arrays, otherwise return -1 if no mismatch is found. The 8423 * index will be in the range of 0 (inclusive) up to the length (inclusive) 8424 * of the smaller array. 8425 * 8426 * <p>If the two arrays share a common prefix then the returned index is the 8427 * length of the common prefix and it follows that there is a mismatch 8428 * between the two elements at that index within the respective arrays. 8429 * If one array is a proper prefix of the other then the returned index is 8430 * the length of the smaller array and it follows that the index is only 8431 * valid for the larger array. 8432 * Otherwise, there is no mismatch. 8433 * 8434 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8435 * prefix of length {@code pl} if the following expression is true: 8436 * <pre>{@code 8437 * pl >= 0 && 8438 * pl < Math.min(a.length, b.length) && 8439 * Arrays.equals(a, 0, pl, b, 0, pl) && 8440 * Double.compare(a[pl], b[pl]) != 0 8441 * }</pre> 8442 * Note that a common prefix length of {@code 0} indicates that the first 8443 * elements from each array mismatch. 8444 * 8445 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8446 * prefix if the following expression is true: 8447 * <pre>{@code 8448 * a.length != b.length && 8449 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8450 * b, 0, Math.min(a.length, b.length)) 8451 * }</pre> 8452 * 8453 * @param a the first array to be tested for a mismatch 8454 * @param b the second array to be tested for a mismatch 8455 * @return the index of the first mismatch between the two arrays, 8456 * otherwise {@code -1}. 8457 * @throws NullPointerException 8458 * if either array is {@code null} 8459 * @since 9 8460 */ mismatch(double[] a, double[] b)8461 public static int mismatch(double[] a, double[] b) { 8462 int length = Math.min(a.length, b.length); // Check null array refs 8463 if (a == b) 8464 return -1; 8465 8466 int i = ArraysSupport.mismatch(a, b, length); 8467 return (i < 0 && a.length != b.length) ? length : i; 8468 } 8469 8470 /** 8471 * Finds and returns the relative index of the first mismatch between two 8472 * {@code double} arrays over the specified ranges, otherwise return -1 if 8473 * no mismatch is found. The index will be in the range of 0 (inclusive) up 8474 * to the length (inclusive) of the smaller range. 8475 * 8476 * <p>If the two arrays, over the specified ranges, share a common prefix 8477 * then the returned relative index is the length of the common prefix and 8478 * it follows that there is a mismatch between the two elements at that 8479 * relative index within the respective arrays. 8480 * If one array is a proper prefix of the other, over the specified ranges, 8481 * then the returned relative index is the length of the smaller range and 8482 * it follows that the relative index is only valid for the array with the 8483 * larger range. 8484 * Otherwise, there is no mismatch. 8485 * 8486 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8487 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8488 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8489 * prefix of length {@code pl} if the following expression is true: 8490 * <pre>{@code 8491 * pl >= 0 && 8492 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8493 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 8494 * Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0 8495 * }</pre> 8496 * Note that a common prefix length of {@code 0} indicates that the first 8497 * elements from each array mismatch. 8498 * 8499 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8500 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8501 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8502 * if the following expression is true: 8503 * <pre>{@code 8504 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8505 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8506 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 8507 * }</pre> 8508 * 8509 * @param a the first array to be tested for a mismatch 8510 * @param aFromIndex the index (inclusive) of the first element in the 8511 * first array to be tested 8512 * @param aToIndex the index (exclusive) of the last element in the 8513 * first array to be tested 8514 * @param b the second array to be tested for a mismatch 8515 * @param bFromIndex the index (inclusive) of the first element in the 8516 * second array to be tested 8517 * @param bToIndex the index (exclusive) of the last element in the 8518 * second array to be tested 8519 * @return the relative index of the first mismatch between the two arrays 8520 * over the specified ranges, otherwise {@code -1}. 8521 * @throws IllegalArgumentException 8522 * if {@code aFromIndex > aToIndex} or 8523 * if {@code bFromIndex > bToIndex} 8524 * @throws ArrayIndexOutOfBoundsException 8525 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8526 * if {@code bFromIndex < 0 or bToIndex > b.length} 8527 * @throws NullPointerException 8528 * if either array is {@code null} 8529 * @since 9 8530 */ mismatch(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)8531 public static int mismatch(double[] a, int aFromIndex, int aToIndex, 8532 double[] b, int bFromIndex, int bToIndex) { 8533 rangeCheck(a.length, aFromIndex, aToIndex); 8534 rangeCheck(b.length, bFromIndex, bToIndex); 8535 8536 int aLength = aToIndex - aFromIndex; 8537 int bLength = bToIndex - bFromIndex; 8538 int length = Math.min(aLength, bLength); 8539 int i = ArraysSupport.mismatch(a, aFromIndex, 8540 b, bFromIndex, 8541 length); 8542 return (i < 0 && aLength != bLength) ? length : i; 8543 } 8544 8545 // Mismatch objects 8546 8547 /** 8548 * Finds and returns the index of the first mismatch between two 8549 * {@code Object} arrays, otherwise return -1 if no mismatch is found. The 8550 * index will be in the range of 0 (inclusive) up to the length (inclusive) 8551 * of the smaller array. 8552 * 8553 * <p>If the two arrays share a common prefix then the returned index is the 8554 * length of the common prefix and it follows that there is a mismatch 8555 * between the two elements at that index within the respective arrays. 8556 * If one array is a proper prefix of the other then the returned index is 8557 * the length of the smaller array and it follows that the index is only 8558 * valid for the larger array. 8559 * Otherwise, there is no mismatch. 8560 * 8561 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8562 * prefix of length {@code pl} if the following expression is true: 8563 * <pre>{@code 8564 * pl >= 0 && 8565 * pl < Math.min(a.length, b.length) && 8566 * Arrays.equals(a, 0, pl, b, 0, pl) && 8567 * !Objects.equals(a[pl], b[pl]) 8568 * }</pre> 8569 * Note that a common prefix length of {@code 0} indicates that the first 8570 * elements from each array mismatch. 8571 * 8572 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8573 * prefix if the following expression is true: 8574 * <pre>{@code 8575 * a.length != b.length && 8576 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8577 * b, 0, Math.min(a.length, b.length)) 8578 * }</pre> 8579 * 8580 * @param a the first array to be tested for a mismatch 8581 * @param b the second array to be tested for a mismatch 8582 * @return the index of the first mismatch between the two arrays, 8583 * otherwise {@code -1}. 8584 * @throws NullPointerException 8585 * if either array is {@code null} 8586 * @since 9 8587 */ mismatch(Object[] a, Object[] b)8588 public static int mismatch(Object[] a, Object[] b) { 8589 int length = Math.min(a.length, b.length); // Check null array refs 8590 if (a == b) 8591 return -1; 8592 8593 for (int i = 0; i < length; i++) { 8594 if (!Objects.equals(a[i], b[i])) 8595 return i; 8596 } 8597 8598 return a.length != b.length ? length : -1; 8599 } 8600 8601 /** 8602 * Finds and returns the relative index of the first mismatch between two 8603 * {@code Object} arrays over the specified ranges, otherwise return -1 if 8604 * no mismatch is found. The index will be in the range of 0 (inclusive) up 8605 * to the length (inclusive) of the smaller range. 8606 * 8607 * <p>If the two arrays, over the specified ranges, share a common prefix 8608 * then the returned relative index is the length of the common prefix and 8609 * it follows that there is a mismatch between the two elements at that 8610 * relative index within the respective arrays. 8611 * If one array is a proper prefix of the other, over the specified ranges, 8612 * then the returned relative index is the length of the smaller range and 8613 * it follows that the relative index is only valid for the array with the 8614 * larger range. 8615 * Otherwise, there is no mismatch. 8616 * 8617 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8618 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8619 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8620 * prefix of length {@code pl} if the following expression is true: 8621 * <pre>{@code 8622 * pl >= 0 && 8623 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8624 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) && 8625 * !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl]) 8626 * }</pre> 8627 * Note that a common prefix length of {@code 0} indicates that the first 8628 * elements from each array mismatch. 8629 * 8630 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8631 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8632 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8633 * if the following expression is true: 8634 * <pre>{@code 8635 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8636 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8637 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)) 8638 * }</pre> 8639 * 8640 * @param a the first array to be tested for a mismatch 8641 * @param aFromIndex the index (inclusive) of the first element in the 8642 * first array to be tested 8643 * @param aToIndex the index (exclusive) of the last element in the 8644 * first array to be tested 8645 * @param b the second array to be tested for a mismatch 8646 * @param bFromIndex the index (inclusive) of the first element in the 8647 * second array to be tested 8648 * @param bToIndex the index (exclusive) of the last element in the 8649 * second array to be tested 8650 * @return the relative index of the first mismatch between the two arrays 8651 * over the specified ranges, otherwise {@code -1}. 8652 * @throws IllegalArgumentException 8653 * if {@code aFromIndex > aToIndex} or 8654 * if {@code bFromIndex > bToIndex} 8655 * @throws ArrayIndexOutOfBoundsException 8656 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8657 * if {@code bFromIndex < 0 or bToIndex > b.length} 8658 * @throws NullPointerException 8659 * if either array is {@code null} 8660 * @since 9 8661 */ mismatch( Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)8662 public static int mismatch( 8663 Object[] a, int aFromIndex, int aToIndex, 8664 Object[] b, int bFromIndex, int bToIndex) { 8665 rangeCheck(a.length, aFromIndex, aToIndex); 8666 rangeCheck(b.length, bFromIndex, bToIndex); 8667 8668 int aLength = aToIndex - aFromIndex; 8669 int bLength = bToIndex - bFromIndex; 8670 int length = Math.min(aLength, bLength); 8671 for (int i = 0; i < length; i++) { 8672 if (!Objects.equals(a[aFromIndex++], b[bFromIndex++])) 8673 return i; 8674 } 8675 8676 return aLength != bLength ? length : -1; 8677 } 8678 8679 /** 8680 * Finds and returns the index of the first mismatch between two 8681 * {@code Object} arrays, otherwise return -1 if no mismatch is found. 8682 * The index will be in the range of 0 (inclusive) up to the length 8683 * (inclusive) of the smaller array. 8684 * 8685 * <p>The specified comparator is used to determine if two array elements 8686 * from the each array are not equal. 8687 * 8688 * <p>If the two arrays share a common prefix then the returned index is the 8689 * length of the common prefix and it follows that there is a mismatch 8690 * between the two elements at that index within the respective arrays. 8691 * If one array is a proper prefix of the other then the returned index is 8692 * the length of the smaller array and it follows that the index is only 8693 * valid for the larger array. 8694 * Otherwise, there is no mismatch. 8695 * 8696 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common 8697 * prefix of length {@code pl} if the following expression is true: 8698 * <pre>{@code 8699 * pl >= 0 && 8700 * pl < Math.min(a.length, b.length) && 8701 * Arrays.equals(a, 0, pl, b, 0, pl, cmp) 8702 * cmp.compare(a[pl], b[pl]) != 0 8703 * }</pre> 8704 * Note that a common prefix length of {@code 0} indicates that the first 8705 * elements from each array mismatch. 8706 * 8707 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper 8708 * prefix if the following expression is true: 8709 * <pre>{@code 8710 * a.length != b.length && 8711 * Arrays.equals(a, 0, Math.min(a.length, b.length), 8712 * b, 0, Math.min(a.length, b.length), 8713 * cmp) 8714 * }</pre> 8715 * 8716 * @param a the first array to be tested for a mismatch 8717 * @param b the second array to be tested for a mismatch 8718 * @param cmp the comparator to compare array elements 8719 * @param <T> the type of array elements 8720 * @return the index of the first mismatch between the two arrays, 8721 * otherwise {@code -1}. 8722 * @throws NullPointerException 8723 * if either array or the comparator is {@code null} 8724 * @since 9 8725 */ mismatch(T[] a, T[] b, Comparator<? super T> cmp)8726 public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) { 8727 Objects.requireNonNull(cmp); 8728 int length = Math.min(a.length, b.length); // Check null array refs 8729 if (a == b) 8730 return -1; 8731 8732 for (int i = 0; i < length; i++) { 8733 T oa = a[i]; 8734 T ob = b[i]; 8735 if (oa != ob) { 8736 // Null-value comparison is deferred to the comparator 8737 int v = cmp.compare(oa, ob); 8738 if (v != 0) { 8739 return i; 8740 } 8741 } 8742 } 8743 8744 return a.length != b.length ? length : -1; 8745 } 8746 8747 /** 8748 * Finds and returns the relative index of the first mismatch between two 8749 * {@code Object} arrays over the specified ranges, otherwise return -1 if 8750 * no mismatch is found. The index will be in the range of 0 (inclusive) up 8751 * to the length (inclusive) of the smaller range. 8752 * 8753 * <p>If the two arrays, over the specified ranges, share a common prefix 8754 * then the returned relative index is the length of the common prefix and 8755 * it follows that there is a mismatch between the two elements at that 8756 * relative index within the respective arrays. 8757 * If one array is a proper prefix of the other, over the specified ranges, 8758 * then the returned relative index is the length of the smaller range and 8759 * it follows that the relative index is only valid for the array with the 8760 * larger range. 8761 * Otherwise, there is no mismatch. 8762 * 8763 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8764 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8765 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common 8766 * prefix of length {@code pl} if the following expression is true: 8767 * <pre>{@code 8768 * pl >= 0 && 8769 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) && 8770 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) && 8771 * cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0 8772 * }</pre> 8773 * Note that a common prefix length of {@code 0} indicates that the first 8774 * elements from each array mismatch. 8775 * 8776 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified 8777 * ranges [{@code aFromIndex}, {@code atoIndex}) and 8778 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper 8779 * if the following expression is true: 8780 * <pre>{@code 8781 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) && 8782 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8783 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex), 8784 * cmp) 8785 * }</pre> 8786 * 8787 * @param a the first array to be tested for a mismatch 8788 * @param aFromIndex the index (inclusive) of the first element in the 8789 * first array to be tested 8790 * @param aToIndex the index (exclusive) of the last element in the 8791 * first array to be tested 8792 * @param b the second array to be tested for a mismatch 8793 * @param bFromIndex the index (inclusive) of the first element in the 8794 * second array to be tested 8795 * @param bToIndex the index (exclusive) of the last element in the 8796 * second array to be tested 8797 * @param cmp the comparator to compare array elements 8798 * @param <T> the type of array elements 8799 * @return the relative index of the first mismatch between the two arrays 8800 * over the specified ranges, otherwise {@code -1}. 8801 * @throws IllegalArgumentException 8802 * if {@code aFromIndex > aToIndex} or 8803 * if {@code bFromIndex > bToIndex} 8804 * @throws ArrayIndexOutOfBoundsException 8805 * if {@code aFromIndex < 0 or aToIndex > a.length} or 8806 * if {@code bFromIndex < 0 or bToIndex > b.length} 8807 * @throws NullPointerException 8808 * if either array or the comparator is {@code null} 8809 * @since 9 8810 */ mismatch( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)8811 public static <T> int mismatch( 8812 T[] a, int aFromIndex, int aToIndex, 8813 T[] b, int bFromIndex, int bToIndex, 8814 Comparator<? super T> cmp) { 8815 Objects.requireNonNull(cmp); 8816 rangeCheck(a.length, aFromIndex, aToIndex); 8817 rangeCheck(b.length, bFromIndex, bToIndex); 8818 8819 int aLength = aToIndex - aFromIndex; 8820 int bLength = bToIndex - bFromIndex; 8821 int length = Math.min(aLength, bLength); 8822 for (int i = 0; i < length; i++) { 8823 T oa = a[aFromIndex++]; 8824 T ob = b[bFromIndex++]; 8825 if (oa != ob) { 8826 // Null-value comparison is deferred to the comparator 8827 int v = cmp.compare(oa, ob); 8828 if (v != 0) { 8829 return i; 8830 } 8831 } 8832 } 8833 8834 return aLength != bLength ? length : -1; 8835 } 8836 } 8837