Java8-Stream join

   /**
     * 生成连接字符串
     * join("_", 1)        => 1
     * join("_", 1, 2)     => 1_2
     * join("_", 1, 2, 3)  => 1_2_3
     * @param delimiter
     * @param objects
     * @return
     */
    public static String join(String delimiter, Object... objects) {
        Stream.Builder keyStreamBuilder = Stream.builder();
        Arrays.stream(objects).map(o -> {
            if (o instanceof String) {
                return (String) o;
            } else {
                return String.valueOf(o);
            }
        }).forEach(keyStreamBuilder::add);
        return keyStreamBuilder.build().collect(Collectors.joining(delimiter));
    }

你可能感兴趣的:(Java8,stream,join)