首页

定义CollectionUtils集合工具类实现数组转List/合并集合/属性合并Map/集合查找/集合不重复校验等处理代码示例

标签:CollectionUtils,集合工具类     发布时间:2018-11-12   

一、前言

通过定义集合工具类CollectionUtils(依赖的 ObjectUtils参见其他文章),实现对象转集合序列List(arrayToList)、合并数组到集合中(mergeArrayIntoCollection)、合并属性配置文件到Map(mergePropertiesIntoMap)、集合遍历Iterator集/实例集/是否包含对象(contains)、两集合包含关系校验(containsAny)、获取两集合第一个相当匹配对象(findFirstMatch)、查询集合指定Class类型的对象(findValueOfType)、判断集合对象不重复校验(hasUniqueObject)等。

二、代码示例

import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Properties;@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@    for (Object elem : arr)@b@      collection.add(elem);@b@  }@b@@b@  public static void mergePropertiesIntoMap(Properties props, Map map)@b@  {@b@    if (map == null)@b@      throw new IllegalArgumentException("Map must not be null");@b@@b@    if (props != null)@b@      for (Enumeration 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@    if (iterator != null)@b@      while (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@    if (enumeration != null)@b@      while (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@    if (collection != null)@b@      for (Iterator localIterator = collection.iterator(); localIterator.hasNext(); ) { Object candidate = localIterator.next();@b@        if (candidate != element) break label28;@b@        label28: 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))) break label45;@b@      label45: return true;@b@    }@b@@b@    return false;@b@  }@b@@b@  public static Object 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))) break label45;@b@      label45: 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)))) break label50;@b@      if (value != null)@b@      {@b@        return null;@b@      }@b@      label50: 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@    for (Class type : types) {@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@        break label50: }@b@      if (candidate == elem) break label50;@b@      label50: 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) break label54;@b@      if (candidate == null) {@b@        candidate = val.getClass();@b@        break label54: }@b@      if (candidate == val.getClass()) break label54;@b@      label54: return null;@b@    }@b@@b@    return candidate;@b@  }@b@@b@  public static <E> Iterator<E> toIterator(Enumeration<E> enumeration)@b@  {@b@    return new EnumerationIterator(enumeration);@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@      return this.enumeration.hasMoreElements();@b@    }@b@@b@    public E next() {@b@      return this.enumeration.nextElement();@b@    }@b@@b@    public void remove() throws UnsupportedOperationException {@b@      throw new UnsupportedOperationException("Not supported");@b@    }@b@  }@b@}