Collection sublimation of Google's guava

Source: Internet
Author: User
As for guava, I will not talk much about it here. The previous article has already been very detailed! This is mainly to share with you some of guava's Sublimation processing of the collection. Well, let's just move on to the Code:
Package COM. joyce. guava. bean;/*** Student Entity class ** @ author Joyce. luo * @ date 02:37:19 */public class student {/*** student ID */private integer stuid;/*** name */private string stuname; /*** age */private integer stuage;/*** @ return the stuid */Public integer getstuid () {return stuid ;} /*** @ Param stuid * The stuid to set */Public void setstuid (integer stuid) {This. stuid = stuid;}/*** @ return the stuname */Public String getstuname () {return stuname ;} /*** @ Param stuname * The stuname to set */Public void setstuname (string stuname) {This. stuname = stuname;}/*** @ return the stuage */Public integer getstuage () {return stuage ;} /*** @ Param stuage * The stuage to set */Public void setstuage (integer stuage) {This. stuage = stuage;}/*****/Public student () {super ();} /*** @ Param stuid * @ Param stuname * @ Param stuage */Public student (integer stuid, string stuname, integer stuage) {super (); this. stuid = stuid; this. stuname = stuname; this. stuage = stuage ;}}

The entity class is available, mainly for the convenience of use in the collection! The key lies in:

/*** @ Description: ** @ title: setguava. java * @ package COM. joyce. guava. main * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:03:53 * @ version V2.0 */package COM. joyce. guava. main; import Java. util. collections; import Java. util. list; import Java. util. map; import COM. google. common. collect. arraylistmultimap; import COM. google. common. collect. bimap; import COM. google. common. coll ECT. classtoinstancemap; import COM. google. common. collect. hashbasedtable; import COM. google. common. collect. hashbimap; import COM. google. common. collect. hashmultiset; import COM. google. common. collect. lists; import COM. google. common. collect. multimap; import COM. google. common. collect. multiset; import COM. google. common. collect. mutableclasstoinstancemap; import COM. google. common. collect. ordering; import COM. googl E. common. collect. table; import COM. joyce. guava. bean. student;/*** @ Description: Guava collection ** @ classname: setguava * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 2014-6-26 11:03:53 * @ version V2.0 */public class setguava {public static void main (string [] ARGs) {/*** guava API provides a useful new collection type, which works well together with existing Java collections. They are multimap, Multiset, * Table, bimap, classtoinstancemap */system. out. println ("multimap: a map with repeated keys. The child classes include listmultimap and setmultimap. The corresponding keys are used to obtain list and set"); testmultimap (); system. out. println ("Multiset: Not a set, you can add repeated elements and count the number of repeated elements"); testmulitiset (); system. out. println ("table: equivalent to map with two keys"); testtable (); system. out. println ("bimap: is a one-to-one ing. You can get the value through the key, or you can get the key through the value"); testbitmap (); system. out. println ("Classtoinstancemap: the map key is not just a type"); testclasstoinstancemap (); system. out. println ("sorting, which is a very flexible comparison class of guava, can be used for operation and expansion. as a comparator, sorting provides many control over the sorting of sets "); testorder ();}/*** @ Description: multimap: a map with repeated keys. subclasses include listmultimap and setmultimap, the corresponding list and set *** @ title: setguava are obtained by using the key respectively. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:19:50 * @ version V2.0 */Private Static void Test Multimap () {multimap <string, student> customersbytype = arraylistmultimap. create (); customersbytype. put ("ABC", new student (1, "Joyce", 20); customersbytype. put ("ABC", new student (1, "Joyce one", 20); customersbytype. put ("ABC", new student (1, "Joyce two", 20); customersbytype. put ("ABC", new student (1, "Joyce three", 20); customersbytype. put ("ABCD", new student (1, "Joyce four", 20); customersbytype. P Ut ("ABCDE", new student (1, "Joyce five", 20); system. out. println (mermersbytype. get ("ABC "). size (); For (student STU: customersbytype. get ("ABC") {system. out. println (Stu. getstuname () ;}}/*** @ Description: Multiset: Not a set. You can add duplicate elements and calculate the number of repeated elements. *** @ title: setguava. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:19:59 * @ version V2.0 */Private Static void tes Tmulitiset () {Multiset <integer> Multiset = hashmultiset. create (); Multiset. add (10); Multiset. add (30); Multiset. add (30); Multiset. add (40); system. out. println (Multiset. count (30); // 2 -- count the number of times XX appears. system. out. println (Multiset. size (); // 4 -- number of elements}/*** @ Description: Table: equivalent to map with two keys *** @ title: setguava. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:24:43 * @ ver Sion V2.0 */Private Static void testtable () {table <integer, integer, student> persontable = hashbasedtable. create (); persontable. put (1, 20, new student (1, "46546", 20); persontable. put (0, 30, new student (2, "46546", 30); persontable. put (0, 25, new student (3, "46546", 25); persontable. put (1, 50, new student (4, "46546", 50); persontable. put (0, 27, new student (5, "46546", 27); persontable. put (1, 29, n EW student (6, "46546", 29); persontable. put (0, 38, new student (7, "46546", 38); persontable. put (1, 66, new student (8, "46546", 66); // obtain the map of the row set <integer, student> rowmap = persontable. row (0); integer rowmax = collections. max (rowmap. keyset (); system. out. println (rowmax);}/*** @ Description: bimap: it is a one-to-one ing. You can get the value through the key or the key through the value *** @ title: setguava. java * @ copyright: Copyright (c) 2014 **@ Author comsys-lzp * @ date 11:36:59 * @ version V2.0 */Private Static void testbitmap () {// bidirectional mapbimap <integer, string> bimap = hashbimap. create (); bimap. put (1, "hello"); bimap. put (2, "helloa"); bimap. put (3, "world"); bimap. put (4, "worldb"); bimap. put (5, "my"); bimap. put (6, "ACC"); // obtain valuestring value = bimap using the key. get (5); system. out. println ("key -- [5]; Value -- [" + value + "]"); // obtain K through value Eyinteger key = bimap. inverse (). get ("my"); system. out. println ("Value -- [my]; key -- [" + key + "]");}/*** @ Description: classtoinstancemap: Sometimes, the key of your map is not one type. They are many types. You want to map them to get this type. * guava provides classtoinstancemap to meet this purpose. ** Apart from inheriting from the map interface, classtoinstacemap provides Methods T getinstance (class <t>) and * t putinstance (class <t>, T), eliminating forced type conversion. ** This class has a simple type of parameter, usually called B, which represents the upper-layer binding of map control. For example, * classtoinstancemap <number> numberdefaults = * mutableclasstoinstancemap. create (); * numberdefaults. putinstance (integer. class, * integer. valueof (0); ** technically, classtoinstancemap <B> implements Map <class <? Extends B>, * B>, or, This Is A ing from the subclass of B to the object B, which may make the generic type of classtoinstancemap slightly messy, * But remember that B is always the upper-layer binding type of map. Generally, B is only an object. Guava provides useful implementations, including * mutableclasstoinstancemap and immutableclasstoinstancemap. * emphasis: for other Map <class, Object> and classtoinstancemap * Native projects, a native type and its corresponding packaging class can be mapped to different values; * *** @ title: setguava. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:42:52 * @ version V2.0 */Private Static void testclasstoinstancemap () {classtoinstancemap <student> classtoinstancemap = mutableclasstoinstancemap. create (); Student Stu = new student (1, "Joyce", 20); classtoinstancemap. putinstance (student. class, Stu); Student stuobj = classtoinstancemap. getinstance (student. class); system. out. println (stuobj. getstuname ();}/*** @ Description: sorting. It is a very flexible comparison class of guava and can be used for operations, extensions, and comparator, sorting provides many control over set sorting. *** @ title: setguava. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:49:30 * @ version V2.0 */Private Static void testorder () {list <integer> numberlist = lists. newarraylist (30, 20, 60, 80, 10); system. out. println (ordering. natural (). sortedcopy (numberlist); // 10, 20, 30, 60, 80 system. out. println (ordering. natural (). reverse (). sortedcopy (numberlist); // 80, 60, 30, 20, 10system. out. println (ordering. natural (). min (numberlist); // 10system. out. println (ordering. natural (). max (numberlist); // 80 numberlist = lists. newarraylist (30, 20, 60, 80, null, 10); system. out. println (ordering. natural (). nullslast (). sortedcopy (numberlist); // 10, 20, 30, 60, 80, nullsystem. out. println (ordering. natural (). nullsfirst (). sortedcopy (numberlist); // null, 10, 20, 30, 60, 80 }}
Effect

I believe that if you have understood the above Code, you will understand it !!! In fact, Guava also provides other methods in the collection, so we will not share them here. If you are interested, you can study it on your own! Guava resources: http://download.csdn.net/detail/luo201227/7207227, OK, this is now! Next time I have the opportunity to share with you other sublimation ideas in guava...

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.