我们经常会遇到对数组、集合类中的对象中的某个string类型字段进行join操作的情况,通常我们会手动遍历集合,提取string字段,然后拼接string,可是写出的代码要不就不通用,无法重用,要不逻辑判断复杂,容易出错,我封装了一个比较通用的join操作工具类,支持数组,List、Set 、Map 等集合,可自定义连接字符串,自定义字符串提取规则
使用示例:
List<String> str = new ArrayList<String>();
str.add("1");
str.add("2");
str.add("3");
str.add("4");
str.add("5");
str.add("6");
System.out.println(PowerJoin.join(str, new JoinStrHandle<String>() {
@Override
public String toStr(String t) {
return "'" + t + "'";
}
}));
Map<String, String> map = new HashMap<String, String>();
map.put(":a", "a");
map.put(":b", "b");
map.put(":c", "c");
map.put(":d", "d");
map.put(":e", "e");
JoinStrHandle<Entry<String, String>> h = new JoinStrHandle<Entry<String, String>>() {
@Override
public String toStr(Entry<String, String> t) {
return t.getKey() + "=>" + t.getValue();
}
};
System.out.println(PowerJoin.join(map.entrySet(), h, "*"));
String[] s = { "9", "8", "7", "6", "5", "4", "3", "2", "1" };
System.out.println(PowerJoin.join(str));
System.out.println(PowerJoin.join(map));
System.out.println(PowerJoin.join(map.keySet()));
System.out.println(PowerJoin.join(map.values()));
System.out.println("entrySet:=>"
+ PowerJoin.join(map.entrySet(),
new JoinStrHandle<Entry<String, String>>() {
@Override
public String toStr(Entry<String, String> t) {
return t.getKey() + "*" + t.getValue();
}
}));
System.out.println(PowerJoin.join(s));
输出:
'1','2','3','4','5','6'
:b=>b*:c=>c*:d=>d*:e=>e*:a=>a
1,2,3,4,5,6
:b=b,:c=c,:d=d,:e=e,:a=a
:b,:c,:d,:e,:a
b,c,d,e,a
entrySet:=>:b*b,:c*c,:d*d,:e*e,:a*a
9,8,7,6,5,4,3,2,1
package cn.vjsp.framework.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* 字符串连接,支持各种集合
*
* @author clojure-chaos-cat
* @since 2014-12-12
* @version 1.0
*/
public class PowerJoin {
/**
* 字符串连接
*
* @param iter
* 可迭代对象(如果为null,返回"")
* @param jsh
* 获取要链接字符串 ,如果为null,则调用toString方法
* @param separator
* 分隔符,默认为逗号
* @return
*
* @since 2014-12-02
* @author clojure-chaos
*/
public static <E> String join(Iterator<E> iter, JoinStrHandle<E> jsh,
String separ) {
if (iter == null || !iter.hasNext()) {
return "";
}
String separator = ",";
if (!isNull(separ)) {
separator = trim(separ);
}
StringBuilder rs = new StringBuilder();
while (iter.hasNext()) {
E e = iter.next();
if (e == null) {
continue;
}
String joinStr = trim(jsh == null ? e.toString() : jsh.toStr(e));
if (!isNull(joinStr)) {
rs.append(iter.hasNext() ? joinStr + separator : joinStr);
}
}
return rs.toString();
}
public static <E> String join(Iterator<E> iter, JoinStrHandle<E> jsh) {
return join(iter, jsh, null);
}
public static <E> String join(Iterator<E> iter, String separ) {
return join(iter, null, separ);
}
public static <E> String join(Iterator<E> iter) {
return join(iter, null, null);
}
/**
* 链接字符串
*
* @param ts
* @param jsh
* @param separ
* @return
*
* @since 2014-12-02
* @author clojure-chaos
*/
public static <E> String join(E[] ts, JoinStrHandle<E> jsh, String separ) {
if (ts == null || ts.length == 0) {
return "";
}
List<E> list = Arrays.asList(ts.clone());
return join(list, jsh, separ);
}
public static <E> String join(E[] ts, JoinStrHandle<E> jsh) {
return join(ts, jsh, null);
}
public static <E> String join(E[] ts, String separ) {
return join(ts, null, separ);
}
public static <E> String join(E[] ts) {
return join(ts, null, null);
}
/**
*
* @param coll
* @param jsh
* @param separ
* @return
*
* @since 2014-12-02
* @author clojure-chaos
*/
public static <E> String join(Collection<E> coll, JoinStrHandle<E> jsh,
String separ) {
if (coll == null || coll.size() == 0) {
return "";
}
return join(coll.iterator(), jsh, separ);
}
public static <E> String join(Collection<E> coll, JoinStrHandle<E> jsh) {
return join(coll, jsh, null);
}
public static <E> String join(Collection<E> coll, String separ) {
return join(coll, null, separ);
}
public static <E> String join(Collection<E> coll) {
return join(coll, null, null);
}
/**
*
* @param map
* @param jsh
* @param separ
* @return
*
* @since 2014-12-02
* @author clojure-chaos
*/
public static <K, V> String join(Map<K, V> map,
JoinStrHandle<Entry<K, V>> jsh, String separ) {
if (map == null || map.isEmpty()) {
return "";
}
return join(map.entrySet(), jsh, separ);
}
public static <K, V> String join(Map<K, V> map,
JoinStrHandle<Entry<K, V>> jsh) {
return join(map, jsh, null);
}
public static <K, V> String join(Map<K, V> map, String separ) {
return join(map, null, separ);
}
public static <K, V> String join(Map<K, V> map) {
return join(map, null, null);
}
public interface JoinStrHandle<E> {
/**
*
* @param t
* @return
*
* @since 2014-12-02
* @author clojure-chaos
*/
String toStr(E t);
}
private static boolean isNull(String str) {
return str == null || "".equals(str.trim());
}
private static String trim(String str) {
return isNull(str) ? "" : str.trim();
}
public static void main(String[] args) {
List<String> str = new ArrayList<String>();
str.add("1");
str.add("2");
str.add("3");
str.add("4");
str.add("5");
str.add("6");
System.out.println(PowerJoin.join(str, new JoinStrHandle<String>() {
@Override
public String toStr(String t) {
return "'" + t + "'";
}
}));
Map<String, String> map = new HashMap<String, String>();
map.put(":a", "a");
map.put(":b", "b");
map.put(":c", "c");
map.put(":d", "d");
map.put(":e", "e");
JoinStrHandle<Entry<String, String>> h = new JoinStrHandle<Entry<String, String>>() {
@Override
public String toStr(Entry<String, String> t) {
return t.getKey() + "=>" + t.getValue();
}
};
System.out.println(PowerJoin.join(map.entrySet(), h, "*"));
String[] s = { "9", "8", "7", "6", "5", "4", "3", "2", "1" };
System.out.println(PowerJoin.join(str));
System.out.println(PowerJoin.join(map));
System.out.println(PowerJoin.join(map.keySet()));
System.out.println(PowerJoin.join(map.values()));
System.out.println("entrySet:=>"
+ PowerJoin.join(map.entrySet(),
new JoinStrHandle<Entry<String, String>>() {
@Override
public String toStr(Entry<String, String> t) {
return t.getKey() + "*" + t.getValue();
}
}));
System.out.println(PowerJoin.join(s));
}
}