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

后端 未结 8 1561
不思量自难忘°
不思量自难忘° 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:09

    I don't have the StringUtil library available (I have no choice over that) so using standard Java I came up with this ..

    If you're confident that your set data won't include any commas or square brackets, you could use:

    mySet.toString().replaceAll("\\[|\\]","").replaceAll(","," ");
    

    A set of "a", "b", "c" converts via .toString() to string "[a,b,c]".

    Then replace the extra punctuation as necesary.

    Filth.

    0 讨论(0)
  • 2020-12-05 04:12

    Maybe a shorter solution:

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

    or

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

    but native, definitely faster

    public String test76 (Set<String> set) {
        return String.join(" ", set);
    }
    
    0 讨论(0)
  • 2020-12-05 04:13

    I use this method:

    public static String join(Set<String> set, String sep) {
        String result = null;
        if(set != null) {
            StringBuilder sb = new StringBuilder();
            Iterator<String> it = set.iterator();
            if(it.hasNext()) {
                sb.append(it.next());
            }
            while(it.hasNext()) {
                sb.append(sep).append(it.next());
            }
            result = sb.toString();
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-05 04:20

    I'm confused about the code replication, why not factor it into a function that takes one set and returns one string?

    Other than that, I'm not sure that there is much that you can do, except maybe giving the stringbuilder a hint about the expected capacity (if you can calculate it based on set size and reasonable expectation of string length).

    There are library functions for this as well, but I doubt they're significantly more efficient.

    0 讨论(0)
  • 2020-12-05 04:21

    As a counterpoint to Seanizer's commons-lang answer, if you're using Google's Guava Libraries (which I'd consider the 'successor' to commons-lang, in many ways), you'd use Joiner:

    Joiner.on(" ").join(set_1);
    

    with the advantage of a few helper methods to do things like:

    Joiner.on(" ").skipNulls().join(set_1);
    // If 2nd item was null, would produce "1, 3"
    

    or

    Joiner.on(" ").useForNull("<unknown>").join(set_1);
    // If 2nd item was null, would produce "1, <unknown>, 3"
    

    It also has support for appending direct to StringBuilders and Writers, and other such niceties.

    0 讨论(0)
  • 2020-12-05 04:28

    If you are using Java 8, you can use the native

    String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
    

    method:

    Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. For example:

     Set<String> strings = new LinkedHashSet<>();
     strings.add("Java"); strings.add("is");
     strings.add("very"); strings.add("cool");
     String message = String.join("-", strings);
     //message returned is: "Java-is-very-cool"
    

    Set implements Iterable, so simply use:

    String.join(" ", set_1);
    
    0 讨论(0)
提交回复
热议问题