前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >工具类学习-CollectionUtils

工具类学习-CollectionUtils

作者头像
发布2019-08-29 11:22:42
1.8K0
发布2019-08-29 11:22:42
举报
文章被收录于专栏:WD学习记录

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://cloud.tencent.com/developer/article/1495108

CollectionUtils是日常经常会用到的一个工具类,在包org.apache.commons.collections中。

目前最常用到的两个方法是CollectionUtils.isEmpty()以及CollectionUtils.isNotEmpty()。

还有待补充一些使用实例

其中有一个私有的静态变量INTEGER_ONE:

代码语言:javascript
复制
private static Integer INTEGER_ONE = new Integer(1);

一个不可修改的空集合:

代码语言:javascript
复制
public static final Collection EMPTY_COLLECTION = UnmodifiableCollection.decorate(new ArrayList());

取并集方法:

代码语言:javascript
复制
public static Collection union(Collection a, Collection b) {

    ArrayList list = new ArrayList();

    Map mapa = getCardinalityMap(a);// 该方法以Collection中的元素作为key,出现次数作为value。

    Map mapb = getCardinalityMap(b);

    Set elts = new HashSet(a);

    elts.addAll(b);

    Iterator it = elts.iterator();



    while(it.hasNext()) {

        Object obj = it.next();

        int i = 0;



        for(int m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i)             {// getFrep是从指定map中获取该元素出现的次数

            list.add(obj);

        }

    }



    return list;

}

使用方法:

代码语言:javascript
复制
List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);

List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);

Collection<Integer> result = CollectionUtils.union(list1, list2);

for (Integer i : result) {

    System.out.print(i + " ");

}

输出如下:

1 1 2 3 4 5 7 9 9 9 9

取交集:

代码语言:javascript
复制
public static Collection intersection(final Collection a, final Collection b) {

    ArrayList list = new ArrayList();

    Map mapa = getCardinalityMap(a);

    Map mapb = getCardinalityMap(b);

    Set elts = new HashSet(a);

    elts.addAll(b);

    Iterator it = elts.iterator();

    while(it.hasNext()) {

    Object obj = it.next();

        for(int i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {

            list.add(obj);

        }

    }

    return list;
    
}

用法:

代码语言:javascript
复制
List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);

List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);

Collection<Integer> result = CollectionUtils.intersection(list1, list2);

for (Integer i : result) {

    System.out.print(i + " ");

}

输出:

1 5 7 9 9 9

取得交集之外的部分:

代码语言:javascript
复制
public static Collection disjunction(final Collection a, final Collection b) {

    ArrayList list = new ArrayList();

    Map mapa = getCardinalityMap(a);

    Map mapb = getCardinalityMap(b);

    Set elts = new HashSet(a);

    elts.addAll(b);

    Iterator it = elts.iterator();

    while(it.hasNext()) {

        Object obj = it.next();

        for(int i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++) {

            list.add(obj);

        }

    }

    return list;

}

用法:

代码语言:javascript
复制
List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);

List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);

Collection<Integer> result = CollectionUtils.disjunction(list1, list2);

for (Integer i : result) {

    System.out.print(i + " ");

}

输出:

1 2 3 4 9

从一个集合中移除另一个集合中存在的元素:

代码语言:javascript
复制
public static Collection subtract(final Collection a, final Collection b) {

    ArrayList list = new ArrayList( a );

    for (Iterator it = b.iterator(); it.hasNext();) {

        list.remove(it.next());

    }

    return list;

}

判断两个集合是否存在交集:

代码语言:javascript
复制
public static boolean containsAny(final Collection coll1, final Collection coll2) {

    if (coll1.size() < coll2.size()) {

        for (Iterator it = coll1.iterator(); it.hasNext();) {

            if (coll2.contains(it.next())) {

                return true;

            }

        }

    } else {

        for (Iterator it = coll2.iterator(); it.hasNext();) {

            if (coll1.contains(it.next())) {

                return true;

            }

        }

    }

    return false;

}

是否是子集:

代码语言:javascript
复制
public static boolean isSubCollection(final Collection a, final Collection b) {

    Map mapa = getCardinalityMap(a);

    Map mapb = getCardinalityMap(b);

    Iterator it = a.iterator();

    while (it.hasNext()) {

        Object obj = it.next();

        if (getFreq(obj, mapa) > getFreq(obj, mapb)) {

            return false;

        }

    }

    return true;

}

是否是真子集:

代码语言:javascript
复制
public static boolean isProperSubCollection(final Collection a, final Collection b) {

    return (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b);

}

判断两个集合是否相等:

代码语言:javascript
复制
public static boolean isEqualCollection(final Collection a, final Collection b) {

    if(a.size() != b.size()) {

        return false;

    } else {

        Map mapa = getCardinalityMap(a);

        Map mapb = getCardinalityMap(b);

        if(mapa.size() != mapb.size()) {

            return false;

        } else {

            Iterator it = mapa.keySet().iterator();

            while(it.hasNext()) {

                Object obj = it.next();

                if(getFreq(obj,mapa) != getFreq(obj,mapb)) {

                    return false;

                }

            }

            return true;

        }

    }

}

对象在集合中出现的次数:

代码语言:javascript
复制
public static int cardinality(Object obj, final Collection coll) {

    if (coll instanceof Set) {

        return (coll.contains(obj) ? 1 : 0);

    }

    if (coll instanceof Bag) {

        return ((Bag) coll).getCount(obj);

    }

    int count = 0;

    if (obj == null) {

        for (Iterator it = coll.iterator();it.hasNext();) {

            if (it.next() == null) {

                count++;

            }

        }

    } else {
    
        for (Iterator it = coll.iterator();it.hasNext();) {

            if (obj.equals(it.next())) {

                count++;

            }

        }

    }

    return count;

}

找到集合中第一个满足条件的元素:

代码语言:javascript
复制
public static Object find(Collection collection, Predicate predicate) {

    if (collection != null && predicate != null) {

        for (Iterator iter = collection.iterator(); iter.hasNext();) {

            Object item = iter.next();

            if (predicate.evaluate(item)) {

                return item;

            }

        }

    }

    return null;

}

对集合中每个元素执行指定的闭包:

代码语言:javascript
复制
public static void forAllDo(Collection collection, Closure closure) {

    if (collection != null && closure != null) {

        for (Iterator it = collection.iterator(); it.hasNext();) {

            closure.execute(it.next());

        }

    }

}

过滤掉某些不满足指定条件的元素:

代码语言:javascript
复制
public static void filter(Collection collection, Predicate predicate) {

    if (collection != null && predicate != null) {

        for (Iterator it = collection.iterator(); it.hasNext();) {

            if (predicate.evaluate(it.next()) == false) {

                it.remove();

            }

        }

    }

}

对集合中每个元素进行指定的操作,这个操作会影响传入的集合:

代码语言:javascript
复制
public static void transform(Collection collection, Transformer transformer) {

    if (collection != null && transformer != null) {

        if (collection instanceof List) {

            List list = (List) collection;

            for (ListIterator it = list.listIterator(); it.hasNext();) {

                it.set(transformer.transform(it.next()));

            }

        } else {

            Collection resultCollection = collect(collection, transformer);

            collection.clear();

            collection.addAll(resultCollection);

        }

    }

}

返回满足指定条件的元素个数:

代码语言:javascript
复制
public static int countMatches(Collection inputCollection, Predicate predicate) {

    int count = 0;

    if (inputCollection != null && predicate != null) {

        for (Iterator it = inputCollection.iterator(); it.hasNext();) {

            if (predicate.evaluate(it.next())) {

                count++;

            }

        }

    }

    return count;

}

判断集合中是否存在满足某个指定条件的元素:

代码语言:javascript
复制
public static boolean exists(Collection collection, Predicate predicate) {

    if (collection != null && predicate != null) {

        for (Iterator it = collection.iterator(); it.hasNext();) {

            if (predicate.evaluate(it.next())) {

                return true;

            }

        }

    }

    return false;

}

找出集合中满足指定条件的所有元素:

代码语言:javascript
复制
public static Collection select(Collection inputCollection, Predicate predicate) {

    ArrayList answer = new ArrayList(inputCollection.size());

    select(inputCollection, predicate, answer);

    return answer;

}

找出集合中所有不满足条件的元素:

代码语言:javascript
复制
public static Collection selectRejected(Collection inputCollection, Predicate predicate) {

    ArrayList answer = new ArrayList(inputCollection.size());

    selectRejected(inputCollection, predicate, answer);

    return answer;

}

添加一个可能为空的元素:

代码语言:javascript
复制
public static boolean addIgnoreNull(Collection collection, Object object) {

    return (object == null ? false : collection.add(object));

}

向集合中添加一个迭代器、枚举、数组:

代码语言:javascript
复制
public static void addAll(Collection collection, Iterator iterator) {

    while (iterator.hasNext()) {

        collection.add(iterator.next());

    }

}

其余还有获取第几个元素的index()方法、获取集合大小的size()方法、获取集合为空、不为空、反转数组reverseArray()、集合是否已经满的isFull()方法、保留全部retainAll()、移除全部removeAll()、获取同步集合、不可修改集合等方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年08月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档