java拓展集合工具類(lèi)CollectionUtils
拓展集合工具類(lèi)CollectionUtils,供大家參考,具體內(nèi)容如下
package com.demo.utils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.demo.bean.EmployeeEntity;
/**
*
* <p>自定義集合工具類(lèi)拓展常用方法</p>
* @autho 董楊煬
* @time 2017-4-10 上午11:33:36
*/
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(CollectionUtils.class);
private static final int DEFAULT_SIZE = 1000;
/**
*
* <p>拆分List為固定大小的多個(gè)集合</p>
* <p>推薦使用</p>
* <p>返回集合的size越小,此方法性能越高</p>
* @param baseList
* @param size
* @return ArrayList
* @autho 董楊煬
* @time 2017-4-10 上午11:30:43
*/
@SuppressWarnings("unchecked")
public static <T> List<List<T>> fastSplitList(List<T> baseList, int size) {
if (baseList == null || baseList.size() == 0) {
return null;
}
if (size <= 0) {
size = DEFAULT_SIZE;
}
int arrSize = baseList.size() % size == 0 ? baseList.size() / size : baseList.size() / size + 1;
List<List<T>> resultList = new ArrayList<List<T>>();
for (int i = 0; i < arrSize; i++) {
if (arrSize - 1 == i) {
resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, baseList.size())));
} else {
resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, size * (i + 1))));
}
}
return resultList;
}
/**
*
* <p>拆分List為固定大小的多個(gè)集合</p>
* <p>返回集合的size越大,此方法性能越高</p>
* @param baseList
* @param size
* @return ArrayList
* @autho 董楊煬
* @time 2017-4-10 上午11:30:43
*/
public static <T> List<List<T>> splitList(List<T> baseList, int size) {
if (baseList == null || baseList.size() == 0) {
return null;
}
if (size <= 0) {
size = DEFAULT_SIZE;
}
List<List<T>> resultList = new ArrayList<List<T>>();
for (int i = 0; i < baseList.size(); ++i) {
if (i % size == 0) {
List<T> result = new ArrayList<T>();
resultList.add(result);
}
resultList.get(i / size).add(baseList.get(i));
}
return resultList;
}
/**
*
* <p>集合轉(zhuǎn)Set</p>
* @param coll 源集合
* @param keyType 屬性類(lèi)型
* @param keyMethodName 屬性get方法
* @return LinkedHashSet
* @autho 董楊煬
* @time 2017-4-10 上午11:31:50
*/
public static <K, V> Set<K> asSet(final java.util.Collection<V> coll,final Class<K> keyType
,final String keyMethodName) {
if (CollectionUtils.isEmpty(coll)) {
return new HashSet<K>(0);
}
final Set<K> set = new LinkedHashSet<K>(coll.size());
try {
for (final V value : coll) {
Object object;
Method method = value.getClass().getMethod(keyMethodName);
object = method.invoke(value);
@SuppressWarnings("unchecked")
final K key = (K) object;
set.add(key);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new CollectionUtilsException("Collection conversion Set exceptions");
}
return set;
}
/**
*
* <p>集合轉(zhuǎn)Map</p>
* <p>比如:List<EmployeeEntity>,講EmployeeEntity的name屬性作為key,轉(zhuǎn)換成Map</p>
* @param coll 源集合
* @param keyType 屬性類(lèi)型
* @param valueType 源數(shù)據(jù)類(lèi)型(實(shí)體類(lèi)型)
* @param keyMethodName 屬性get方法
* @return LinkedHashMap
* @autho 董楊煬
* @time 2017-4-10 上午11:32:01
*/
public static <K, V> Map<K, V> asMap(final java.util.Collection<V> coll,final Class<K> keyType
,final Class<V> valueType,final String keyMethodName) {
if (CollectionUtils.isEmpty(coll)) {
return new LinkedHashMap<K, V>(0);
}
final Map<K, V> map = new LinkedHashMap<K, V>(coll.size());
try {
Method method = valueType.getMethod(keyMethodName);
for (final V value : coll) {
Object object;
object = method.invoke(value);
@SuppressWarnings("unchecked")
final K key = (K) object;
map.put(key, value);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new CollectionUtilsException("Collection conversion Map exceptions");
}
return map;
}
/**
*
* <p>集合轉(zhuǎn)List</p>
* @param coll
* @return ArrayList
* @autho 董楊煬
* @time 2017-4-10 上午11:32:56
*/
public static <V> List<V> asList(final java.util.Collection<V> coll) {
if (CollectionUtils.isEmpty(coll)) {
return new ArrayList<V>(0);
}
final List<V> list = new ArrayList<V>();
for (final V value : coll) {
if (value != null) {
list.add(value);
}
}
return list;
}
/**
* <p>集合<String>toString</p>
* @param collection 泛型必須為String類(lèi)型
* @param split 比如連接符","
* @return
* @autho 董楊煬
* @time 2017-4-10 下午3:22:24
*/
public static String collToString(Collection<String> collection, String split) {
StringBuilder sb = new StringBuilder();
if (collection != null) {
int i = 0, size = collection.size();
for (Iterator<String> iterator = collection.iterator(); iterator.hasNext();) {
String str = iterator.next();
sb.append(str);
if (++i < size) {
sb.append(split);
}
}
}
return sb.toString();
}
static class CollectionUtilsException extends RuntimeException{
private static final long serialVersionUID = 1L;
public CollectionUtilsException(String s) {
super(s);
}
public CollectionUtilsException(String s, Throwable e) {
super(s, e);
}
public CollectionUtilsException(Throwable e) {
super(e);
}
}
public static void main(String[] args) {
List<String> baseList = new ArrayList<String>(1000000);
for (int i = 0; i < 1000000; i++) {
baseList.add("data:"+" i");
}
long currentTimeMillis1 = System.currentTimeMillis();
List<List<String>> splitList = splitList(baseList, 1000);
long currentTimeMillis2 = System.currentTimeMillis();
System.out.println(splitList.size());
System.out.println("切割完成時(shí)間為:"+String.valueOf(currentTimeMillis2-currentTimeMillis1)+"毫秒");
long currentTimeMillis3 = System.currentTimeMillis();
List<List<String>> newList = fastSplitList(baseList,1000);
long currentTimeMillis4 = System.currentTimeMillis();
System.out.println(newList.size());
System.out.println("快速切割完成時(shí)間為:"+String.valueOf(currentTimeMillis4-currentTimeMillis3)+"毫秒");
List<EmployeeEntity> employeeList = new ArrayList<EmployeeEntity>();
for (int i = 1; i < 11; i++) {
EmployeeEntity entity = new EmployeeEntity();
entity.setName("名字"+String.valueOf(i));
employeeList.add(entity);
}
System.out.println(employeeList.size());
Set<String> set = CollectionUtils.asSet(employeeList, String.class, "getName");
for (String name : set) {
System.out.println(name);
}
Map<String, EmployeeEntity> map = CollectionUtils.asMap(employeeList, String.class, EmployeeEntity.class, "getName");
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key);
System.out.println(map.get(key));
}
List<EmployeeEntity> list = CollectionUtils.asList(map.values());
for (EmployeeEntity employeeEntity : list) {
System.out.println(employeeEntity);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot + WebSocket 實(shí)現(xiàn)答題對(duì)戰(zhàn)匹配機(jī)制案例詳解
這篇文章主要介紹了SpringBoot + WebSocket 實(shí)現(xiàn)答題對(duì)戰(zhàn)匹配機(jī)制,分別為每個(gè)用戶(hù)擬定四種在線狀態(tài),通過(guò)流程圖給大家展示,需要的朋友可以參考下2021-05-05
身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn)
這篇文章主要介紹了身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn),本文講解了18身份證號(hào)碼的結(jié)構(gòu)、根據(jù)17位數(shù)字本體碼獲取最后一位校驗(yàn)碼程序?qū)嵗葍?nèi)容,需要的朋友可以參考下2015-06-06
詳解SpringBoot基礎(chǔ)之banner玩法解析
SpringBoot項(xiàng)目啟動(dòng)時(shí)會(huì)在控制臺(tái)打印一個(gè)默認(rèn)的啟動(dòng)圖案,這個(gè)圖案就是我們要講的banner,這篇文章主要介紹了SpringBoot基礎(chǔ)之banner玩法解析,感興趣的小伙伴們可以參考一下2019-04-04
深入學(xué)習(xí)Spring Cloud-Ribbon
這篇文章主要介紹了Spring Cloud-Ribbon的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧2021-03-03
Java Cache詳解及簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了 Java Cache詳解及簡(jiǎn)單實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-02-02
DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說(shuō)明
這篇文章主要介紹了DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
使用@Cacheable緩存解決雙冒號(hào)::的問(wèn)題
這篇文章主要介紹了使用@Cacheable緩存解決雙冒號(hào)::的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

