美文网首页
高级集合——字符串拼接

高级集合——字符串拼接

作者: hello高world | 来源:发表于2017-01-12 09:44 被阅读0次

    字符串拼接

    1.代码

    package org.java8.collector;
    
    import static java.util.stream.Collectors.joining;
    import java.util.stream.Stream;
    import lombok.extern.slf4j.Slf4j;
    import org.java8.vo.Artist;
    import com.google.common.base.Joiner;
    import com.google.common.base.Strings;
    
    @Slf4j
    public class StringJoining {
    
        public static void main(String[] args) {
            String result = Stream.of(new Artist("tinygao", "CN"),
                                      new Artist("jj", "US"),
                                      new Artist(null, "US"),
                                      new Artist("", "US")
                                      )
                                      .map(Artist::getName)
                                      .filter(x->!Strings.isNullOrEmpty(x))
                                      .collect(joining(",", "{", "}"));
            log.info(result);
            /**使用guava做字符串处理**/
            Joiner joiner = Joiner.on(',').skipNulls();
            log.info(joiner.join("wrong", null, "wrong"));
        }
    }
    

    2.输出结果

    09:15:35.038 [main] INFO org.java8.collector.StringJoining - {tinygao,jj}
    09:15:35.038 [main] INFO org.java8.collector.StringJoining - wrong,wrong

    相关文章

      网友评论

          本文标题:高级集合——字符串拼接

          本文链接:https://www.haomeiwen.com/subject/sqddbttx.html