Summary of common Java collections

Source: Internet
Author: User

List

public interface List<E>extends Collection<E>
An ordered collection (also called a sequence) that allows repeating elements.
Common Implementation classes:

  • ArrayList:
    The underlying use of the array structure, features: Fast query, adding and deleting operations are slow, and the thread is not synchronized.
    To ensure synchronization, you can use: List list = Collections.synchronizedList(new ArrayList()); to wrap, the default capacity is 10.
    Common methods:,,,,, add addAll remove indexOf subList contains , isEmpty .....

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    List<string>List =new arraylist<string> (); 
    list.add ( list.add ( list.add ( list.add ( list.add ( for (int i = 0; I < list.size (); i++) {
    list.remove (i);
    system.out.println (list);

    Easy-to-ignore method: trimToSize() //fixed capacity size, how much to give, how much, ensureCapacity(int minCapacity) //Preset size
    Custom ArrayList:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21st
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    /**
    * Simple Analog ArrayList
    * @author Admol
    */
    public class myArrayList {
    Privateobject[] data;
    PrivateIntSize

    Public myArrayList () {
    This10);
    }

    Public myArrayList (int i) {
    data =NewObject[i];
    }

    /**
    * Returns the number of elements owned
    * @return
    */
    PublicIntSize () {
    ReturnSize
    }

    /**
    * Gets the element at the specified position
    * @param i
    * @return
    */
    PublicObjectGetint i) {
    return data[i];
    }

    /**
    * add Element
    * @param obj
    */
    PublicvoidAddObject obj) {
    Storage capacity is full and needs to be expanded
    if (data.length = =Size) {
    object[] Temp =Newobject[Size + (Size >>1)];
    Array copy: (the copied array, starting from the first few elements copied, copied to the target array, the beginning of the pasted position, the number of elements to be copied)
    System.arraycopy (data,0, temp,0,size);
    data = temp;
    }
    Storage space is not full
    data[size++] = obj;
    }

    /**
    * Delete the element at the specified location
    * @param i
    */
    Publicvoid Remove (int i) {
    Example: A total of 10 elements, the element to be deleted is labeled 3, is actually starting from subscript 4 to mark 3 copy paste (10-3-1) Element
    System.arraycopy (data, i +1, data, I,Size-i-1);
    size--;
    /**
    * Delete the specified element
    * @param obj
    */
    Public void remove (object obj) {
    for (int i = 0; i < size; i++) {
    if (data[i].equals (obj)) {
    remove (i);
    return;
    }
    /span>
  • LinkedList
    Use a doubly linked list for storage, allowing all elements (including null). Non-thread safe, to ensure synchronization, can be used List list = Collections.synchronizedList(new LinkedList()); for packaging, can be used as a stack and queue to use.

  • Vector
    is also an ordered set that allows repeating elements and thread safety.

Map
    • HashMap
      The implementation of a hash table-based MAP interface that stores key-value pairs, which are unique, allow null values and NULL keys, are non-synchronous (except for non-synchronous and allow null, the HashMap class is roughly the same as Hashtable), and synchronization can be usedMap m = Collections.synchronizedMap(new HashMap(...));
      Common methods:
      1. Add to. Put (Key,value): When the stored key is the same, the new value replaces the old value and returns the old value. Returns NULL if the key is not duplicated. void Putall (MAP);
      2. Delete. void Clear (): Empty value Remove (key): Deletes the specified key.
      3. Judge. Boolean IsEmpty (): Boolean ContainsKey (Key): Contains key Boolean containsvalue (value): Contains value
      4. Remove. int size (): Returns the length of value get (key): Gets the corresponding value by specifying the key. If NULL is returned, you can tell that the key does not exist. Of course there is a special case, that is, in the HashMap collection, the null key can be stored in the null value.
        Collection values (): Gets all the values in the Map collection.
    • HashTable
      The bottom layer is a hash table data structure that cannot be stored in null, NULL, or thread synchronization.
      An instance of Hashtable has two parameters that affect its performance: 初始容量 and 加载因子 (default. 75)
Set
    • HashSet
      The underlying data structure is a hash table (actually a HASHMAP instance) that allows null elements, unique and unordered, non-thread safe. The element's uniqueness is ensured by the hashcode and equals of the element. If the hashcode value of the element is the same, the Equals is true, and equals is not called if the value of the element's hashcode is different.
Collections

Provides a wrapper to manipulate the collection to perform operations such as finding, sorting, replacing, threading, and converting non-synchronized collections to synchronous. Common methods:
Collections.max (list);//returns the element with the largest dictionary order in the list.
int index = Collections.binarysearch (list, "zz");//binary lookup, returns the corner label. Must be orderly.
Collections.fill ();//You can replace all the elements in the list collection with the specified elements.
Collections.repalceall (list, "to be replaced", "substituted value");//You can replace the specified element in the list collection with the specified element. Collections.reverse (); Reverse
Collections.reverseorder (parameter is comparator);//reverse-reverse sort. Reverse:
Collections.shuffle (list);//random displacement of the position of the elements in the list.
Synchronizedxxx (XXX); The method by which the asynchronous collection is converted to a synchronous collection:

Arrays

The tool class used to manipulate array objects is a static method.
Arrays.aslist: Can be converted from Array to List. Parameters that can be used as constructors for other collection types.
Arrays.binarysearch: Quick Find in a sorted or one segment.
ARRAYS.COPYOF: You can use this method if you want to enlarge the array capacity and don't want to change its contents.
Arrays.copyofrange: You can copy an entire array or part of it.
Advanced version of Arrays.deepequals, Arrays.deephashcode:arrays.equals/hashcode, supports operation of sub-arrays.
Arrays.equals: If you want to compare two arrays for equality, you should call this method instead of the Equals method in the Array object (the Equals () method is not overridden in the array object, so this method compares references without comparing the contents).
Arrays.fill: Fills the entire array or part of it with a given value.
Arrays.hashcode: Used to calculate its hash value based on the contents of the array (the hashcode () of the array object is not available).
Arrays.sort: Sorts the entire array or part of an array. You can also use this method to sort an array of objects with a given comparer.
Arrays.tostring: Prints the contents of the array.

Relationship Diagram

Summary of common Java collections

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.