zoukankan      html  css  js  c++  java
  • Collection和Collections

    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);
    • 混排
    • 基于随机源的输入随机排序该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接口

     

  • 相关阅读:
    获取农历日期
    图片上传代码(C#)
    ASP.net使用技术总结(1)GridView控件的单击处理
    JavaScript使用小技巧:IE8的关闭处理
    FrameSet左右收缩编码
    哈哈,开心!今天注册开通了 弟弟Kernel 的网志
    设计模式简介
    Delphi字符串、PChar与字符数组之间的转换
    C++中数组参数详解
    1、简单工厂模式
  • 原文地址:https://www.cnblogs.com/Chsy/p/11779616.html
Copyright © 2011-2022 走看看