Java集合

 

1.大boss接口Collection

  • boolean add(E e)
  • boolean addAll(Collection<? extends E> c)
  • void clear()
  • boolean contains(Object o)
  • boolean containsAll(Collection<?> c)
  • Iterator<E> iterator()
  • boolean remove(Object o)
  • boolean removeAll(Collection<?> c)
  • boolean retainAll(Collection<?> c) 仅保留此集合中包含在指定集合中的元素
  • int size()
  • Object[] toArray()

2.迭代器Iterator

这是一个接口,Collection继承了它,只有3个方法,hasNext()判断是否还有元素,next()获取当前的元素并移动到下一个位置,remove()从底层集合中删除此迭代器返回的最后一个元素,用来遍历的。实现如下

        ArrayList<Integer> list=new ArrayList<Integer>();
        list.add(10);
        list.add(33);
        Iterator it=list.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }

它是以内部类的方式实现的,是依赖集合存在的,不同的集合容器存取方法不同,所以在各自类中用迭代器遍历的方法也不同。

3.List有序(存储和取出顺序一致,数组就算是有序)、可重复。

4.Set:元素不可重复

查了不少博客,没啥人讲得详细,大概都是这几个方法,都是在子类中体现,理解了HashMapArrayListLinkedList的底层数据结构基本就理解了全部集合。

原文地址:https://www.cnblogs.com/shoulinniao/p/12702696.html