Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the basic tools in guava

Shulou Source: shulou.com Published: 2022-06-02 02:39:43 09月22日 Update

In this issue, the editor will bring you about the basic tools in guava. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Basic tools [Basic utilities]

Optional class:

/ * *

* Optional is mostly used to take map values.

* key exists. If it is a non-null value, the original mapping value is returned. If it is a null value, the default value after or is returned.

* key does not exist. Return the default value after or

, /

Map map = new HashMap ()

Map.put ("aaa", 111)

Map.put ("bbb", 222)

Map.put ("ccc" 333)

Map.put (ddd, 444)

System.out.println (map.get ("eee")); / / returns a null value

Object eee = Optional.fromNullable (map.get ("eee")) .or ("555")

System.out.println (eee)

/ *

* jdk 1.8 has been implemented

* key exists. If the value of defaultValue is different from that of the mapping, defaultValue is returned, otherwise the original mapping value is returned.

* key does not exist. Return the default value.

, /

Object fff = map.getOrDefault (map.get ("fff"), "666")

System.out.println (fff)

/ / defaultValue focuses on reassigning keys, while Optional focuses on judging null.

Results:

Null

five hundred and fifty five

six hundred and sixty six

Preconditions class:

/ / used to check whether the input parameters of the method meet the conditions.

/ / there are mainly checkNotNull, checkArgument and checkElementIndex

Public class PreconditionsTest {

Public static void main (String [] args) {

PreconditionsTest prec = new PreconditionsTest ()

Try {

System.out.println (prec.sqrt (- 3.0))

} catch (IllegalArgumentException e) {

System.out.println (e.getMessage ())

}

Try {

System.out.println (prec.sum (null, 3))

} catch (NullPointerException e) {

System.out.println (e.getMessage ())

}

Try {

System.out.println (prec.getValue (5))

} catch (IndexOutOfBoundsException e) {

System.out.println (e.getMessage ())

}

}

Private int getValue (int I) {

Int [] data = {11, 22, 33, 44, 55}

Preconditions.checkElementIndex (I, data.length, "Illegal Argument passed: Invalid index.")

Return data [i]

}

Private int sum (Integer a, Integer b) {

A = Preconditions.checkNotNull (a, "Illegal Argument passed: First parametere is Null.")

B = Preconditions.checkNotNull (b, "Illegal Argument passed: Second parametere is Null.")

Return a + b

}

Private double sqrt (double v) {

Preconditions.checkArgument (v > 0.0, "Illegal Argument passed: Negative value% s.", v)

Return Math.sqrt (v)

}

}

Results:

Illegal Argument passed: Negative value-3.0.

Illegal Argument passed: First parametere is Null.

Illegal Argument passed: Invalid index. (5) must be less than size (5)

Objects class:

/ / the main methods are equals, hashcode, toString, compare/compareTo, comparisonChain

Public class ObjectsTest {

Public static void main (String [] args) {

String testName = "root"

String realName = "root"

/ / instead of testName! = null & & realName.equals (testName)

If (Objects.equal (testName, realName)) {

System.out.println (testName + "::" + realName)

}

Student S1 = new Student ("Mbhesh", "Pcrashar", 1, "VI")

Student S2 = new Student ("Mbhesh", "Pbrashar", 1, "VI")

Student S3 = new Student ("Mahesh", "Parashar", 1, "VI")

Student S4 = new Student ("Achesh", "Parashar", 1, "VI")

Student S5 = new Student ("Suresh", null, 3, null)

List stu = new ArrayList ()

Stu.add (S1)

Stu.add (S2)

Stu.add (S3)

Stu.add (S4)

Stu.add (S5)

Collections.sort (stu)

For (int I = 0; I < stu.size (); iTunes +) {

System.out.println (stu.get (I) .getFirstName () + ":" + stu.get (I) .getLastName ())

}

/ / MoreObjects.toStringHelper output strings are more flexible

System.out.println (MoreObjects.toStringHelper (S1) .add ("Name", s1.getFirstName () + "+ s1.getLastName ()) .add (" Class ", s1.getClassName ()) .add (" RollNo ", s1.getRollNo ()) .toString ())

System.out.println (MoreObjects.toStringHelper (S2) .add ("firstName", s2.getFirstName ()) .add ("lastName", s2.getLastName ()

}

}

Class Student implements Comparable {

Private String firstName

Private String lastName

Private int rollNo

Private String className

Public Student (String firstName, String lastName, int rollNo, String className) {

This.firstName = firstName

This.lastName = lastName

This.rollNo = rollNo

This.className = className

}

@ Override

Public boolean equals (Object o) {

If (this = = o) return true

If (o = = null | | getClass ()! = o.getClass ()) return false

Student student = (Student) o

Return rollNo = = student.rollNo & &

Objects.equal (firstName, student.firstName) & &

Objects.equal (lastName, student.lastName) & &

Objects.equal (className, student.className)

}

@ Override

Public int hashCode () {

Return Objects.hashCode (firstName, lastName, rollNo, className)

}

Public String getFirstName () {

Return firstName

}

Public void setFirstName (String firstName) {

This.firstName = firstName

}

Public String getLastName () {

Return lastName

}

Public void setLastName (String lastName) {

This.lastName = lastName

}

Public int getRollNo () {

Return rollNo

}

Public void setRollNo (int rollNo) {

This.rollNo = rollNo

}

Public String getClassName () {

Return className

}

Public void setClassName (String className) {

This.className = className

}

@ Override

Public int compareTo (Student student) {

Return ComparisonChain.start (). Compare (this.firstName, student.firstName). Result (this.lastName, student.lastName).

}

}

Output result:

Root::root

Achesh::Parashar

Mahesh::Parashar

Mbhesh::Pbrashar

Mbhesh::Pcrashar

Suresh::null

Student {Name=Mbhesh Pcrashar, Class=VI, Roll No=1}

Student {firstName=Mbhesh, lastName=Pbrashar}

Ordering class:

/ / you can skip implementing Comparator and directly inherit Ordering

Ordering byLengthOrdering = new Ordering () {

@ Override

Public int compare (String str1, String str2) {

Return Ints.compare (str2.length (), str1.length ())

}

}

List train = new ArrayList ()

Train.add ("a")

Train.add ("aba")

Train.add ("abbb")

Train.add ("bbba")

Train.add ("aaaaa")

Collections.sort (train, byLengthOrdering)

System.out.println (train.toString ())

Results: [aaaaa, abbb, bbba, aba, a]

/ / when chained, read from back to front. Null values can be easily controlled.

Ordering fruitOrdering = Ordering.natural () .nullsFirst () .onResultOf (new Function () {

@ Nullable

@ Override

Public String apply (@ Nullable Fruit fruit) {

Return fruit.shape

}

});

List fruits = new ArrayList ()

Fruit doubleApple = new Fruit ("Apple", "Oval")

Fruits.add (doubleApple)

Fruits.add (new Fruit ("orange", "round"))

Fruits.add (new Fruit (durian, null))

Fruits.add (doubleApple)

Collections.sort (fruits, fruitOrdering)

For (Fruit fruit: fruits) {

System.out.println (fruit.name+ "::" + fruit.shape)

}

Class Fruit {

String name

@ Nullable String shape

Public Fruit (String name, String shape) {

This.name = name

This.shape = shape

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

@ Nullable

Public String getShape () {

Return shape

}

Public void setShape (@ Nullable String shape) {

This.shape = shape

}

}

Results:

Durian:: null

Oranges: round

Apple:: Oval

Apple:: Oval

These are the basic tools in guava shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Result ellipse apple foundation content circle method durian orange analysis output professional small and medium rich in content character string that is tools articles right and wrong Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Microsoft Docker OPPO Reno