Java learning 4-2_common tools

Common library classes

、 Java.util.Objects

1 Object method

1.1 equals

The equals method is to determine whether two objects are equal, and it is null safe.

When comparing two objects, the Object.equals method is prone to throw a null pointer exception.

The source code is as follows

public static boolean equals(Object a, Object b) {
    
    
        return (a == b) || (a != null && a.equals(b));
    }

1.2 deepEquals

Verify that two arrays are deep equality. This method is especially useful in array comparisons.

1.3 hash(Object… values)

Generate a hash code for the sequence of input values.

1.4 toString(Object o)

If the parameter is not null, return the result of calling toString, otherwise return null

1.5 requireNonNull(T obj)

If the parameter is not null, return T, otherwise throw a NullPointerException.

二、java.lang.Math

Common method

  1. Math.round(), returns the int closest to the parameter, it means "rounding"
  2. Math.rint(), returns the double value that is closest to the parameter and equal to a certain integer. If two numbers are equally close, it returns the even number
  3. Math.floor(), returns the largest (closest to positive infinity) double value, which is less than or equal to the parameter and equal to an integer
  4. Math.ceil(), returns the smallest (closest to negative infinity) double value, which is greater than or equal to the parameter and equal to an integer
  5. Math.cbrt(), returns the cube root of the double value
  6. Math.sqrt(), returns the positive square root of a double value that is correctly rounded
  7. Math.pow(), returns the value of the first parameter to the power of the second parameter
  8. Math.max(), returns the larger of two double values
  9. Math.min(), returns the smaller of two double values
  10. Math.abs(), find the absolute value

三、java.util.Arrays

The Arrays class contains various methods for processing arrays, and each method is basically a static method, which can be called directly by the class name Arrays.

  1. asList(), the function is to return a fixed size list supported by the specified array . The ArrayList returned by this method is not our commonly used collection class java.util.ArrayList. The ArrayList here is an internal class of Arrays java.util.Arrays.ArrayList
  2. sort(), this method is used for array sorting. There are a series of overloaded methods of this method in the Arrays class, which can handle 7 basic data types, including byte, char, double, float, int, long, short, etc. Can be sorted, there are Object types (implementing the Comparable interface), and the comparator Comparator.
  3. binarySearch(), to find an element in the array using binary method. This method is the same as the sort method and is applicable to various basic data types and objects.
  4. copyOf(), copy array elements. The bottom layer is implemented by System.arraycopy(), which is a native method.
  5. fill(), this series of methods are used to assign values ​​to arrays, and can specify a range of assignments.

四、java.math.BigDecimal

The API class BigDecimal provided by Java in the java.math package is used to perform precise operations on numbers with more than 16 significant digits. The double-precision floating-point variable double can handle 16 significant digits, but in practical applications, it may be necessary to operate and process larger or smaller numbers. Under normal circumstances, we can directly use Float and Double to deal with those numbers that do not need accurate calculation precision, but Double.valueOf(String) and Float.valueOf(String) will lose precision. So in development, if we need accurate calculation results, we must use the BigDecimal class

  1. abs(), returns the absolute BigDecimal value of BigDecimal.
  2. abs(MathContext mc), returns the BigDecimal of the absolute value of the BigDecimal value, and rounds it according to the context setting.
  3. add(BigDecimal augend), the return value is (this + augend), and its scale is max(this.scale(), augend.scale()).
  4. add(BigDecimal augend, MathContext mc), the return value is (this + augend), rounded according to the context setting.
  5. byteValueExact(), convert BigDecimal to byte, and check the missing information.
  6. compareTo(BigDecimal val), compares BigDecimal with the specified val.
  7. divide(BigDecimal divisor), the return value is (this / divisor), and its preferred scale is (this.scale()-divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion), ArithmeticException throws one.

、 、 Java.util.Date

Standard time: January 1, 1970, 0:0:00. The standard time of each region is different, because there are differences in time zones. New, China belongs to the East Eight District, so the standard time is January 1, 1970, 8: 0: 0

  1. Date(), used to construct an object without parameters, the current system time is used by default.
  2. Date(long date), the object is constructed according to the number of milliseconds specified by the parameter, where the parameter is the number of milliseconds from January 1, 1970, 0:0:0, (used with the File class)
  3. getTime(), Get the number of milliseconds from 0:00:00 on January 1, 1970 to the current object
  4. setTime(), used to set the time of the current object to the number of milliseconds specified by the parameter, away from the standard time

六、java.text.DateFormat

This class is used to format time output

  1. parse(), parse the text of the string, and return Date.
  2. format(), format a Date as a date/time string.

、 Java.util.Calendar

Used to describe date information, many methods in this class replace the obsolete methods of the Date class. This class is an abstract class and cannot instantiate objects.

  1. getInstance(), the user gets the reference of the current class
  2. set(int year, int month, int date, int hourOfDay, int minute, int second), set the year, month, day, hour, minute, and second according to the parameters
  3. getTime(), used to convert Calendar type to Date type and return.

、 、 Java.lang.System

The facilities provided include standard input, standard output and error output streams; access to externally defined attributes and environment variables; methods for loading files and libraries; and practical methods for quickly copying a part of the array

  1. Field

    Modifiers and types Field and description
    public final static PrintStream err "standard" error output stream.
    public final static InputStream in "Standard" input stream.
    public final static PrintStream out "standard" output stream.
  2. method

  3. currentTimeMillis(), returns the current value of the high-resolution time source of the running Java virtual machine, in nanoseconds

  4. nanoTime(), copy the array in the specified source array from the specified position to the specified position of the target array

  5. arraycopy(Object src, int srcPos, Object dest, int destPos, int length),

  6. lineSeparator(), returns the system-dependent line separator string.

  7. getProperties(), determine the current system properties

  8. Related attributes

    key Description of relevant value
    java.version Java Runtime Environment version
    java.vendor Java runtime environment provider
    java.vendor.url Java vendor URL
    java.home Java installation directory
    java.vm.specification.version Java Virtual Machine Specification Version
    java.vm.specification.vendor Java Virtual Machine Specification Supplier
    java.vm.specification.name Java virtual machine specification name
    java.vm.version Java virtual machine implementation version
    java.vm.vendor Java virtual machine implementation vendor
    java.vm.name](http://java.vm.name/) Java virtual machine implementation name
    java.specification.version Java Runtime Environment Specification Version
    java.specification.vendor Java Runtime Environment Specification Supplier
    java.specification.name Java runtime environment specification name
    java.class.version Java class format version number
    java.class.path Java classpath
    java.library.path List of paths to search when loading the library
    java.io.tmpdir Default temporary file path
    java.compiler The name of the JIT compiler to be used
    java.ext.dirs The extension directory or the path of the directory is out of date. This property and the mechanism to implement it can be removed in a future version.
    os.name Operating system name
    os.arch Operating system architecture
    os.version Operating system version
    file.separator File separator ("/" on UNIX)
    path.separator Path separator (":" on UNIX)
    line.separator Line separator ("\n" on UNIX)
    user.name User's account name
    user.home User's home directory
    user.dir User's current working directory

Nine, String

Common method

SN (serial number) Method description
1 char charAt(int index), returns the char value at the specified index.
2 int compareTo(Object o), compare this string with another object.
3 int compareTo(String anotherString), compare two strings lexicographically.
4 int compareToIgnoreCase(String str), compare two strings lexicographically, regardless of case.
5 String concat(String str), connect the specified string to the end of this string.
6 boolean contentEquals(StringBuffer sb), returns true if and only if the string has characters in the same order as the specified StringBuffer.
7 static String copyValueOf(char data),返回指定数组中表示该字符序列的 String。
8 static String copyValueOf(char data, int offset, int count),返回指定数组中表示该字符序列的 String。
9 boolean endsWith(String suffix),测试此字符串是否以指定的后缀结束。
10 boolean equals(Object anObject),将此字符串与指定的对象比较。
11 boolean equalsIgnoreCase(String anotherString),将此 String 与另一个 String 比较,不考虑大小写。
12 byte getBytes(),使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
13 byte getBytes(String charsetName),使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
14 void getChars(int srcBegin, int srcEnd, char dst, int dstBegin),将字符从此字符串复制到目标字符数组。
15 int hashCode(),返回此字符串的哈希码。
16 int indexOf(int ch),返回指定字符在此字符串中第一次出现处的索引。
17 int indexOf(int ch, int fromIndex),返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
18 int indexOf(String str),返回指定子字符串在此字符串中第一次出现处的索引。
19 int indexOf(String str, int fromIndex),返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
20 String intern(),返回字符串对象的规范化表示形式。
21 int lastIndexOf(int ch),返回指定字符在此字符串中最后一次出现处的索引。
22 int lastIndexOf(int ch, int fromIndex),返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
23 int lastIndexOf(String str),返回指定子字符串在此字符串中最右边出现处的索引。
24 int lastIndexOf(String str, int fromIndex), 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
25 int length(),返回此字符串的长度。
26 boolean matches(String regex),告知此字符串是否匹配给定的正则表达式。
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len),测试两个字符串区域是否相等。
28 boolean regionMatches(int toffset, String other, int ooffset, int len),测试两个字符串区域是否相等。
29 String replace(char oldChar, char newChar),返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
30 String replaceAll(String regex, String replacement),使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
31 String replaceFirst(String regex, String replacement),使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
32 String split(String regex),根据给定正则表达式的匹配拆分此字符串。
33 String split(String regex, int limit),根据匹配给定的正则表达式来拆分此字符串。
34 boolean startsWith(String prefix), 测试此字符串是否以指定的前缀开始。
35 boolean startsWith(String prefix, int toffset),测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
36 CharSequence subSequence(int beginIndex, int endIndex),返回一个新的字符序列,它是此序列的一个子序列。
37 String substring(int beginIndex),返回一个新的字符串,它是此字符串的一个子字符串。
38 String substring(int beginIndex, int endIndex),返回一个新字符串,它是此字符串的一个子字符串。
39 toCharArray(),将此字符串转换为一个新的字符数组。
40 String toLowerCase(),使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
41 String toLowerCase(Locale locale),使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
42 String toString(),返回此对象本身(它已经是一个字符串!)。
43 String toUpperCase(),使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
44 String toUpperCase(Locale locale),使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
45 String trim(),返回字符串的副本,忽略前导空白和尾部空白。
46 static String valueOf(primitive data type x),返回给定data type类型x参数的字符串表示形式。
47 CharSequence chars(),判断是否包含指定的字符系列。
48 isEmpty(),判断字符串是否为空。

Guess you like

Origin blog.csdn.net/Sky_Coolssy/article/details/108802281