集合简介

介绍

1、集合类存放的都是对象的引用,而非对象本身,出于表达上的便利,我们称集合中的对象就是指集合中对象的引用。先介绍一下顶层接口,接下来详细介绍List,Set,Map。下面将贴出源码,可以参考下,理解起来比较有逻辑性,不想看源码,可以直接跳到下面对各个集合的介绍

集合框架

Iterable

集合和Map的顶层父类,里面有Iterator<T> iterator(); 方法,所以所有继承Iterable接口都可以通过iterator() 来进行遍历数据

源代码:

/**
 * Implementing this interface allows an object to be the target of
 * the "foreach" statement.
 * @param <T> the type of elements returned by the iterator
 * @since 1.5
 */
public interface Iterable<T> {

    /**
     * Returns an iterator over a set of elements of type T.
     *
     * @return an Iterator.
     */
    Iterator<T> iterator();
}

迭代访问示例:

Iterator it = collection.iterator(); // 获得一个迭代器
while(it.hasNext()) {
   Object obj = it.next(); // 得到下一个元素
}

Collection

集合接口,其中有两个比较常用的集合接口List和Set(还有一个JDK1.5后出来的Queue)

源代码:

public interface Collection<E> extends Iterable<E> {
    int size(); //集合长度
    boolean isEmpty(); //判断是否为空
    boolean contains(Object o);  //是否包含制定对象
    Iterator<E> iterator(); // 返回遍历对象
    Object[] toArray();   // 转换成数组对象
    <T> T[] toArray(T[] a);   //转换成指定的对象
    boolean add(E e);   //添加特定对象
    boolean remove(Object o);  // 删除指定对象
    boolean containsAll(Collection<?> c);  //该集合是否包含所有指定集合内容
    boolean addAll(Collection<? extends E> c); //添加集合所有元素到该集合中
    boolean removeAll(Collection<?> c);  //删除C集合中的所有元素
    //保留集合中含有集合C中的元素(也就是取这两个集合的交集,如果有交集,返回true,并且把交集以外的元素删除)
    boolean retainAll(Collection<?> c);  
    void clear();  //清空集合
    boolean equals(Object o);  //判断集合相等
    int hashCode();  //哈希值
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值