A summary of collection and generic collation in JAVA __java

Source: Internet
Author: User
Tags comparable set set
Summary of collection and generics collation in Java There are three main types of collections used in Java: List, set, Map;

Implementation of the interface Collection are: List, Set;

List,
The list interface implements the following three types of implementations: ArrayList : Dynamic array LinkedList : An efficient set of vectorsdesigned for frequent insertion or deletion of intermediate elements: dynamic arrays, synchronized, Thread-Safe

More commonly used is the ArrayList array, but the ArrayList will appear duplicate elements, and there is no way to sort, need to rely on the Collections class sort () method;
The parameters of Collections.sort () need to meet certain conditions;

public static <t extends Comparable<? Super t>> void Sort (list<t> List);
public static <T> void sort (list<t> List, comparator< super t> C)

So for the elements in the set, you must implement the CompareTo method in the comparable interface, or define a class that implements comparator;
As can be seen from the definition, as long as the implementation of the list interface can be sorted in this way. Set: There are no duplicate elements in the collection
There are several implementation classes for set interfaces:
hashset: linkedhashset: TreeSet: orderly;
For the set set, the mechanism of checking repetition is mainly used in Hashcode and Equels methods.
You can override the two methods for the elements in the collection to achieve the effect of validation duplicates.
TreeSet will sort the elements, we know that when we encounter the sort, the elements in the collection need to implement the comparable interface CompareTo () method, or create comparator with a constructor with a TreeSet parameter TreeSet.

Map TreeMap HashMap linkedhashmap Hashtable
The map is primarily accessed in the form of key-value pairs.

Generic type

Public <t extends animal> void takething (arraylist<t> list) public
void Takething (ArrayList (? extends Animal> list)
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.