高级集合——字符串拼接

2017-01-12  本文已影响0人  hello高world

字符串拼接

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

上一篇下一篇

猜你喜欢

热点阅读