java 通用字符串连接 支持List Set Map等集合 很通用

我们经常会遇到对数组、集合类中的对象中的某个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));
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值