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