[Guava Study Notes] Basic Utilities: Null, precondition, Object method, sorting, exception, guava Study Notes

Source: Internet
Author: User
Tags iterable

[Guava Study Notes] Basic Utilities: Null, precondition, Object method, sorting, exception, guava Study Notes

My technical blog is often maliciously crawled and reproduced by rogue websites. Please move to the original article: Workshop.

Null

Guava uses Optional to indicate T-type references that may be null.

Create:Optional. of (T) does not accept nullOptional. fromNullable (T) accepts nullOptional. absent ()

Query:IsPresent ()Get ()Or (T) if it is null, It is TOrNull ()

Others:Objects. firstNonNull (T, T) is used to specify the default value for null. If both are null, NullPointerException is thrown.

    EmptyToNull (String ),NullToEmpty (String)

Prerequisites

Usage:

checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);checkArgument(i < j, "Expected i < j, but %s > %s", i, j);

CheckArgument (boolean)CheckNotNull (T)CheckState (boolean)

CheckElementIndex (int index, int size)CheckPositionIndexes (int start, int end, int size)

Inline use:This. field = checkNotNull (field)

Object Method

Objects. equals (a, B) equals that can be nullObjects. hash (Object ...)

ToString ():

// Returns "ClassName{x=1}"Objects.toStringHelper(this).add("x", 1).toString();// Returns "MyObject{x=1}"Objects.toStringHelper("MyObject").add("x", 1).toString();

Compare ():

public int compareTo(Foo that) {    return ComparisonChain.start()            .compare(this.aString, that.aString)            .compare(this.anInt, that.anInt)            .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())            .result();}
Sort

Ordering: Comparator implementation

Structure: natural order of natural ()UsingToString () Lexicographic OrderFrom (Comparator)

Chain call:Reverse ()NullsFirst ()NullsLast ()Lexicographical ()OnResultOf (Function)

Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {  public String apply(Foo foo) {    return foo.sortedBy;  }});

Set and element:GreatestOf (Iterable iterable, int k)Min (Iterable)Min (E, E ...)

IsOrdered (Iterable)SortedCopy (Iterable)




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.