apache CollectionUtils源码阅读

1.getCardinalityMap(): 获取统计出现次数的map

/**
 * self note:
 * 通过集合拿到 <key,value>为<集合元素,元素出现次数>的Map
 * 传入的集合<B>不能为空</B>
 */
public static Map getCardinalityMap(final Collection coll) {
   
    Map count = new HashMap();
    //循环遍历,如果Map里面没有该元素,则塞值,如果有,出现次数加一
    for (Iterator it = coll.iterator(); it.hasNext(); ) {
   
        Object obj = it.next();
        Integer c = (Integer) (count.get(obj));
        if (c == null) {
   
            count.put(obj, INTEGER_ONE);
        } else {
   
            count.put(obj, new Integer(c.intValue() + 1));
        }
    }
    return count;

2.getFreq(): 通过key拿到value(私有方法)

/**
 * 通过传入的元素,在Map里面拿到该元素出现的次数.如果没有该元素则返回0
 * freqMap是以Object为key,出现次数为value的Map
 */
private static final int getFreq(final Object obj, final Map freqMap) {
   
    //拿到出现次数。null强转之后还是null--,map的get可以看一下,大致是通过hash值去拿到对应的值
    Integer count = (Integer) freqMap.get(obj);
    if (count != null) {
   
        return count.intValue();
    }
    return 0;
}

3.union(): 合集

/**
     * self note:
     * 求合集
     * <p>
     * <b>直接返回List,拿到Collection对象之后强转List,没有问题</b>
     */
    public static Collection union(final Collection a, final Collection b) {
   
        ArrayList list = new ArrayList(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了-凡

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值