Fastest way to put contents of Set to a single String with words separated by a whitespace?

后端 未结 8 1560
不思量自难忘°
不思量自难忘° 2020-12-05 03:34

I have a few Sets and want to transform each of these into a single String where each element of the original Set is sep

8条回答
  •  有刺的猬
    2020-12-05 04:12

    Maybe a shorter solution:

    public String test78 (Set set) {
        return set
            .stream()
            .collect(Collectors.joining(" "));
    }
    

    or

    public String test77 (Set set) {
        return set
            .stream()
            .reduce("", (a,b)->(a + " " + b));
    }
    

    but native, definitely faster

    public String test76 (Set set) {
        return String.join(" ", set);
    }
    

提交回复
热议问题