Guava Joiner and Mapjoiner thread safety

Source: Internet
Author: User

  • {@code Joiner} and {@code Mapjoiner} instances are always immutable (see the Java Concurrency Programming section for more information)

  • [1. The state cannot be modified after it is created. 2. All fields are final type, and it is created correctly (no escape from this reference occurs during creation)]

  • Create an internal call constructor by using the static function on, and for setting the null type, an anonymous class is generated inside the function, overloading the related function that sets the null type.

  • So they're all thread-safe. Their constructors are private, and their static methods must be called to generate their objects (without reflecting the constructed object case)

    public class Chapter2code {private final list<string> List = lists.newarraylist ("name", "Doctor", "Sex", "man", null ); @Rulepublic expectedexception expectedexception = Expectedexception.none (); @Testpublic void Test_joiner () {Joiner Jo    Iner = Joiner.on (","). Skipnulls ();    Assertthat (Joiner.join (list), Equalto ("Name,doctor,sex,man"));    Joiner = Joiner.on (","). Usefornull ("null");    Assertthat (Joiner.join (list), Equalto ("Name,doctor,sex,man,null"));    Joiner = Joiner.on ("|");    Expectedexception.expect (Nullpointerexception.class);//If NULL pointers are not processed, an exception is thrown. Joiner.join (list);}    @Testpublic void Test_joiner_wrong_use () {Joiner Joiner = Joiner.on (",");    Joiner.skipnulls ();//This method generates a new object that has no effect on the settings of the original object Expectedexception.expect (Nullpointerexception.class); Joiner.join (list);} @Testpublic void Test_joiner_appendto_stringbuilder () {Joiner Joiner = Joiner.on ("|").    Skipnulls ();    StringBuilder builder = new StringBuilder (256);    Joiner.appendto (builder, list); Assertthat (BUIlder.tostring (), Equalto ("Name|doctor|sex|man")); System.out.println (Builder.tostring ());}    @Testpublic void Test_joiner_appendto_file () throws IOException {File File = new file ("A.txt");    if (!file.exists ()) {file.createnewfile (); } try (FileWriter FileWriter = new FileWriter (file);)    {Joiner.on ("#"). Skipnulls (). AppendTo (FileWriter, list);    } catch (Exception e) {throw new RuntimeException (e);    } list<string> lines = Files.readalllines (Paths.get ("a.txt"));    SYSTEM.OUT.PRINTLN (lines); Assertthat (Joiner.on (""). Join (lines), Equalto (Joiner.on ("#"). Skipnulls (). Join (list));}    @Testpublic void Test_mapjoiner () {map<string, string> Map = Maps.newlinkedhashmap ();    Map.put ("Name", "Doctor");    Map.put ("Sex", "man");    String join = Joiner.on (","). Withkeyvalueseparator ("="). Join (map); Assertthat (Join, Equalto ("Name=doctor,sex=man"));}}

Guava Joiner and Mapjoiner thread safety

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.