Java 8 enhanced tool class Arrays notes, java8arrays

Source: Internet
Author: User

Java 8 enhanced tool class Arrays notes, java8arrays

Make notes at will
Arrays tool categories are divided into two categories: Single-thread and multi-thread
All of the following statements with a subscript range are headers without tails.

Single thread:
1. binarySearch: The subscript of the key in the search array: binarySearch binary search. The array must be ordered and exist in this array. Otherwise, a negative subscript is returned.
Arrays. binarySearch (Object [] a, Object key): int
Arrays. binarySearch (Object [] a, int fromIndex, int toIndex, Object key): int
Skip to Example

2. copyOf: copy a new array. The copy length is determined by newLength. The length can be larger than the length of the copied array.

CopyOfRange: copy the value within the specified subscript range

CopyOf (T [] original, int newLength): T [] copyOfRange (T [] original, int from, int to): T []
Skip to Example

3. sort: Sorting in ascending order by default, which can be customized

Sort (Object [] a): voidsort (Object [] a, int fromIndex, int toIndex): voidsort (T [] a, Comparator <? Super T> c): voidsort (T [] a, int fromIndex, int toIndex, Comparator <? Super T> c): Jump to the example of void

4. toString: array toString

DeepToString: Two-dimensional array toString

ToString (Object [] a): StringdeepToString (Object [] a): String jump to the example

5. equals: compare whether the content of a one-dimensional array is equal

DeepEquals: compare whether the content of a two-dimensional array is equal

Equals (Object [] a, Object [] a2) deepEquals (Object [] a1, Object [] a2) jumps to the example

6. fill: Set all arrays to val, or set the array to val within the subscript range.

Fill (Object [] a, Object val): voidfill (Object [] a, int fromIndex, int toIndex, Object val): void jump to the example

7. setAll: perform expression operations on all arrays.

SetAll (T [] array, IntFunction <? Extends T> generator): Jump to the example of void

8. stream: converts an array to a stream and processes the array. All stream processing methods are available.

Stream (T [] array): Skip to the example of Stream <T>

Multithreading:

9. parallelPrefix: Binary iteration, binary operation on the original array content

ParallelPrefix (T [] array, BinaryOperator <T> op): voidparallelPrefix (T [] array, int fromIndex, int toIndex, BinaryOperator <T> op): void jump to Example

10. parallelSetAll: An array is used for expression operations.

ParallelSetAll (T [] array, IntFunction <? Extends T> generator): Jump to the example of void

11. parallelSort: Sort arrays in ascending or custom order.

ParallelSort (T [] a): voidparallelSort (T [] a, int fromIndex, int toIndex, Comparator <? Super T> cmp): Jump to the example of void

12. spliterator: return a Spliterator for Spliterator-related operations.

Spliterator (T [] array): Spliterator <T> jumps to the example


Hodgedge:
Public static void main (String [] args) {Integer [] arrayTest = {6, 1, 9, 2, 5, 7, 6, 10, 6, 12 }; // directly create a List <Integer> intList = Arrays. asList (5, 7, 9); // The following is a single-threaded algorithm that processes arrays.
// 1. // search for the subscript of the key in the array: binarySearch binary search. The array must be ordered and exist in this array. Otherwise, a negative subscript Integer arr [] = {1, 2, 4, 4, 5, 6} is returned }; int binarySearch = Arrays. binarySearch (arr, 3); System. out. println (binarySearch); // search for the subscript int binarySearch0 = Arrays of the key within the specified range. binarySearch (arr, 0, 3, 3); System. out. println (binarySearch0 );
// 2. // copy the new array. The copy length is determined by newLength. The length can be greater than the length of the copied array. Integer [] copyArray1 = Arrays. copyOf (arrayTest, 5); arrayPrint (copyArray1); // copy the value within the specified subscript range, including the header without the end Integer [] copyArray2 = Arrays. copyOfRange (arrayTest, 2, 7); arrayPrint (copyArray2 );
// 3. // In the specified subscript, the array is sorted in ascending order by default. This will change the original array. The subscript contains the header and does not include the last Integer [] sortArray1 = Arrays. copyOf (arrayTest, arrayTest. length); Arrays. sort (sortArray1,); arrayPrint (sortArray1); // sort all Arrays in the array. sort (sortArray1); arrayPrint (sortArray1); Integer [] sortArray2 = Arrays. copyOf (arrayTest, arrayTest. length); // uses the comparator to sort in descending order and Arrays within the specified subscript range. sort (sortArray2, 0, 5, (x, y)-> y. compareTo (x); arrayPrint (sortArray2); // sort Arrays in descending order using the comparator. sort (sortArray2, (x, y)-> y. compareTo (x); arrayPrint (sortArray2 );
// 4. // array toString System. out. println (Arrays. toString (arrayTest); Integer [] [] stuGrades = {, 9}, {,}, {, 10 }}; // two-dimensional array toString System. out. println (Arrays. deepToString (stuGrades ));
// 5. integer [] repeated S1 = Arrays. copyOf (arrayTest, arrayTest. length); // compare whether the content of a one-dimensional array is equal to System. out. println (Arrays. equals (round S1, arrayTest); Integer [] [] Round S2 = Arrays. copyOf (stuGrades, stuGrades. length); // compare whether the two-dimensional array content is equal to System. out. println (Arrays. deepEquals (stuGrades, ipvs2 ));
// 6. integer [] fillArr = new Integer [5]; // sets an array to val (5) Arrays. fill (fillArr, 5); arrayPrint (fillArr); // set the specified range of an array to val (10). The header does not include the end Arrays. fill (fillArr, 2, 3, 10); arrayPrint (fillArr );
// 7. integer [] setAllArr = Arrays. copyOf (arrayTest, arrayTest. length); // an array is used to perform expression operations on Arrays. setAll (setAllArr, a-> a * 3); System. out. println (setAllArr );
// 8. // New Features of Java 8: Stream processing of array. All Stream processing methods are available (Lambda and Stream will be used in a specific article) Arrays. stream (arrayTest ). map (a-> a * 2 ). filter (a-> a> 10 ). sorted (). distinct (). limit (6 ). forEach (a-> System. out. print (a + ""); System. out. println (); // The following is a multi-threaded algorithm that processes arrays (most Methods Starting with parallel can fully utilize modern CPU multi-core and effectively process large-scale arrays)
      
// 9. integer [] arrayPP1 = Arrays. copyOf (arrayTest, arrayTest. length); arrayPrint (arrayPP1); // binary iteration, binary operation on the original array content Arrays. parallelPrefix (arrayPP1, (x, y)-> x * y); arrayPrint (arrayPP1); Integer [] arrayPP2 = Arrays. copyOf (arrayTest, arrayTest. length); // binary operations are performed on the content of the original array within the specified subscript range. The subscript contains the header and does not contain the Arrays. parallelPrefix (arrayPP2, 0, 5, (x, y)-> x * y); arrayPrint (arrayPP2 );
// 10. integer [] arrayPSA = Arrays. copyOf (arrayTest, arrayTest. length); // assign a value to each element of the original array. In the following example, the subscript * 5 is assigned to the Arrays element of the array. parallelSetAll (arrayPSA, a-> a * 5); arrayPrint (arrayPSA); // 11. integer [] arrayPS1 = Arrays. copyOf (arrayTest, arrayTest. length); // sort the array in ascending order. parallelSort (arrayPS1); arrayPrint (arrayPS1); // sort the elements in the specified subscript range by the specified sorting method. The header does not include the Arrays at the end. parallelSort (arrayPS1, 0, 5, (x, y)-> y. compareTo (x); arrayPrint (arrayPS1); // 12. // return a Spliterator for other operations. Spliterator <Integer> spliterator = Arrays. spliterator (arrayPS1); // cut out a part of the original Spliterator as a new Spliterator. If it cannot be switched, null Spliterator <Integer> integerSpliterator = spliterator is returned. trySplit (); // estimateSize there are several elements to process while (spliterator. estimateSize ()> 0) {// process spliterator for each element. tryAdvance (x-> System. out. print (x * 2 + "");} System. out. println (); // integerSpliterator for Spliterator traversal. forEachRemaining (a-> System. out. print (a + ""); System. out. println (); // You Need to traverse the System. out. println (integerSpliterator. getExactSizeIfKnown (); // indicates the features of the Spliterator used to optimize the System. out. println (spliterator. characteristics ();} // late-stage cancer public static void arrayPrint (Object [] oArray) {System. out. println (Arrays. toString (oArray ));}
 

 

 
Reference: http://www.cnblogs.com/ECJTUACM-873284962/p/7363224.html
Reference: http://blog.csdn.net/lirx_tech/article/details/51437129

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.