Collection和Collections

时间:2019-11-01 22:12:58   收藏:0   阅读:105

Collection是一个接口(Interface),Collections是一个类

注意:Collections并没有实现Collection接口

 

Collections

Collections不能被实例化,它是集合类(ArrayList,HashSet等)的一个工具类。这是因为Collections的构造函数被private修饰,不能通过new Collections()来实例化一个Collections的对象。

Collections里定义了很多集合操作的静态方法,如排序(sort),链表反转(reverse)等

Collections的使用:

Collections.sort(list);
或者
Collections.sort(list,new ListComparator);
Collections.reverse(list);
Collections.shuffle(list);
Collections.fill(list,0);

 

Collection

Collection是最基本的集合接口,ArrayList,LinkedList,HashSet,HashMap等都可以向上转型为该接口的引用;如ArrayList实现了List接口,List接口又继承了Collection接口,因此ArrayList可以向上转型为Collection接口。

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
public interface List<E> extends Collection<E> {}
public interface Collection<E> extends Iterable<E> {}

除了List, Collection接口还被以下接口继承

BeanContext, BeanContextServices, BlockingDeque<E>, BlockingQueue<E>, Deque<E>, EventSet, List<E>, NavigableSet<E>, 

Queue<E>, Set<E>, SortedSet<E>, TransferQueue<E>

同时,Collection接口继承了Iterable接口

 

 

 

原文:https://www.cnblogs.com/Chsy/p/11779616.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!