Use of JDK tools and JDK tools
JDK has many built-in frequently-used tool classes ending with "s", such as collection tool class Collections, array tool class Arrays, object tool class Objects, file tool class Files, path tool class Paths, maths is a mathematical tool, but there are some tool classes that do not end with "s", such as TimeUnit and System.
The following describes the usage of each tool class.
(1) define many useful APIs in Objects, such as null pointer judgment and comparison of equal Objects. Common APIs include the following:
Used to determine whether two objects are equal. Both parameters can be null.
Public static boolean equals (Object a, Object B)
Used to determine whether an object is empty
Public static boolean isNull (Object obj)
Used to determine whether an object is not empty
Public static boolean nonNull (Object obj)
It is used for non-null check of variables. If it is null, a null pointer exception is thrown. If it is not null, this variable is returned. It can also throw specified exception information.
Public static <T> T requireNonNull (T obj)
Public static <T> T requireNonNull (T obj, String message)
Objects example:
1 static void useObjects () {2 String str1 = null; 3 String str2 = "Hello Java"; 4 5 // judge whether str1 and str2 are equal 6 boolean character flag = Objects. equals (str1, str2); 7 8 // null pointer judgment. If it is null, a null pointer exception is thrown. Otherwise, the original value 9 str1 = Objects is returned. requireNonNull (str1); 10 str2 = Objects. requireNonNull (str2, "Str2 is null"); 11 12 // judge whether str1 is null13 boolean nullFlag = Objects. isNull (str1); 14 // judge whether str2 is not null 15 boolean nonNullFlag = Objects. nonNull (str2); 16}View Code
(2) Arrays defines many array-related APIs.
// Sort Arrays
Public static void sort (int [])
// Binary search for elements in an array
Public static int binarySearch (long [] a, long key)
// Determine whether two arrays are equal (the array elements are equal and the array length is consistent)
Public static boolean equals (long [] a, long [] a2)
// Fill the array with the specified value
Public static void fill (long [] a, long val)
// Copy the Array
Public static <T> T [] copyOf (T [] original, int newLength)
// Obtain the hashCode of the array
Public static int hashCode (long a [])
// Convert the entire array to a string in the format of [a, B, c,..., d]
Public static String toString (int [])
Arrays uses the following sample code:
1 static void useArrays () {2 int [] arr1 = {5, 3, 2, 1, 4}; 3 int [] arr2 = {5, 3, 2, 1, 4, 5}; 4 5 // sort arr1. The default value is ascending. 6 Arrays. sort (arr1); 7 8 // search for the position of 2 in arr1. The index value is returned. arr1 must be an ordered 9 int index = Arrays. binarySearch (arr1, 2); 10 11 // judge whether arr1 and arr2 are equal 12 boolean flag = Arrays. equals (arr1, arr2); 13 14 int [] newArr = new int [10]; 15 // use 10 to fill the newArr array with 16 Arrays. fill (newArr, 10); 17 18 // copy the elements of the newArr array 0-4 and return the new array 19 int [] ansArr = Arrays. copyOf (newArr, 5); 20 21 // obtain the hashCode22 int ansHashCode = Arrays of ansArr. hashCode (ansArr); 23 24 // convert ansArr to a String of 25 String str = Arrays. toString (ansArr); 26}View Code
(3) Many APIs commonly used in Collections are defined. The common APIs are as follows:
// Sort the List
Public static <T extends Comparable <? Super T> void sort (List <T> list)
// Returns the element index value in the List. The List must be ordered.
Public static <T> int binarySearch (List <? Extends Comparable <? Super T> list, T key)
// Flip List
Public static void reverse (List <?> List)
// Find the minimum element value in the List
Public static <T extends Object & Comparable <? Super T>
T min (Collection <? Extends T> coll)
// Find the maximum element value in the List
Public static <T extends Object & Comparable <? Super T>
T max (Collection <? Extends T> coll)
The sample code of Collections is as follows:
1 static void useCollections () {2 List <Integer> nums = new ArrayList <> (); 3 nums. add (1); 4 nums. add (3); 5 nums. add (2); 6 nums. add (5); 7 nums. add (4); 8 // sort the elements in the List 9 Collections. sort (nums); 10 11 // search for the index value of 5 in nums 12 int index = Collections. binarySearch (nums, 5); 13 14 // flip nums15 Collections. reverse (nums); 16 // find the minimum 17 Collections in nums. min (nums); 18 // find the maximum value of 19 Collections in nums. max (nums); 20}View Code
(4) Files defines many common file-related APIs. The common APIs are as follows:
// Obtain the byte input stream of the specified object
Public static InputStream newInputStream (Path path, OpenOption... options)
// Obtain the byte output stream of the specified file
Public static OutputStream newOutputStream (Path path, OpenOption... options)
// Get the character input stream of the specified file with the specified Encoding
Public static BufferedReader newBufferedReader (Path path, Charset cs)
// Get the character output stream of the specified file with the specified Encoding
Public static BufferedWriter
NewBufferedWriter (Path path, Charset cs, OpenOption... options)
// Get all the content in a file with the specified encoding and return it as a string stream
Public static Stream <String> lines (Path path, Charset cs)
// Create a file
Public static Path createFile (Path path, FileAttribute <?>... Attrs)
// Create a path
Public static Path createDirectory (Path dir, FileAttribute <?>... Attrs)
// Delete an object
Public static void delete (Path path)
// If the object exists, delete the object
Public static boolean deleteIfExists (Path path
// Copy a file
Public static Path copy (Path source, Path target, CopyOption... options)
// File Movement
Public static Path move (Path source, Path target, CopyOption... options)
The sample code for using Files is as follows:
1 static void useFiles () throws IOException {2 Path dataPath = Paths. get ("data.txt"); 3 // get character input stream 4 InputStream is = Files. newInputStream (dataPath); 5 // get character output stream 6 OutputStream OS = Files. newOutputStream (dataPath); 7 // get the character input stream 8 BufferedReader reader = Files. newBufferedReader (dataPath); 9 // get the character output stream 10 BufferedWriter writer = Files. newBufferedWriter (dataPath); 11 // obtain all the content in the file and return it as a String Stream. Each String represents a line of 12 streams of file text <String> lines = Files. lines (dataPath, Charset. forName ("UTF-8"); 13 14 // create a file path 15 Files. createDirectories (Paths. get ("hello"); 16 // create a file 17 Files. createFile (Paths. get ("config.txt"); 18 19 // delete a file or folder. If the folder does not exist, an exception of 20 Files is thrown. delete (Paths. get ("data.txt"); 21 // delete a file or folder. If the folder does not exist, the file will not be deleted or 22 Files will be thrown out. deleteIfExists (Paths. get ("hello"); 23 24 // only Files can be copied, but folders cannot be copied 25 Files. copy (Paths. get ("1.txt"), Paths. get ("2.txt"); 26 27 // move 28 Files to a file or folder. move (Paths. get ("SRC"), Paths. get ("DES"); 29}View Code
(5) Paths is mainly used in combination with Files and Files, so there is no need to talk about it.
(6) Maths mainly encapsulates some common mathematical-related operations, which are the most familiar tools and do not need to be explained.
(7) TimeUnit is convenient for thread sleep:
// Thread sleep for 5 hours
TimeUnit. HOURS. sleep (5 );
// Thread sleep for 5 minutes
TimeUnit. MINUTES. sleep (5 );
// Thread sleep for 5 seconds
TimeUnit. SECONDS. sleep (5 );
(8) System is also a tool class that everyone is familiar with. For example, the most common console output is
System. out. println (). Common APIs include:
// Obtain the current time, in milliseconds
Public static native long currentTimeMillis ()
// End Java program running
Public static void exit (int status)
// Copy an array
Public static native
Void arraycopy (Object src, int srcPos, Object dest, int destPos, int length)
The sample code for using System is as follows:
1 static void useSystem () {2 // obtain the current time, in milliseconds 3 long time = System. currentTimeMillis (); 4 5 // copy array 1 to array 2 6 int [] arr1 = {1, 2, 3, 4, 5 }; 7 int [] arr2 = new int [5]; 8 System. arraycopy (arr1, 0, arr2, 0, arr1.length); 9 10 // exit Program 11 normally. exit (0); 12}View Code
This article only gives a rough introduction to the usage of various tool classes. For more information, see the relevant API documentation.
In case of any errors, please note.