Guava Pack Learning---Lists

Source: Internet
Author: User

The guava package is recommended by my colleagues in my recent project and is a library that Google has launched. There are a lot of features, including collections, caches, native type support, concurrency libraries, general annotations, string processing, Io, and more. We use guava dependencies in our projects, but in fact we use only a small subset of them, such as the Declaration and processing of collections and functional styles.

Say less nonsense, first:

We will find that there are too many things, basically all add up to have hundreds of thousands of classes, but often used in fact dozens of classes. In fact, you can set up a common-utils package in the project to copy part of the guava class in the past, without having to rely on all guava to come in.

The most use of work is list, set, map, STRING,I/O, first look at lists bar.

true )publicfinalclass Lists {}

This is the declaration of the lists tool in guava, this gwtcompatible annotation has two parameters, one is the serializable one is imulated, is a Boolean value, indicating whether it is necessary to support serialization, Whether the emulation (in fact, is inconsistent with the JVM's default implementation). The comments in the guava are very detailed and there are a lot of methods, but in fact I often use only a few list declarations and some functions to split the list, so I only care about those methods. Look at the method of guava lists class:

In fact, many of the methods are using generics to simplify the operation of the usual manual knocking. Some methods in jdk1.7 and later should have become deprecated.

 @GwtCompatible (serializable = true  )  public  static  <E> Arraylist<e> newarraylist (iterable<? extends  E> elements) {checknotnull (elements);  //  for GWT  //     Let ArrayList's sizing logic work, if possible  return  (Elements instanceof   Collection) ? new  Arraylist<e> (Collections2.cast (elements)): Newarraylist (Elements.iterator ()  ); }

There are several overloaded methods in lists, such as Newarraylist and newlinkedlist, which actually simplify some work by using generics. There may be some questions about my use of lists.newarraylist (XXX) and direct new arraylist<?> () What is the difference, in fact, is to save the typing Kung fu. For example, if you create a list of list<map<string,map<string,object>> like this, you'll have to go to the keyboard if you want to use new. In fact, using guava to declare the only way to use the overloaded method is:

list<map<string,map<string,object>>> list = Lists.newarraylist ();

In fact, behind the creation of LinkedList, copyonwritearraylist and so on, no longer look.

One more useful way is to split the list:

 Public Static int size) {    checknotnull (list);     > 0);     return instanceof randomaccess)         New Randomaccesspartition<t>(list, size)        new partition<t>(list, size);  } 

For example, when you need to request someone else's API to pass in the parameter, the number of arguments is limited, you can split and then request the results in order.

Randomaccess means speeding up access, not guaranteeing order but allowing all elements to have constant time to get. The detailed code for partition is listed below:

Private Static classPartition<t>extendsAbstractlist<list<t>> {    FinalList<t>list; Final intsize; Partition (List<T> list,intsize) {       This. List =list;  This. Size =size; } @Override PublicList<t> Get (intindex)      {Checkelementindex (index, size ()); intStart = Index *size; intEnd = Math.min (start +size, list.size ()); returnlist.sublist (start, end); } @Override Public intsize () {returnintmath.divide (List.size (), size, roundingmode.ceiling); } @Override Public BooleanIsEmpty () {returnList.isEmpty (); }  }

That is, when you get it, it goes to the sublist operation.

And then there are some very small features:

  @Beta  publicstatic immutablelist<character> charactersof ( String string) {    returnnew  stringasimmutablelist (Checknotnull (String));  }

Gets the immutable char of a string.

  @Beta  publicstatic list<character> charactersof (charsequence Sequence) {    returnnew  charsequenceaslist (checknotnull (sequence));  }

Get a list of charsquence

@CheckReturnValue Public Static<T> list<t> Reverse (list<t>list) {    if(Listinstanceofimmutablelist) {      return((immutablelist<t>list). reverse (); } Else if(Listinstanceofreverselist) {      return((reverselist<t>list). Getforwardlist (); } Else if(Listinstanceofrandomaccess) {      return NewRandomaccessreverselist<t>(list); } Else {      return NewReverselist<t>(list); }  }

Reverses a list.

There are still many methods of rewriting and static protected, but they are common. Then go and see the sets class.

Guava Pack Learning---Lists

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.