zoukankan      html  css  js  c++  java
  • Java Collection

    A collection — sometimes called a container — is simply an object that groups
    multiple elements into a single unit.

    Store Retrieve Manipulate Communicate

    Collection Framework

    A collection framework is a unified architecture for representing and manipulating
    collections. It contains:

    • Interface

    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
    , interface generally form a hierarchy.

    • Implementations

    Implementations of collections interface. 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 essence, algorithms are reusable functionality.

    Best-known examples of collections framework are the C++ STL and Smalltalk's collection hierarchy. But they are quite
    complex, which gave them a reputation for having a steep learning curve.

    Benefits of the Java Collection Framework

    The Java Collection Framework provides the following benefits:

    • Reduce programming effort
    • Increase program speed and quality
    • Allows interoperability(互用性) among unrelated APIs
    • Reduces effort to learn and use new APIs.
    • Reduces effort to design new APIs
    • Foster software reuse

    Interface

    The core collection interfaces encapsulate different types of collections, which are shown in the figure below.
    The core collection interfaces form a hierarchy.

    graph TD A[Collections] --> Set A --> Stack A --> List A --> Queue A --> Deque Set --> SortedSet Map --> SortedMap
    • Collection

      The root of the collection hierarchy. A collection represents a group of objects known as
      its elements. The Collection interface is the least common denominator that all collections
      implement and is used to pass collections around and to manipulate them when
      maximum generality is desired.

    • Set

      A collection that cannot contain duplicate elements.

      • SortedSet

      A Set that maintains its elements in ascending order.

    • List

      An ordered collection(aslo known as sequence)

    • Queue

      A collection used to hold multiple elements prior to processing.
      Queues typically, but do not necessarily, order elements in a FIFO.

    • Deque

      A collection used to hold multiple elements prior to processing. Deques can be
      used both as FIFO and LIFO.

    • Map

      An object that maps keys to values.

      • SortedMap

      A Map that maintains its mappings in ascending key order.

    类名称 作用
    HashSet 为优化査询速度而设计的 Set。它是基于 HashMap 实现的,HashSet 底层使用 HashMap 来保存所有元素,实现比较简单
    TreeSet 实现了 Set 接口,是一个有序的 Set,这样就能从 Set 里面提取一个有序序列
    ArrayList 一个用数组实现的 List,能进行快速的随机访问,效率高而且实现了可变大小的数组
    ArrayDueue 是一个基于数组实现的双端队列,按“先进先出”的方式操作集合元素
    LinkedList 对顺序访问进行了优化,但随机访问的速度相对较慢。此外它还有 addFirst()、addLast()、getFirst()、getLast()、removeFirst() 和 removeLast() 等方法,能把它当成栈(Stack)或队列(Queue)来用
    HsahMap 按哈希算法来存取键对象
    TreeMap 可以对键对象进行排序

    Collection functions:

    方法名称 说明
    boolean add(E e) 向集合中添加一个元素,如果集合对象被添加操作改变了,则返回 true。E 是元素的数据类型
    boolean addAll(Collection c) 向集合中添加集合 c 中的所有元素,如果集合对象被添加操作改变了,则返回 true。
    void clear() 清除集合中的所有元素,将集合长度变为 0。
    boolean contains(Object o) 判断集合中是否存在指定元素
    boolean containsAll(Collection c) 判断集合中是否包含集合 c 中的所有元素
    boolean isEmpty() 判断集合是否为空
    Iteratoriterator() 返回一个 Iterator 对象,用于遍历集合中的元素
    boolean remove(Object o) 从集合中删除一个指定元素,当集合中包含了一个或多个元素 o 时,该方法只删除第一个符合条件的元素,该方法将返回 true。
    boolean removeAll(Collection c) 从集合中删除所有在集合 c 中出现的元素(相当于把调用该方法的集合减去集合 c)。如果该操作改变了调用该方法的集合,则该方法返回 true。
    boolean retainAll(Collection c) 从集合中删除集合 c 里不包含的元素(相当于把调用该方法的集合变成该集合和集合 c 的交集),如果该操作改变了调用该方法的集合,则该方法返回 true。
    int size() 返回集合中元素的个数

    In JDK 7, you can use diamond operator like this
    List<String> list = new ArrayList<>();

    In JDK8 and later, the Collection interface also exposes methods Stream stream()
    and Stream parallelStream(), for obtaining sequential or parallel streams from the
    underlying collection.

    Travering Collection

    There are three ways to traverse colletctions: aggregate operation, for-each, Iterators.

    • Aggregate Operation

    In JDK 8 and later, the prefered method of iteratoring over a colection is to obtain a stream
    and perform aggregate operations on it.

    myShapesCollection.stream()
    .filter(e -> e.getColor() == Color.RED)
    .forEach(e -> System.out.println(e.getName()))
    

    or use parallelStream like this:

    myShapesCollection.parallelStream()
    .filter(e -> e.getColor() == Color.RED)
    .forEach(e -> System.out.println(e.getName()));
    

    Convert the elements of a Collection to String objects, then join them.

    String joined = elements.stream()
        .map(Object::toString)
        .collect(Collectors.joining(", "));
    

    sum int with:

    int total = employees.stream()
    .collect(Collectors.summingInt(Employee::getSalary)));
    
    • For-each Construct
    for (Object o : collection)
    	System.out.println(o);
    
    • Iterators

    An Iterator is an object that enables you to traverse though a collection and to
    remove elements from the collection selectively, if desired.

    Use Iterators instead of for-each construct when you need to:

    • remove the current element.
    • iterate over multiple collection in parallel.

    Collection Interface Bulk Operation

    Operation Usage
    containAll return true if the target Collection contains all of the elements in the specified Collection.
    addAll adds all of elements in the specified Collection to the target Collection.
    removeAll remove from the target Collection all of its elements that are also contained in the specified Collection
    retainAll removes from the target Collection all its elements that are not also contained in the specified Collection
    clear remove all elements from the Collection

    Collection Interface Array Operation

    The toArray method are provided as a bridge between collections and older APIs that expect arrays on input.

    Set

    The set interface contains only methods inherited from Collections and adds the
    restriction that duplicated elements are prohibited.

    The Java platform contains three general-purpose Set implementations.

    • HashSet

    Store elements in a hash table, is the best-performing implementation, and disordered.

    • TreeSet

    Stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet

    • LinkedHashSet

      Implemented as a hash table with a linked list running through it, orders its
      elements based on the order in which they were inserted into the set (insertion-order).
      LinkedHashSet spares its clients from the unspecified, generally chaotic ordering provided by HashSet at a cost that is only slightly higher.

    You can crate another Collection containing the same elements but with all duplicated
    eliminated.

    Collection<Type> noDups =  new HashSet<Type>(c);
    

    in JDK 8 or later, you could easily collect into a Set using aggregate operation.

    c.stream()
    .collect(Collectors.toSet()); // no duplicates
    

    If you want keep order of the original collection while removing duplicate elements.

    Collection<Type> noDups = new LinkedHashSet<Type>(c);
    

    List

    • Positional access

    Manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.

    • Search

      Searches for a specified object in the list and returns is numerical position.
      Search method includes indexOf and lastIndexOf.

    • Iteration

      Besides Next(), remove() and hasNext(), List add hasPrevious() and previous() specifically.
      You can traverse in reverse by hasPrevious and previous.

    for (ListIterator<Type> it = list.listIterator(list.size()); it.hasPrevious(); ) {
        Type t = it.previous();
        ...
    }
    
    • Range-View

    The sublist method performs arbitrary range operations on the list.

    ref

  • 相关阅读:
    [LeetCode] 500. Keyboard Row 键盘行
    [LeetCode] 502. IPO 上市
    [LeetCode] 495. Teemo Attacking 提莫攻击
    [LeetCode] 655. Print Binary Tree 打印二叉树
    [LeetCode] 654. Maximum Binary Tree 最大二叉树
    [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值
    Dubbo 在maven项目中的应用
    前后端分离springmvc和RESTful理解
    一个相对通用的JSON响应结构,其中包含两部分:元数据与返回值
    MAC OS查看端口占用情况及杀死进程
  • 原文地址:https://www.cnblogs.com/sonnet/p/15187508.html
Copyright © 2011-2022 走看看