Expand the collection tool class CollectionUtils for your reference. The specific content is as follows
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.List;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>Common methods for custom collection tool class expansion</p> * @autho Dong Yangyang* @time 2017-4-10 11:33:36 am */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>Split List to multiple sets of fixed size</p> * <p>Recommended use</p> * <p>Return the smaller the size of the set, the higher the performance of this method</p> * @param baseList * @param size * @return ArrayList * @autho Dong Yangyang* @time 2017-4-10 11:30:43 am */ @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>Split List to multiple sets of fixed size</p> * <p>Return the larger the size of the set, the higher the performance of this method</p> * @param baseList * @param size * @return ArrayList * @autho Dong Yangyang* @time 2017-4-10 11:30:43 am */ 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 to Set</p> * @param col Source collection* @param keyType Attribute type* @param keyMethodName Attribute get method* @return LinkedHashSet * @autho Dong Yangyang* @time 2017-4-10 11:31:50 am */ 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>Collection to Map</p> * <p>For example: List<EmployeeEntity>, talk about the name attribute of EmployeeEntity as a key and convert it into Map</p> * @param coll Source collection* @param keyType Property type* @param valueType Source data type (entity type) * @param keyMethodName Property get method* @return LinkedHashMap * @autho Dong Yangyang* @time 2017-4-10 11:32:01 am */ 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>Collection to List</p> * @param coll * @return ArrayList * @autho Dong Yangyang* @time 2017-4-10 11:32:56 am */ 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> Collection<String>toString</p> * @param collection The generic must be of String type* @param split For example, the connector "," * @return * @autho Dong Yangyang* @time 2017-4-10 3:22:24 pm */ public static String colToString(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("The cutting completion time is:"+String.valueOf(currentTimeMillis2-currentTimeMillis1)+"ms"); long currentTimeMillis3 = System.currentTimeMillis(); List<List<String>> newList = fastSplitList(baseList,1000); long currentTimeMillis4 = System.currentTimeMillis(); System.out.println(newList.size()); System.out.println("Quick cutting completion time is:"+String.valueOf(currentTimeMillis4-currentTimeMillis3)+"ms"); List<EmployeeEntity> employeeList = new ArrayList<EmployeeEntity>(); for (int i = 1; i < 11; i++) { EmployeeEntity entity = new EmployeeEntity(); entity.setName("name"+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); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.