java拓展集合工具类CollectionUtils

网友投稿 322 2023-02-11


java拓展集合工具类CollectionUtils

拓展集合工具类CollectionUtils,供大家参考,具体内容如下

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;

/**

*

*

自定义集合工具类拓展常用方法

* @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;

/**

*

*

拆分List为固定大小的多个集合

*

推荐使用

*

返回集合的size越小,此方法性能越高

* @param baseList

* @param size

* @return ArrayList

* @autho 董杨炀

* @time 2017-4-10 上午11:30:43

*/

@SuppressWarnings("unchecked")

public static List> fastSplitList(List 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> resultList = new ArrayList>();

for (int i = 0; i < arrSize; i++) {

if (arrSize - 1 == i) {

resultList.add((List) new ArrayList( baseList.subList(i * size, baseList.size())));

} else {

resultList.add((List) new ArrayList( baseList.subList(i * size, size * (i + 1))));

}

}

return resultList;

}

/**

*

*

拆分List为固定大小的多个集合

*

返回集合的size越大,此方法性能越高

* @param baseList

* @param size

* @return ArrayList

* @autho 董杨炀

* @time 2017-4-10 上午11:30:43

*/

public static List> splitList(List baseList, int size) {

if (baseList == null || baseList.size() == 0) {

return null;

}

if (size <= 0) {

size = DEFAULT_SIZE;

}

List> resultList = new ArrayList>();

for (int i = 0; i < baseList.size(); ++i) {

if (i % size == 0) {

List result = new ArrayList();

resultList.add(result);

}

resultList.get(i / size).add(baseList.get(i));

}

return resultList;

}

/**

*

*

集合转Set

* @param coll 源集合

* @param keyType 属性类型

* @param keyMethodName 属性get方法

* @return LinkedHashSet

* @autho 董杨炀

* @time 2017-4-10 上午11:31:50

*/

public static Set asSet(final java.util.Collection coll,final Class keyType

,final String keyMethodName) {

if (CollectionUtils.isEmpty(coll)) {

return new HashSet(0);

}

final Set set = new LinkedHashSet(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;

}

/**

*

*

集合转Map

*

比如:List,讲EmployeeEntity的name属性作为key,转换成Map

* @param coll 源集合

* @param keyType 属性类型

* @param valueType 源数据类型(实体类型)

* @param keyMethodName 属性get方法

* @return LinkedHashMap

* @autho 董杨炀

* @time 2017-4-10 上午11:32:01

*/

public static Map asMap(final java.util.Collection coll,final Class keyType

,final Class valueType,final String keyMethodName) {

if (CollectionUtils.isEmpty(coll)) {

return new LinkedHashMap(0);

}

final Map map = new LinkedHashMap(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;

}

/**

*

*

集合转List

* @param coll

* @return ArrayList

* @autho 董杨炀

* @time 2017-4-10 上午11:32:56

*/

public static List asList(final java.util.Collection coll) {

if (CollectionUtils.isEmpty(coll)) {

return new ArrayList(0);

}

final List list = new ArrayList();

for (final V value : coll) {

if (value != null) {

list.add(value);

}

}

return list;

}

/**

*

集合toString

* @param collection 泛型必须为String类型

* @param split 比如连接符","

* @return

* @autho 董杨炀

* @time 2017-4-10 下午3:22:24

*/

public static String collToString(Collection collection, String split) {

StringBuilder sb = new StringBuilder();

if (collection != null) {

int i = 0, size = collection.size();

for (Iterator 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 baseList = new ArrayList(1000000);

for (int i = 0; i < 1000000; i++) {

baseList.add("data:"+" i");

}

long currentTimeMillis1 = System.currentTimeMillis();

List> splitList = splitList(baseList, 1000);

long currentTimeMillis2 = System.currentTimeMillis();

System.out.println(splitList.size());

System.out.println("切割完成时间为:"+String.valueOf(currentTimeMillis2-currentTimeMillis1)+"毫秒");

long currentTimeMillis3 = System.currentTimeMillis();

List> newList = fastSplitList(baseList,1000);

long currentTimeMillis4 = System.currentTimeMillishttp://();

System.out.println(newList.size());

System.out.println("快速切割完成时间为:"+String.valueOf(currentTimeMillis4-currentTimeMillis3)+"毫秒");

List employeeList = new ArrayList();

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 set = CollectionUtils.asSet(employeeList, String.class, "getName");

for (String name : set) {

System.out.println(name);

}

Map map = CollectionUtils.asMap(employeeList, String.class, EmployeeEntity.class, "getName");

Set keySet = map.keySet();

for (String key : keySet) {

System.out.println(key);

System.out.println(map.get(key));

}

List list = CollectionUtils.asList(map.values());

for (EmployeeEntity employeeEntity : list) {

System.out.println(employeeEntity);

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用Vue构建可重用的分页组件
下一篇:Angular 组件之间的交互的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~