Javase Note [8] The comparators, System, Math, Biginteger, Bigdecimal

2023-03-17  

Video link:java zero basic tutorial

The objects in

java, under normal circumstances, can only compare: == or! =. Can’t use> or <. However, in the development scenario, we need to sort multiple objects. The meaning of the comparison is required outside words. How to achieve? Use any of the two interfaces: comparable

  1. Like string, packaging, etc., the Comparable interface has been implemented.
  2. After rewriting the compareto () method of String, packaging, arrange from small to large
  3. Rewrite the rules of compareto ():
    If the current object THIS is greater than the metaphysical object OBJ, returns a positive integer
    If the current object THIS is less than the object OBJ, the negative integer is returned
    If the current object THIS is equal to the metaphysical object OBJ, return zero
  4. For the custom class, if we need to sort, we can let the custom class implement the Comparable interface, rewrite the Compareto () method, and indicate how to sort in the compareto (obj) method.
  1. background
    The current element type does not implement java.util.comParable interface without being convenient to modify the code, or implement the sorting rules of java.util.comparable interfaces are not suitable for the current operation, so you can consider using the object of the comparator to sort and forcibly confront each other Multiple objects perform overall sort comparison
  2. Rewam compare (Object O1, Object O2) method, compare the size of O1 and O2: If the method returns a positive integer, it means that O1 is greater than O2;
  3. OKPass the comparator to the sort method(such as Collections.sort or Arrays.sort), which allows accurate control in the order of sorting
    You can also use compators to control the order of certain data structures (such as orderly SET or orderly mapping), or what are the unprecedented sequences of Collection.

    Comparison of the use of

comparable interface and comparator interface

Comparable interface Once given a give
Comparator interface is a temporary comparison

The

System class represents the system, and many of the system -level attributes and control methods are placed inside the class. This class is located in the java.lang package
Since the constructor of this class is private, it is impossible to create an object of this class, that is, it is impossible to instantiated this class. The internal member variables and members’ methods are static, so it can also be easily called

  • member variable
    System class contains three member variables: IN, OUT and ERR, which represent standard input flow (keyboard input), standard output flow (display) and standard error output stream (display)
  • Member method
    native long currentTimeMillis(): The role of this method is to return the current computer time.
    void exit(int status) : The role of this method is to exit the program. The value of Status is 0 representing normal exit, and non -zero represents abnormal exit.Use this method to achieve the exit function of the program in the graphical interface programminget al.
    void gc() : The role of this method is to request the system for garbage recovery. As for whether the system is immediately recycled, it depends on the implementation of the garbage recycling algorithm in the system and the situation of the system execution.
    String getProperty(String key): The role of this method is to obtain the value corresponding to the attribute of the attributes in the system. The role of common attribute names and attributes in the system as shown in the following table:
Attribute name Attribute description
java.version Java Running Environment Version
java.home java installation directory
os.name The name of the operating system
os.version The version of the operating system
user.name User’s account name
user.home User’s main list
user.dir User’s current working directory

java.lang.math provides a series of static methods for scientific calculations. The parameter and return value type of the method is generally Double

Method name Instructions
abs Absolute value
acos,asin,atan,cos,sin,tan triangle function
sqrt square root
pow(double a, double b) A’s b power
log Natural pair
exp E is the bottom index
max(double a, double b)
min(double a, double b)
random() Return to random numbers from 0.0 to 1.0
long round(double a) Double type data A converts to Long type (four houses and five entry)
toDegrees(double angrad) arc-> angle
toRadians(double angdeg) angle-> arc

The

Integer class is a packaging class for int, and the maximum plastic value that can be stored is 2^31-1, and the LONG class is also limited, with a maximum of 2^63-1. If you want to represent the large integer, whether it is the basic data type or their packaging category, let alone the operation, let alone the calculation
java.math bagBiginteger can indicate an integer that is unchanged any accuracy. Biginteger provides the corresponding objects of all Java’s basic integer operators, and provides all related methods for Java.lang.math. In addition, Biginteger also provides calculations: mold calculation, GCD computing, quality test, prime number generation, bit operation, and some other operations

Constructor
Biginteger (String Val): Construct the Biginteger object according to the string

Commonly used methods

Method name Instructions
public BigInteger abs() Return to the absolute value of this Biginteger
BigInteger add(BigInteger val) Biginteger with its value (this + value)
BigInteger subtract(BigInteger val) Biginteger with a value (this -value)
BigInteger multiply(BigInteger val) Biginteger with this value (this * value)
BigInteger divide(BigInteger val) Biginteger with this value (this / value). Our integer phase retains only the integer part
BigInteger remainder(BigInteger val) Biginteger with a value (this % value)
BigInteger[] divideAndRemainder(BigInteger val) Returns the two Biginteger array of two Biginteger after the heel (this % value) (this % value)
BigInteger pow(int exponent) Biginteger with its value (this ^ exponent)

General Float and Double classes can be used for scientific calculations or engineering computing, but in business computing, the digital accuracy is required to be relatively high, so the use of java.math.bigdecimal classes
Bigdecimal class supports unavailable classes and arbitrary accuracy, symbolic decimal decimal setting points

Constructor
public BigDecimal(double val)
public BIgDecimal(String val)

Common method
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BIgDecimal divide(BigDecimal divisor, int scale, int roundingMode)

source

Random Posts

Android query the use of the Cursor class during the database

Javaweb Note — HTML/CSS Basic Learning 2 (Writing Specification of HTML File)

PendingINTENT detailed explanation

qt Multi -thread programming object thread and function execution thread

30 minutes from zero play to deploy docker+k8s cluster