A(1)java集合类总结(1)Collection

前言

前一篇博客,大略的总结了一下各种常见集合类的介绍和基本使用,从这篇博客开始参考API文档,和源码分析,详细的介绍每个集合类的使用,力求在源码的角度来分析,加深理解。

看一下java集合类的继承图

这是我在百度上找的一张图片,详细的介绍了各种集合类的继承关系。下面开始第一个集合类的介绍Collection:

1 Collection的介绍:

1.1 Collection是什么?

  1. 集合的由来:对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定,就需要使用集合来进行存储。
  2. 集合的特点:(1)集合中存储的元素是对象(2)集合的长度是可变的;(3)集合是不可以存储基本数据类型的
  3. 集合是用于存储对象的容器。而每种容器内部都有其独特的数据结构,正因为不同的容器内部数据结构不同,使其各自有自己独特的使用场景。虽然每个容器有其独特的结构但是类似的容器还是存在共性的(至少对容器内部对象的操作方法上是存在共性的),所以这些共性方法能被不断抽取,最终形成了集合框架体系。

与数组的区别:

  数组 集合
长度 固定 可变
存储元素 基本类型/引用类型 引用类型
元素类型的一致性 必须一致 可以不一致


1.2继承关系和源码分析: 

从上图可以看到Collection位于集合层次结构中的根节点。他继承了Iterator的接口。在java1.8的版本中包含了18个方法:

public interface Collection<E> extends Iterable<E> {
    
    int size();
   
    boolean isEmpty();
   
    boolean contains(Object o);
   
    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);
 
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }
    
    boolean retainAll(Collection<?> c);
 
    void clear();

    boolean equals(Object o);
   
    int hashCode();

    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }
 
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
}

1.3 方法介绍

  • add(E e):返回值是boolean,添加一个元素
  • addAll( Collection<? extend E> c ):返回值是boolean类型,将集合C中的所有元素添加到此集合。
  • clear():返回值是void,删除此集合中的所有元素
  • contains( Object o):如果包含元素O则返回为true
  • containsAll( Collection<?> c):如果包含集合C返回为true
  • equals( Object o):将指定的对象与此集合进行比较已获得相等性
  • hashCode():返回类型是int,返回此集合的哈希码值
  • isEmpty():如果集合为空则返回true
  • itreator():返回此集合元素的迭代器。
  • remove(Object o):返回类型是boolean,删除指定的元素
  • removeIf(Predicte<? super E> filter):删除满足条件的所有元素
  • removeAll(Collection<?> c):返回类型是boolean,删除包含集合c的所有元素。
  • size():返回类型是int,返回此集合的元素数量
  • toArray():返回类型是Object[] ,将此集合转化为数组
  • stream():返回类型是Stream<E>,返回以此集合作为源的顺序流
  • spliterator():创建一个Spliterator在这个集合中。

注意:Spliterator(splitable iterator可分割迭代器)接口是Java为了并行遍历数据源中的元素而设计的迭代器,这个可以类比最早Java提供的顺序遍历迭代器Iterator,但一个是顺序遍历,一个是并行遍历。他是从java1.8的版本中开始加进去的。

2 Collection的遍历:

上面给出了Collection的源码。也给出了API文档中描述的方法。接下来就是常见的一些遍历方式。

2.1 for-each遍历

public class Test5 {

	public static void main(String[] args) {
		Collection<String>  collection=new ArrayList<String>();
		collection.add("张三");
		collection.add("李四");
		collection.add("王五");
		
		//第一种方式:使用for-each
		for(String name:collection) {
			System.out.println(name);
		}
	}

}

2.2 Iterator 迭代器

public class Test5 {

	public static void main(String[] args) {
		Collection<String>  collection=new ArrayList<String>();
		collection.add("张三");
		collection.add("李四");
		collection.add("王五");
		//第二种方式:使用Iterator迭代器
		Iterator<String> it=collection.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}

}

 

猜你喜欢

转载自blog.csdn.net/SDDDLLL/article/details/81407013