
com.hc360.rsf.common.utils.CollectionUtils Maven / Gradle / Ivy
The newest version!
/**
* CollectionUtils.java 2012-5-10
* Copyright(c) 2000-2012 HC360.COM, All Rights Reserved.
*/
package com.hc360.rsf.common.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CollectionUtils {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List sort(List list) {
if (list != null && list.size() > 0) {
Collections.sort((List)list);
}
return list;
}
private static final Comparator SIMPLE_NAME_COMPARATOR = new Comparator() {
public int compare(String s1, String s2) {
if (s1 == null && s2 == null) {
return 0;
}
if (s1 == null) {
return -1;
}
if (s2 == null) {
return 1;
}
int i1 = s1.lastIndexOf('.');
if (i1 >= 0) {
s1 = s1.substring(i1 + 1);
}
int i2 = s2.lastIndexOf('.');
if (i2 >= 0) {
s2 = s2.substring(i2 + 1);
}
return s1.compareToIgnoreCase(s2);
}
};
public static List sortSimpleName(List list) {
if (list != null && list.size() > 0) {
Collections.sort(list, SIMPLE_NAME_COMPARATOR);
}
return list;
}
public static Map> splitAll(Map> list, String separator) {
if (list == null) {
return null;
}
Map> result = new HashMap>();
for (Map.Entry> entry : list.entrySet()) {
result.put(entry.getKey(), split(entry.getValue(), separator));
}
return result;
}
public static Map> joinAll(Map> map, String separator) {
if (map == null) {
return null;
}
Map> result = new HashMap>();
for (Map.Entry> entry : map.entrySet()) {
result.put(entry.getKey(), join(entry.getValue(), separator));
}
return result;
}
public static Map split(List list, String separator) {
if (list == null) {
return null;
}
Map map = new HashMap();
if (list == null || list.size() == 0) {
return map;
}
for (String item : list) {
int index = item.indexOf(separator);
if (index == -1) {
map.put(item, "");
} else {
map.put(item.substring(0, index), item.substring(index + 1));
}
}
return map;
}
public static List join(Map map, String separator) {
if (map == null) {
return null;
}
List list = new ArrayList();
if (map == null || map.size() == 0) {
return list;
}
for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (value == null || value.length() == 0) {
list.add(key);
} else {
list.add(key + separator + value);
}
}
return list;
}
public static boolean mapEquals(Map, ?> map1, Map, ?> map2) {
if (map1 == null && map2 == null) {
return true;
}
if (map1 == null || map2 == null) {
return false;
}
if (map1.size() != map2.size()) {
return false;
}
for (Map.Entry, ?> entry : map1.entrySet()) {
Object key = entry.getKey();
Object value1 = entry.getValue();
Object value2 = map2.get(key);
if (! objectEquals(value1, value2)) {
return false;
}
}
return true;
}
private static boolean objectEquals(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
return true;
}
if (obj1 == null || obj2 == null) {
return false;
}
return obj1.equals(obj2);
}
public static Map toStringMap(String... pairs) {
Map parameters = new HashMap();
if (pairs.length > 0) {
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("pairs must be even.");
}
for (int i = 0; i < pairs.length; i = i + 2) {
parameters.put(pairs[i], pairs[i + 1]);
}
}
return parameters;
}
@SuppressWarnings("unchecked")
public static Map toMap(Object ... pairs) {
Map ret = new HashMap();
if (pairs == null || pairs.length == 0) return ret;
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("Map pairs can not be odd number.");
}
int len = pairs.length / 2;
for (int i = 0; i < len; i ++) {
ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);
}
return ret;
}
private CollectionUtils() {
}
/**
* 判断集合容器是否为空
*
* @param coll
* @return
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy