Java examples for java.util:Collection Join
join collection with separator
//package com.java2s; import java.util.Collection; import java.util.Map; import java.util.Map.Entry; public class Main { public static void main(String[] argv) { Collection list = java.util.Arrays.asList("asdf", "java2s.com"); String separator = "java2s.com"; System.out.println(join(list, separator)); }/*from w w w . j a va 2 s . c o m*/ public static String join(Collection<? extends Object> list, String separator) { StringBuilder sb = new StringBuilder(); for (Object o : list) { sb.append(o.toString()); sb.append(separator); } if (sb.length() > 0 && separator.length() > 0) { sb.delete(sb.length() - separator.length(), sb.length()); } return sb.toString(); } public static String join(Map<? extends Object, ? extends Object> map, String keyValueSeparator, String entrySeparator) { StringBuilder sb = new StringBuilder(); for (Entry<? extends Object, ? extends Object> entry : map .entrySet()) { sb.append(entry.getKey().toString()); sb.append(keyValueSeparator); sb.append(entry.getValue().toString()); sb.append(entrySeparator); } if (sb.length() > 0 && entrySeparator.length() > 0) { sb.delete(sb.length() - entrySeparator.length(), sb.length()); } return sb.toString(); } }