java拓展集合工具类CollectionUtils
程序员文章站
2022-06-11 12:11:23
拓展集合工具类collectionutils,供大家参考,具体内容如下
package com.demo.utils;
import java.lang.r...
拓展集合工具类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; /** * * <p>自定义集合工具类拓展常用方法</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为固定大小的多个集合</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为固定大小的多个集合</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>集合转set</p> * @param coll 源集合 * @param keytype 属性类型 * @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>集合转map</p> * <p>比如:list<employeeentity>,讲employeeentity的name属性作为key,转换成map</p> * @param coll 源集合 * @param keytype 属性类型 * @param valuetype 源数据类型(实体类型) * @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>集合转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类型 * @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("切割完成时间为:"+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("快速切割完成时间为:"+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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。