zoukankan      html  css  js  c++  java
  • java 的collection

    参考:http://skyuck.iteye.com/blog/526358

    https://www.tutorialspoint.com/java/java_collections.htm

    Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects. Although these classes were quite useful, they lacked a central, unifying theme. Thus, the way that you used Vector was different from the way that you used Properties.

    The collections framework was designed to meet several goals, such as −

    • The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) were to be highly efficient.

    • The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.

    • The framework had to extend and/or adapt a collection easily.

    Towards this end, the entire collections framework is designed around a set of standard interfaces. Several standard implementations such as LinkedList, HashSet, and TreeSet, of these interfaces are provided that you may use as-is and you may also implement your own collection, if you choose.

    A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following −

    • Interfaces − These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.

    • Implementations, i.e., Classes − These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

    • Algorithms − These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.

    In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not collections in the proper use of the term, but they are fully integrated with collections.

    -------------------------------------------------------------------------------------

    在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便。这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结构中熟知的。例如Map,Set,List等。并且Java用面向对象的设计对这些数据结构和算法进行了封装,这就极大的减化了程序员编程时的负担。程序员也可以以这个集合框架为基础,定义更高级别的数据抽象,比如栈、队列和线程安全的集合等,从而满足自己的需要。 

    Java2的集合框架,抽其核心,主要有三种:List、Set和Map。如下图所示: 

    需要注意的是,这里的 Collection、List、Set和Map都是接口(Interface),不是具体的类实现。 List lst = new ArrayList(); 这是我们平常经常使用的创建一个新的List的语句,在这里, List是接口,ArrayList才是具体的类。 

    常用集合类的继承结构如下: 
    Collection<--List<--Vector 
    Collection<--List<--ArrayList 
    Collection<--List<--LinkedList 
    Collection<--Set<--HashSet 
    Collection<--Set<--HashSet<--LinkedHashSet 
    Collection<--Set<--SortedSet<--TreeSet 
    Map<--SortedMap<--TreeMap 
    Map<--HashMap 

    -----------------------------------------------SB分割线------------------------------------------ 

    List: 
    List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下 >标)来访问List中的元素,这类似于Java的数组。 

    Vector: 
    基于数组(Array)的List,其实就是封装了数组所不具备的一些功能方便我们使用,所以它难易避免数组的限制,同时性能也不可能超越数组。所以,在可能的情况下,我们要多运用数组。另外很重要的一点就是Vector是线程同步的(sychronized)的,这也是Vector和ArrayList 的一个的重要区别。 

    ArrayList: 
    同Vector一样是一个基于数组上的链表,但是不同的是ArrayList不是同步的。所以在性能上要比Vector好一些,但是当运行到多线程环境中时,可需要自己在管理线程的同步问题。 

    LinkedList: 
    LinkedList不同于前面两种List,它不是基于数组的,所以不受数组性能的限制。 
    它每一个节点(Node)都包含两方面的内容: 
    1.节点本身的数据(data); 
    2.下一个节点的信息(nextNode)。 
    所以当对LinkedList做添加,删除动作的时候就不用像基于数组的ArrayList一样,必须进行大量的数据移动。只要更改nextNode的相关信息就可以实现了,这是LinkedList的优势。 

    List总结: 

    • 所有的List中只能容纳单个不同类型的对象组成的表,而不是Key-Value键值对。例如:[ tom,1,c ]
    • 所有的List中可以有相同的元素,例如Vector中可以有 [ tom,koo,too,koo ]
    • 所有的List中可以有null元素,例如[ tom,null,1 ]
      • 基于Array的List(Vector,ArrayList)适合查询,而LinkedList 适合添加,删除操作
  • 相关阅读:
    Mysql大文本类型
    js-原生Js汉语拼音首字母匹配城市名
    js-原生Js汉语拼音首字母匹配城市名
    Java学习从基础,中级,高级框架,所有模块整理
    python数组和字符串互相转换
    Spring,SpringMVC,MyBatis,SSM配置文件比较
    Spring,SpringMVC,MyBatis,SSM配置文件比较
    Spring,SpringMVC,MyBatis,SSM配置文件比较
    GPUImage学习总结
    图像处理技术汇总
  • 原文地址:https://www.cnblogs.com/oxspirt/p/7364717.html
Copyright © 2011-2022 走看看