Java Collection Framework

Source: Internet
Author: User

Java Collection Framework one. Collection interface

In the collection framework, the collection (Collection) interface is located at the top of the set interface and the list interface, and is the parent interface of the set interface and the list interface.

List interface

The list interface inherits from the collection interface, and he has the following features:
1. The elements in the list are sequential.

    1. Lists usually allow repeating elements.

    2. The list implementation class typically supports NULL elements.

    3. The elements in the List object container can be accessed through an index.

ArrayList

ArrayList is conceptually similar to an array, representing a set of indexed elements, the difference being that ArrayList does not have a predetermined size, and its length can be increased on demand.
The following is a summary of ArrayList methods:

        List list = new ArrayList();
  1. The Boolean Add (e E) Adds the specified element to the end of this list.

        list.add("hello");    list.add(1);   
  2. void Add (int index, E Element) inserts the specified element into the specified position in this list.

        
  3. void Clear () Removes all elements from this list.

        list.clear();    
  4. Boolean contains (Object o) returns true if the specified element is contained in this list.

        list.contains("hello");   
  5. E get (int index) returns the element at the specified position in this list.

        
  6. an int indexOf (Object o) returns the index of the specified element that first appeared in this list, or 1 if the list does not contain elements.

        
  7. Boolean IsEmpty () returns True if there are no elements in this list

        System.out.println(list.isEmpty());
  8. E Remove (int index) removes the element at the specified position in this list.

        list.remove(2);  
  9. The e set (int index, E Element) replaces the element at the specified position in this list with the specified element.

        list.set(1, "wode");   
LinkedList

LinkedList is a list of two-way linked lists, where each object in the list is placed in a separate space, and the index of the previous and next joins is saved in each space.

    LinkedList list = new LinkedList();
  1. The Boolean Add (e E) Adds the specified element to the end of this list.

  2. void Add (int index, E Element) inserts the specified element at the location specified in this list.

    list.add(2,"hah");
  3. void Clear () Removes all elements from this list.

    list.clear();   
  4. Boolean contains (Object o) returns True if this list contains the specified element.

    System.out.println(list.contains(0));
  5. E get (int index) returns the element at the specified position in this list.

    list.get(1);
  6. an int indexOf (Object o) returns the index of the specified element that first appeared in this list, or 1 if the element is not included in this list.

    list.indexOf("hello");
  7. E pop () pops an element at the stack represented by this list.

    list.pop();
  8. void push (E e) pushes an element into the stack represented by this list.

    list.push(3);
  9. E Remove (int index) removes the element at the specified position in this list.

    list.remove(2);
  10. e Set (int index, E Element) replaces the element at the specified position in this list with the specified element.

    list.set(1, 9);
Set interface

The set interface also inherits from the collecting interface, and also inherits all the methods of the collection interface, which has the following characteristics:

    1. A set type container cannot contain duplicate elements, and when an element is added to a container, the object that joins the object container of the set type must override the Equals () method to compare whether the contents of the element are duplicated.
    2. The elements may have order or there can be no order of Ken.
    3. Elements in a set cannot be accessed based on an index because the elements may not be in order.
HashSet class

The HashSet class is a set interface implementation based on the hashing algorithm, and he has the following features:

    1. When traversing hashset, there are no sequential elements in them.

    2. Duplicate elements are not allowed in HashSet. The repeating element here refers to the two objects that return true when the same hash code is used and compared with the Equals () method.

    3. Allows the inclusion of a null element.

      Set<String> set = new HashSet<String>();

Method Summary
1. Boolean Add (E) If the specified element is not already contained in this set, the specified element is added.

    set.add("zhangsan");
    1. void Clear () Removes all elements from this set.

      set.clear();
    2. Boolean contains (Object o) returns True if this set contains the specified element.

      set.contains("lisi");
    3. Boolean IsEmpty () returns True if this set contains no elements.

      set.isEmpty();
    4. Iterator Iterator () returns an iterator that iterates over the elements in this set.

      Iterator<String> it = set.iterator();       String s = null;while ( it.hasNext()) {                          s = it.next();     System.out.println(s);}
    5. Boolean remove (Object o) if the specified element exists in this set, it is removed.

      set.remove("lisi");  
TreeSet class

The TreeSet class not only implements the class set interface, but also implements the SortedSet interface, thus ensuring that the objects in the collection are sorted in a certain order. (Dictionary order)

    Set<String> set = new TreeSet<String>();
  1. Boolean Add (E) adds the specified element to this set if the element does not already exist in the set.

    set.add("zhangsan");
  2. E ceiling (e) returns the smallest element in this set that is greater than or equal to the given element, or null if no such element exists.

     set.ceiling("lisi");
  3. Comparator () returns a comparer that sorts the elements in this set, or null if this set uses the natural order of its elements.

    set.comparator();
  4. Boolean contains (Object o) returns True if this set contains the specified element.

    set.contains("lisi");
  5. E Floor (e) returns the largest element in this set that is less than or equal to the given element, or null if no such element exists.

     set.floor("lisi");
  6. Iterator Iterator () returns an iterator that iterates in ascending order on the element in this set.

    Iterator<String> it = set.iterator();       
  7. A Boolean remove (Object o) removes the specified element from the set (if the element exists in this set).

    set.remove("lisi");  
  8. SortedSet subset (e fromelement, E toelement) returns a partial view of this set, with elements from fromelement (including) to Toelement (not included).

    set.subSet("lisi", "lier");
Map interface

The map interface is another important interface in the Java Collection framework that differs from the collection interface, which corresponds to a collection of corresponding relationships from the value.

HashMap class

The HashMap class is a hash algorithm based map interface implementation. HashMap it to be maintained in a hash table for maintenance, the key is unique. However, HashMap does not guarantee that the keys are sorted in a particular sequence, especially if the order is permanent.

    Map<String, String> map = new TreeMap<String, String>();
    1. V Put (K key, V value) associates the specified value with the specified key in this mapping.

    2. V get (Object key) returns the value mapped by the specified key, or null if the map does not contain any mapping relationships for that key.

      System.out.println(map.get("cd"));
    3. Boolean ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key. Boolean Containsvalue (Object value) returns True if this mapping maps one or more keys to the specified value.

      map.containsKey("cd");map.containsValue("成都");
    4. Set KeySet () returns a set view of the keys contained in this map.

      System.out.println(map.map.keySet());
    5. V Remove (Object key) removes the mapping of the specified key from this mapping, if any.

      map.remove("cd");  
TreeMap class

In the TreeMap class, the key is stored in the same way as the TreeSet, which stores the key in the tree, with the order of the keys arranged in either natural order or custom order.

    TreeMap<String, String> map = new TreeMap<String, String>();
  1. K Ceilingkey (k key) returns the minimum key greater than or equal to the given key, or null if no such key exists.

    map.ceilingKey("cd");
  2. Boolean ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key.

  3. Boolean Containsvalue (Object value) returns True if this mapping maps one or more keys for the specified value.

    map.containsValue("成都");   
  4. K Floorkey (k key) returns the maximum key that is less than or equal to the given key, or null if no such key exists.

    map.floorKey("cd");  
  5. V get (Object key) returns the value mapped by the specified key, or null if the map does not contain any mapping relationships for that key

    map.get("cd");   
  6. Set KeySet () returns the set view of the key contained by this map.

    map.keySet();  
  7. V Put (K key, V value) associates the specified value with the specified key in this map.

    map.put("cd", "成都");map.put("bj", "北京");map.put("sh", "上海");
  8. V Remove (Object key) If there is a mapping relationship for the key in this TreeMap, delete it.

     map.remove("cd");
  9. Sortedmap<k,v> SubMap (k Fromkey, K tokey) returns a partial view of this map, with its key values ranging from Fromkey (including) to Tokey (not included).

    map.subMap("cd", "sh");

Java Collection Framework

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.