概述
一、前言
基于spring-core(4.1.4)的org.springframework.util.CollectionUtils集合工具类,对常见集合collect和对象、集合List、Map、数组间的转换、包含关系等之间相关操作,具体参见下面二源码说明。
二、源码说明package org.springframework.util;@b@@b@import java.io.Serializable;@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Collections;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.LinkedHashMap;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Properties;@b@import java.util.Set;@b@@b@public abstract class CollectionUtils@b@{@b@ public static boolean isEmpty(Collection> collection)@b@ {@b@ return ((collection == null) || (collection.isEmpty()));@b@ }@b@@b@ public static boolean isEmpty(Map, ?> map)@b@ {@b@ return ((map == null) || (map.isEmpty()));@b@ }@b@@b@ public static List arrayToList(Object source)@b@ {@b@ return Arrays.asList(ObjectUtils.toObjectArray(source));@b@ }@b@@b@ public static void mergeArrayIntoCollection(Object array, Collection collection)@b@ {@b@ if (collection == null)@b@ throw new IllegalArgumentException("Collection must not be null");@b@@b@ Object[] arr = ObjectUtils.toObjectArray(array);@b@ Object[] arrayOfObject1 = arr; int i = arrayOfObject1.length; for (int j = 0; j void mergePropertiesIntoMap(Properties props, Map map)@b@ {@b@ Enumeration en;@b@ if (map == null)@b@ throw new IllegalArgumentException("Map must not be null");@b@@b@ if (props != null)@b@ for (en = props.propertyNames(); en.hasMoreElements(); ) {@b@ String key = (String)en.nextElement();@b@ Object value = props.getProperty(key);@b@ if (value == null)@b@ {@b@ value = props.get(key);@b@ }@b@ map.put(key, value);@b@ }@b@ }@b@@b@ public static boolean contains(Iterator> iterator, Object element)@b@ {@b@ while ((iterator != null) && @b@ (iterator.hasNext())) {@b@ Object candidate = iterator.next();@b@ if (ObjectUtils.nullSafeEquals(candidate, element))@b@ return true;@b@@b@ }@b@@b@ return false;@b@ }@b@@b@ public static boolean contains(Enumeration> enumeration, Object element)@b@ {@b@ while ((enumeration != null) && @b@ (enumeration.hasMoreElements())) {@b@ Object candidate = enumeration.nextElement();@b@ if (ObjectUtils.nullSafeEquals(candidate, element))@b@ return true;@b@@b@ }@b@@b@ return false;@b@ }@b@@b@ public static boolean containsInstance(Collection> collection, Object element)@b@ {@b@ Iterator localIterator;@b@ if (collection != null)@b@ for (localIterator = collection.iterator(); localIterator.hasNext(); ) { Object candidate = localIterator.next();@b@ if (candidate == element)@b@ return true;@b@ }@b@@b@@b@ return false;@b@ }@b@@b@ public static boolean containsAny(Collection> source, Collection> candidates)@b@ {@b@ if ((isEmpty(source)) || (isEmpty(candidates)))@b@ return false;@b@@b@ for (Iterator localIterator = candidates.iterator(); localIterator.hasNext(); ) { Object candidate = localIterator.next();@b@ if (source.contains(candidate))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public static E findFirstMatch(Collection> source, Collection candidates)@b@ {@b@ if ((isEmpty(source)) || (isEmpty(candidates)))@b@ return null;@b@@b@ for (Iterator localIterator = candidates.iterator(); localIterator.hasNext(); ) { Object candidate = localIterator.next();@b@ if (source.contains(candidate))@b@ return candidate;@b@ }@b@@b@ return null;@b@ }@b@@b@ public static T findValueOfType(Collection> collection, Class type)@b@ {@b@ if (isEmpty(collection))@b@ return null;@b@@b@ Object value = null;@b@ for (Iterator localIterator = collection.iterator(); localIterator.hasNext(); ) { Object element = localIterator.next();@b@ if ((type == null) || (type.isInstance(element))) {@b@ if (value != null)@b@ {@b@ return null;@b@ }@b@ value = element;@b@ }@b@ }@b@ return value;@b@ }@b@@b@ public static Object findValueOfType(Collection> collection, Class>[] types)@b@ {@b@ if ((isEmpty(collection)) || (ObjectUtils.isEmpty(types)))@b@ return null;@b@@b@ Class[] arrayOfClass = types; int i = arrayOfClass.length; for (int j = 0; j collection)@b@ {@b@ if (isEmpty(collection))@b@ return false;@b@@b@ boolean hasCandidate = false;@b@ Object candidate = null;@b@ for (Iterator localIterator = collection.iterator(); localIterator.hasNext(); ) { Object elem = localIterator.next();@b@ if (!(hasCandidate)) {@b@ hasCandidate = true;@b@ candidate = elem;@b@ }@b@ else if (candidate != elem) {@b@ return false;@b@ }@b@ }@b@ return true;@b@ }@b@@b@ public static Class> findCommonElementType(Collection> collection)@b@ {@b@ if (isEmpty(collection))@b@ return null;@b@@b@ Class candidate = null;@b@ for (Iterator localIterator = collection.iterator(); localIterator.hasNext(); ) { Object val = localIterator.next();@b@ if (val != null)@b@ if (candidate == null) {@b@ candidate = val.getClass();@b@ }@b@ else if (candidate != val.getClass())@b@ return null;@b@@b@ }@b@@b@ return candidate;@b@ }@b@@b@ public static A[] toArray(Enumeration enumeration, A[] array)@b@ {@b@ ArrayList elements = new ArrayList();@b@ while (enumeration.hasMoreElements())@b@ elements.add(enumeration.nextElement());@b@@b@ return elements.toArray(array);@b@ }@b@@b@ public static Iterator toIterator(Enumeration enumeration)@b@ {@b@ return new EnumerationIterator(enumeration);@b@ }@b@@b@ public static MultiValueMap toMultiValueMap(Map> map)@b@ {@b@ return new MultiValueMapAdapter(map);@b@ }@b@@b@ public static MultiValueMap unmodifiableMultiValueMap(MultiValueMap extends K, ? extends V> map)@b@ {@b@ Assert.notNull(map, "'map' must not be null");@b@ Map result = new LinkedHashMap(map.size());@b@ for (Map.Entry entry : map.entrySet()) {@b@ List values = Collections.unmodifiableList((List)entry.getValue());@b@ result.put(entry.getKey(), values);@b@ }@b@ Map unmodifiableMap = Collections.unmodifiableMap(result);@b@ return toMultiValueMap(unmodifiableMap);@b@ }@b@@b@ private static class MultiValueMapAdapter@b@ implements MultiValueMap, Serializable@b@ {@b@ private final Map> map;@b@@b@ public MultiValueMapAdapter(Map> map)@b@ {@b@ Assert.notNull(map, "'map' must not be null");@b@ this.map = map;@b@ }@b@@b@ public void add(K key, V value)@b@ {@b@ List values = (List)this.map.get(key);@b@ if (values == null) {@b@ values = new LinkedList();@b@ this.map.put(key, values);@b@ }@b@ values.add(value);@b@ }@b@@b@ public V getFirst(K key)@b@ {@b@ List values = (List)this.map.get(key);@b@ return ((values != null) ? values.get(0) : null);@b@ }@b@@b@ public void set(K key, V value)@b@ {@b@ List values = new LinkedList();@b@ values.add(value);@b@ this.map.put(key, values);@b@ }@b@@b@ public void setAll(Map values)@b@ {@b@ for (Map.Entry entry : values.entrySet())@b@ set(entry.getKey(), entry.getValue());@b@ }@b@@b@ public Map toSingleValueMap()@b@ {@b@ LinkedHashMap singleValueMap = new LinkedHashMap(this.map.size());@b@ for (Map.Entry entry : this.map.entrySet())@b@ singleValueMap.put(entry.getKey(), ((List)entry.getValue()).get(0));@b@@b@ return singleValueMap;@b@ }@b@@b@ public int size()@b@ {@b@ return this.map.size();@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return this.map.isEmpty();@b@ }@b@@b@ public boolean containsKey(Object key)@b@ {@b@ return this.map.containsKey(key);@b@ }@b@@b@ public boolean containsValue(Object value)@b@ {@b@ return this.map.containsValue(value);@b@ }@b@@b@ public List get(Object key)@b@ {@b@ return ((List)this.map.get(key));@b@ }@b@@b@ public List put(K key, List value)@b@ {@b@ return ((List)this.map.put(key, value));@b@ }@b@@b@ public List remove(Object key)@b@ {@b@ return ((List)this.map.remove(key));@b@ }@b@@b@ public void putAll(Map extends K, ? extends List> m)@b@ {@b@ this.map.putAll(m);@b@ }@b@@b@ public void clear()@b@ {@b@ this.map.clear();@b@ }@b@@b@ public Set keySet()@b@ {@b@ return this.map.keySet();@b@ }@b@@b@ public Collection> values()@b@ {@b@ return this.map.values();@b@ }@b@@b@ public Set>> entrySet()@b@ {@b@ return this.map.entrySet();@b@ }@b@@b@ public boolean equals(Object other)@b@ {@b@ if (this == other)@b@ return true;@b@@b@ return this.map.equals(other);@b@ }@b@@b@ public int hashCode()@b@ {@b@ return this.map.hashCode();@b@ }@b@@b@ public String toString()@b@ {@b@ return this.map.toString();@b@ }@b@ }@b@@b@ private static class EnumerationIterator@b@ implements Iterator@b@ {@b@ private Enumeration enumeration;@b@@b@ public EnumerationIterator(Enumeration enumeration)@b@ {@b@ this.enumeration = enumeration;@b@ }@b@@b@ public boolean hasNext()@b@ {@b@ return this.enumeration.hasMoreElements();@b@ }@b@@b@ public E next()@b@ {@b@ return this.enumeration.nextElement();@b@ }@b@@b@ public void remove() throws UnsupportedOperationException@b@ {@b@ throw new UnsupportedOperationException("Not supported");@b@ }@b@ }@b@}
最后
以上就是清爽黑夜为你收集整理的collectionutils包_基于springframework的集合处理工具类CollectionUtils对常见对象查找包含转换操作...的全部内容,希望文章能够帮你解决collectionutils包_基于springframework的集合处理工具类CollectionUtils对常见对象查找包含转换操作...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复