Guava of Joiner and Splitter

Source: Internet
Author: User

Recently in the preparation of a guava for customers to share, so will continue to update about guava more details to share. This document will record guava string processing joiner (connection) and splitter (split) processing.

Joiner

First, let's take a look at one of the cases we often meet:

题目:对于一个如下定义List   List<String> list =of("1", "2", null, “3”);按照’,’分割,并过滤掉null。

How do we continue without a third-party library, such as Common-lange,guava, with native Java?

1 2 3 4 5 6 7 8 9 Ten  One  A  -  -  the
Public   static String join(List stringlist,  String delimiter) {?  StringBuilder builder = new StringBuilder();     ?  for (Object Item : stringlist) {?     if (item ! = null) {?         Builder            . Append (item)              . Append (delimiter); ?             }         ? }?     builder. SetLength (builder. length () delimiter. length ());   ? return Builder . toString (); ?   }

is not very simple, but cumbersome, and there is a pit, we use append way, son ah every time for the completion of, we have to fix the last separator: Builder.setlength (Builder.length () Delimiter.length ());

What about the guava version?

1 2 3 4 5 6
Public   static String joinbyguava( List  Stringlist, String delimiter) {?  returnJoiner           .  on (delimiter)                  . Skipnulls ()                 . Join (stringlist);                  }  

We are not considering more details, and we are semantically telling the reader of the code what delimiter to use to filter the null value and join.

Note: Of course we can also use Common-lange to very simple completion: Stringutils.join (Stringlist, delimiter). But personal recommendation to use guava instead of common-lange, because guava there are more useful methods, follow-up will be introduced, there are more semantics.

Splittermapjoinner and Mapsplitter

The best case for Mapjoinner and Mapsplitter is the Param encoding of the URL.

Mapjoinner
题目:生产一个查询id: 123,name: green的学生信息的url。

The code for using Guava's Mapjoinner is as follows:

 joiner   on   (  "&"   withkeyvalueseparator   (  "="   join   ( immutablemap   of   (  "id"    "123"    "name"    "green"     
TD class= "Code" >
1  

Here the delimiter between the on incoming map item is used, and the withkeyvalueseparator passes the delimiter between the map item Key/value. So it's easy to implement without having to implement a for loop code.

Mapsplitter
题目:对url中的查询字符串"id=123&name=green"进行分割

The code for using Guava's mapsplitter is as follows:

final  map  <  string   string  >  join  =  splitter   on   (  "&"   withkeyvalueseparator   (  "="   split   (  "Id=123&name=green"     
TD class= "Code" >
1  

This also takes advantage of the first delimiter on the passed-in string, withkeyvalueseparator the delimiter of the passed-in item, resulting in the Key/value entry of the map, which results in a map object of {id=123, name=green}.

Guava of Joiner and Splitter

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.