一、前言

基于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对常见对象查找包含转换操作...相关推荐

  1. java递归生成树结构_突破CRUD | 万能树Java工具类封装(源码)

    0.学完本文你或许可以收获 感受一个树工具从初始逐步优化完善的过程 树工具封装的设计思考与实现思路 最后收获一款拿来即用的树工具源代码 对于前端树组件有一定了解和使用过的同学可直接跳跃到第3章节开始. ...

  2. 学习集合工具类CollectionUtils——List对象案例

    学习集合工具类CollectionUtils--List对象案例 一.依赖 二.案例 三.结果展示 一.依赖 <dependency><groupId>org.apache.c ...

  3. java(五)-迭代器,数据结构,List,Set ,TreeSet集合,Collections工具类

    day05[迭代器,数据结构,List,Set ,TreeSet集合,Collections工具类] 主要内容 Collection集合的遍历方式: 迭代器. foreach(增强for循环) JDK ...

  4. Java基础巩固(二)异常,多线程,线程池,IO流,Properties集合,IO工具类,字符流,对象流,Stream,Lambda表达式

    一.异常,多线程 学习目标 : 异常的概述 异常的分类 异常的处理方式 自定义异常 多线程入门 1 异常的概述 1.1 什么是异常? 异常就是程序出现了不正常情况 , 程序在执行过程中 , 数据导致程 ...

  5. (10)Java泛型-Map集合-集合框架工具类-可变参数-静态导入

    -- 部分1.5新特性Java泛型-Map集合-集合框架工具类 泛型 概述: JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制. 对于泛型可以这样理解: 没有使用泛型时,只要是对 ...

  6. 基于AFNetworking的封装的工具类

    基于AFNetworking的封装的工具类MXERequestService // // MXERequestService.h // testAFNetWorking // // Created b ...

  7. Java——操作集合的工具类:Collections

    Java 提供了一个操作 Set .List 和 Map 等集合的工具类 :Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作 转载于:https://www.cnb ...

  8. 操作集合的工具类Collections

    1       操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...

  9. java 集合操作工具包_java之操作集合的工具类--Collections

    Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...

  10. Day18JavaSE——Map集合Collections工具类集合案例练习

    Day18JavaSE--Map集合&Collections工具类&集合案例练习 文章目录 Day18JavaSE--Map集合&Collections工具类&集合案例 ...

最新文章

  1. 《github一天一道算法题》:并归排序
  2. win10无线投屏_日臻薄技 | 手机如何投屏到电脑
  3. 10_Eclipse中演示Git冲突的解决
  4. unity官方教程-TANKS(一)
  5. 成都优步uber司机第四组奖励政策
  6. Please read Security section of the manual to find out how to run mysqld as root!错误解决
  7. Stackoverflow 年度报告 2020:开发者最喜爱的数据库是什么?
  8. js中substr与substring的差别
  9. python 中间件
  10. java中$和 的区别详解_MyBatis中#{}和${}的区别详解
  11. get buffer from CMSampleBufferRef
  12. ISO50001认证辅导,ISO50001验厂优化所有流程中的能源性能,促进更高效的能源管理
  13. APP设计~切图那些事儿
  14. 如何查找专栏(知乎专栏汇总)
  15. QT封装exe和安装包详解
  16. Apache,mysql,PHP,lanp环境
  17. 日文windows系统 oracle数据库乱码的处理
  18. 数字排在最前,英文字母其次,汉字则按照拼音进行排序,获取中文首字母
  19. jsp80141汽车租赁租车车辆管理系统ssm+mysql
  20. 南京大学2021计算机考研复试线是多少,2021南京大学

热门文章

  1. Unity加载模块深度解析(网格篇)
  2. php不使用copy()函数复制文件的方法
  3. jdbc 连接oracle 数据库格式
  4. Silverlight socket组件
  5. 用ssh2连接linux实现putty功能范例代码
  6. TensorFlow应用实战-18-Policy Gradient算法
  7. 开发者论坛一周精粹(第六期):阿里B2B研发管理难题如何应对?打造强有力的技术中台...
  8. Android ProGuard 代码压缩混淆与打包优化
  9. Elasticsearch 基于ElastAlert发送邮件报警
  10. 【Cocos2d-Js基础教学(2)类的使用和面向对象】