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

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

    I use this method:

    public static String join(Set set, String sep) {
        String result = null;
        if(set != null) {
            StringBuilder sb = new StringBuilder();
            Iterator it = set.iterator();
            if(it.hasNext()) {
                sb.append(it.next());
            }
            while(it.hasNext()) {
                sb.append(sep).append(it.next());
            }
            result = sb.toString();
        }
        return result;
    }
    

提交回复
热议问题