Guava Self-study note one

Source: Internet
Author: User

  Chance Contact Guava, for me this small white, Google's things are of course tall on the, so holding learning (installed) the mentality of learning (forced), on the Internet to learn from the predecessors. Of course, I learn guava mainly for a few purposes, the first of course is to think about whether guava can be applied to their own weekday code, if the guava reference can make the writing of the Code more concise and elegant, but also improve the performance of the program, reduce unpredictable errors, of course, is very good. Of course, our purpose is certainly not limited to this, when can be very good application to the project, forgot to say, guava should be a tool class collection, inside for many of our usual objects, methods carried out a simple package, Rather than calling Java native methods to extend the method by calling guava, this package should not be too simple. In view of this, the ultimate goal of guava learning should be through the source of the analysis to learn its cool ideas. (written to this, already confused)         Zhang San and John Doe said let him go to find Harry daughter-in-law, first regardless of the purpose of the next door to the old king-in-law, if the next door old Wang moved, of course, should be unmasked, to catch the night road, then this time the old king can not find, And how to find his daughter-in-law. This should be understood in the program, the null pointer is abnormal. There is no big thing, but your so inexplicable empty pointers are not not elegant, you say no way, Lao Wang is not at home. Well, now to each family home to a post housekeeper, you go to ask the housekeeper, people do not know the housekeeper, so he will not be inexplicably spit you a blank pointer. That guava has the butler free.         Who's the housekeeper, yes, that's optional.         optional assigns a housekeeper to null, and now you want an empty object, as long as          optional< Integer> nullableopt=optional.fromnullable (NULL);        just fine, So nullableopt This object is a null pointer to the object, now so Nullableopt.ispresent () Call this method returns whether the object is empty, return a Boolean, Not NULL is true. Of course you don't have to save a null in. In general you will have a variable like a, then you        optional<integer> aopt=optional.fromnullable (a);        So you can do that if ( Aopt.ispresent ()) {} Determines if it is empty, and if not empty, how to get the value of the object managed by the steward. Get pair.         Possible.get () like this, you can take the specific value of the jurisdiction. You may not be able to, this value has not been stored in, of course, this value is supposed to be stored in, it is also very convenient. There are two ways to store specific values          optional<integer> possible=optional.of (6); If NULL is saved, run the Times null pointer          optional<integer> nonullableopt= Optional.fromnullable (10); and this is the second way, you will find that the fromnullable can either be null or save a specific object, so is so cool. Of course if you really want to take an empty, Optional also has a full-time method for you to flirt with         optional.absent () The returned object, the Ispresent () method such as false, That is, the housekeeper clearly told you that Lao Wang was gone. Ran away.         Quietly tell you two ways         For example         optional<integer > nullableopt=optional.fromnullable (null); this         You can use the asset method         Set<integer> set=nullableopt.asset (); Get a set, of course the set size () is 1&nbsp       In addition, now that you have a problem with this, you have to look at the value inside that is not your original value, like this         Optional<Integer>  bopt=optional.fromnullable (12); You put a 12, and then you forget, you say how you forget         Okay, forget it's okay, I'm a dead horse, bopt.or (8) If this is really the current 8, will give you back to the original deposit in the 8, which is obviously not ah, it's OK, dead horse when live horse, take you pass in this 8 charge sucks, you know I know, just fine. Oh..         at the beginning we want to get the value of this existence is not first ispresent () judge, this is not uncomfortable, nothing, there is a trick         Bopen.ornull The master is immutable, so that if it is NULL, it returns NULL if it is a specific object or value, it would have been given to him.         Today's class meeting, open here, adjourned.   Son ~~        Wait, leave a small code ~ ~
 Packagecom.test;ImportJava.util.Set;Importorg.junit.Test;Importcom.google.common.base.Optional; Public classoptionaltest {@Test Public voidTestoptional ()throwsException {Optional<Integer> Possible=optional.of (6); Optional<Integer> absentopt=optional.absent (); Optional<Integer> nullableopt=optional.fromnullable (NULL); Optional<Integer> Nonullableopt=optional.fromnullable (10); if(Possible.ispresent ()) {System.out.println ("Possible isPresent:" +possible.ispresent ()); System.out.println ("Possible value:" +possible.get ()); }        if(Absentopt.ispresent ()) {System.out.println ("Absentopt isPresent:" +absentopt.ispresent ());; } System.out.println ("Fromnullableopt isPresent:" +nullableopt.ispresent ());; if(Nonullableopt.ispresent ()) {System.out.println ("Nonullableopt isPresent:" +nonullableopt.ispresent ());; }} @Test Public voidTestmethodreturn () {Optional<Long> value =method (); if(value.ispresent () = =true) {System.out.println ("Get return Value:" +value.get ()); }Else{System.out.println ("Get return Value:" + value.or ( -12l)); } System.out.println ("Get Return value Ornull:" +value.ornull ()); Optional<Long> Valuenonull =Methodnonull (); if(valuenonull.ispresent () = =true) {Set<Long> set=Valuenonull.asset (); System.out.println ("Get the size of the return value set:" +set.size ()); System.out.println ("Get return Value:" +valuenonull.get ()); }Else{System.out.println ("Get return Value:" + valuenonull.or ( -12l)); } System.out.println ("Get Return value Ornull:" +valuenonull.ornull ()); }    PrivateOptional<long>method () {returnOptional.fromnullable (NULL); }    PrivateOptional<long>Methodnonull () {returnOptional.fromnullable (15L); }}

  

Guava Self-study note one

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.