首页

基于springframework的集合处理工具类CollectionUtils对常见对象查找包含转换操作

标签:类集工具,collectionutils,springframework,源码,集合转换     发布时间:2017-12-12   

一、前言

基于spring-core(4.1.4)的org.springframework.util.CollectionUtils集合工具类,对常见集合collect和对象、集合List、Map、数组间的转换、包含关系等之间相关操作,具体参见下面二源码说明。

二、源码说明

1.CollectionUtils源码如下(其中org.springframework.util.ObjectUtils说明参见其他文章

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 <E> void mergeArrayIntoCollection(Object array, Collection<E> 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 < i; ++j) { Object elem = arrayOfObject1[j];@b@      collection.add(elem);@b@    }@b@  }@b@@b@  public static <K, V> void mergePropertiesIntoMap(Properties props, Map<K, V> 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> E findFirstMatch(Collection<?> source, Collection<E> 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> T findValueOfType(Collection<?> collection, Class<T> 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 < i; ++j) { Class type = arrayOfClass[j];@b@      Object value = findValueOfType(collection, type);@b@      if (value != null)@b@        return value;@b@    }@b@@b@    return null;@b@  }@b@@b@  public static boolean hasUniqueObject(Collection<?> 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, E extends A> A[] toArray(Enumeration<E> 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 <E> Iterator<E> toIterator(Enumeration<E> enumeration)@b@  {@b@    return new EnumerationIterator(enumeration);@b@  }@b@@b@  public static <K, V> MultiValueMap<K, V> toMultiValueMap(Map<K, List<V>> map)@b@  {@b@    return new MultiValueMapAdapter(map);@b@  }@b@@b@  public static <K, V> MultiValueMap<K, V> 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<K, V>@b@    implements MultiValueMap<K, V>, Serializable@b@  {@b@    private final Map<K, List<V>> map;@b@@b@    public MultiValueMapAdapter(Map<K, List<V>> 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<K, V> values)@b@    {@b@      for (Map.Entry entry : values.entrySet())@b@        set(entry.getKey(), entry.getValue());@b@    }@b@@b@    public Map<K, V> 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<V> get(Object key)@b@    {@b@      return ((List)this.map.get(key));@b@    }@b@@b@    public List<V> put(K key, List<V> value)@b@    {@b@      return ((List)this.map.put(key, value));@b@    }@b@@b@    public List<V> remove(Object key)@b@    {@b@      return ((List)this.map.remove(key));@b@    }@b@@b@    public void putAll(Map<? extends K, ? extends List<V>> m)@b@    {@b@      this.map.putAll(m);@b@    }@b@@b@    public void clear()@b@    {@b@      this.map.clear();@b@    }@b@@b@    public Set<K> keySet()@b@    {@b@      return this.map.keySet();@b@    }@b@@b@    public Collection<List<V>> values()@b@    {@b@      return this.map.values();@b@    }@b@@b@    public Set<Map.Entry<K, List<V>>> 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<E>@b@    implements Iterator<E>@b@  {@b@    private Enumeration<E> enumeration;@b@@b@    public EnumerationIterator(Enumeration<E> 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@}