Tips for using the Guava Toolkit in Java projects

Source: Internet
Author: User

Original: http://www. Zuidaima.com/blog/3182745969511424.htm

I've only heard of this toolkit before, but the real thing is in D75 demand. I'm going to implement the list set that the foreground passes in to the database with a semicolon-separated concatenation of a string

At the beginning I used the code logic as follows:

StringBuffer interfacelist=New  stringbuffer (); // determines whether the collection in the foreground binding is empty if (interfaces!=null) {     for(String str:interfaces) {          interfacelist.append ( STR). Append (";" );    } // remove the last semicolon     tpc.setinterfaces (interfacesstr.substrting(0,interfacelist.length ()-1);}

Feel the use of stringbuffer here is no problem, and then hery to me that the code can also be compressed, I saw the next Apache StringUtils this class and Google's guava package

The joiner of this class. I found that such a toolkit is really powerful, encapsulated a lot of features, learning is endless. The code above only needs to write a sentence to replace it:

String Interfacesstr=stringutils.join (Interfaces.iterator (), '; ');
or string interfacesstr= joiner.on (";"). Join (Interfaces.iterator ());

Sometimes we write like this:

Person person1= Thenew person ("Jack"); Person Person2=new person ("Jack", +); Person Person2=new person ("Jack", +); // will go to this list collection: List<Person> personlist=new arraylist<person>();p ersonlist.add (Person1); Personlist.add (Person2);p ersonlist.add (Person3);

This can be done in guava:list<person> list = Lists.newarraylist (Person1, Person2, Person3), of course arrays.aslist (Person1, Person2, Person3);

There are two ways to compare object sizes:

1. The entity class implements the Conparable interface; 2. Implement the Comparator interface in Collections.sort ()

In the Guava object comparison, the implementation of the comparator can be written like this:

New Comparator<person>() {            @Override            publicint  Compare (person O1, person O2) {                return  comparisonchain.start (). Compare (O1.getname (), O2.getname ()). Compare (O1.getage ( ), O2.getage ())                        . result ();}            }

There are a lot of features that you have to explore and learn, omitted here ...

Tips for using the Guava Toolkit in Java projects

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.