collection中常用方法二三事

  1. containsAll:此方法工作方式并不如人意,它是对比较集合逐个调用contains方法,源码参考如下

    1
    2
    3
    4
    5
    6
    7
    public boolean containsAll(Collection<?> c) {
    Iterator<?> e = c.iterator();
    while (e.hasNext())
    if (!contains(e.next()))
    return false;
    return true;
    }
  1. contains:源码参考

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Iterator<E> it = iterator();
    if (o==null) {
    while (it.hasNext())
    if (it.next()==null)
    return true;
    } else {
    while (it.hasNext())
    if (o.equals(it.next()))
    return true;
    }
    return false;
Donate comment here