高级集合——字符串拼接

字符串拼接

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

你可能感兴趣的:(高级集合——字符串拼接)