Collections in Java (SET,LIST,MAP)

Source: Internet
Author: User
Tags comparable shuffle


Collections Class Summary ***************************

There are three main types of Java collections:
Set (set)
List (lists)
Map (map)


Collection interface
Collection is the most basic collection interface, declaring a common method that applies to a Java collection ( including set and list only).
Both Set and list inherit the Conllection,map.
Class collections is a wrapper class. It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated, just like a tool class that serves the Java collection framework. java.util.Collections


Common methods for collection interfaces (these methods can be inherited by set and list):
(1) sort () sorting method
function definition: public static <t extends Comparable<? Super t>> void Sort (list<t> List) is based on the element's
Sorts the specified list in ascending order by nature.
Parameters: The list to sort.
function definition: public static <T> void sort (list<t> list,comparator<? super T> C) to sort the specified list according to the order produced by the specified comparer. All elements within this list must be compared to each other using the specified comparer.
Parameters: list-The list to sort, and a comparer to determine the order of the list.
(2) BinarySearch () Two-point search method
function definition: public static <T> int BinarySearch (list<? extends Comparable<? Super t>> List,t Key)
Use the binary search method to search for the specified list to get the specified object, compare the list elements in ascending order before making this method call, otherwise the result is indeterminate, this method performs O (n) Link traversal and O (log n) secondary element comparison.
Parameters: list-The list to search, key-the key to search.
function definition: public static <T> int BinarySearch (list<? extends t> List, T key, comparator<? super T> C) According to the specified comparison Sort the list in ascending order.
Parameters: list-The list to search, the key-to search for, and the C-sorted list of comparators.
(3) reverse () Inversion method
function definition: public static void reverse (list<?> list), which reverses the order of the elements in the specified list, which runs in linear time.
Parameters: List of list-elements to be reversed
(4) Shuffle () reorganization method
function definition: public static void Shuffle (List<?> list), the specified list is displaced with the default random source, and all permutations are roughly equal in probability.
Parameter: list-the list to be reorganized
function definition: public static void Shuffle (List<?> list,random Rnd), which is used to displace the specified list using the specified random source.
Parameters: list-The list to be reorganized, rnd-the random source used to group the list.
(5) Swap () Swap method
function definition: public static void Swap (list<?> list,int i,int J), exchanging elements at the specified position in the specified list.
Parameters: list-The list of element exchanges, I-the index of an element to be exchanged, and the index of another element that J wants to exchange.
(6) Fill () Replacement method
function definition: public static <T> void Fill (list<? super t> List,t obj), replaces all elements in the specified list with the specified element, and runs linear time.
Parameters: list-A list populated with the specified elements, obj-the elements used to populate the specified list.
(7) Copy () Copying method
function definition: public static <T> void copy (LIST&LT;? super t> dest,list<? extends t> src), copying all elements from one list to another. After you do this, the index of each copied element in the target list is equal to the index of that element in the source list, and the target list must be at least equal to the source list.
Parameters: dest-target list, src-source list (the copied list).
(8) min () Min value method
function definition: public static <t extends Object & comparable<? Super t>> T min (collection<? extends T> Coll), which returns the smallest element of a given Collection according to the natural order of the elements, All elements in the collection must implement the comparable interface, in addition, all elements in the collection must be mutually comparable.
Parameter: coll-will determine the collection of its smallest element.
function definition: public static <T> T min (collection<? extends T> coll,comparator<? Super T> Comp), based on the order of the specified comparator, Returns the smallest element of a given collection.
Parameter: coll-determines the comparer whose smallest element is collection,comp-used to determine the smallest element.
(9) Max () Max value method
function definition: public static <t extends Object & comparable<? Super t>> T Max (collection<? extends T> Coll), which returns the largest element of a given Collection based on the natural order of the elements.
Parameter: coll-will determine the collection of its largest element.
function definition: public static <T> T Max (collection<?extends t> coll,comparator<? Super t> Comp), depending on the order in which the comparator is produced, Returns the largest element of a given collection.
Parameter: coll-determines the comparer whose largest element is collection,comp-used to determine the largest element
(ten) rotate () rotation method (move element method)
function definition: public static void rotate (list<?> list,int distance), rotating the elements in the specified list according to the specified distance.
Parameters: list-The list to be rotated, the distance that the distance-list rotates, can make 0, negative number or greater than list.size ().
(one) ReplaceAll () Replace all functions
function definition: public static <T> Boolean replaceall (list<t> list,t oldval,t newval), replacing all occurrences of a specified value with another value.
Parameter: list-the list in which the substitution is made; oldval-the original value to be replaced; newval-replaces the new value of Oldvald.
(AddAll) (collection<? super t> C, T ... elements)
Adds all the specified elements to the specified collection.


function method of Set *************

1.Set Function Method:
The set has exactly the same interface as the collection, so there is no additional functionality, unlike the previous two different lists. In fact set is collection, but behaves differently. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently.) Set does not save duplicate elements (more responsible for how the elements are judged)
Set: Each element that is stored in the set must be unique because set does not save duplicate elements. The element that joins the set must define the Equals () method to ensure the uniqueness of the object. The set has exactly the same interface as the collection. The set interface does not guarantee the order in which elements are maintained.
HashSet: Set for quick find design. The object that is deposited into the hashset must define HASHCODE ().
TreeSet: The set of the save order, the underlying tree structure. Use it to extract ordered sequences from the set.
Linkedhashset: has hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions). The result is displayed in the order in which the elements are inserted when iterating through the set using Iterators.


function method of List ************

2.List Function Method:
There are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not for the fast random access design, but has a more general method.
List: Order is the most important feature of the list: it ensures that the elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (this is recommended for linkedlist use only. A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and remove elements from the middle of a list.
ArrayList: A list implemented by an array. Allows fast random access to elements, but inserts and removes elements in the middle of the list is slow.
Listiterator should only be used to traverse the ArrayList backward, not to insert and remove elements. Because that's much more expensive than linkedlist.
LinkedList: Sequential access is optimized, and the cost of inserting and deleting to the list is small. Random access is relatively slow. (use ArrayList instead.) ) also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), which are not defined in any interface or base class Enables LinkedList to be used as stacks, queues, and bidirectional queues.
The list interface inherits the collection interface, and the ArrayList class and the LinkedList class implement the list interface.
differences between the ArrayList class and the LinkedList class:
Arraylist:get,set fast, Add,remove slow, when the table header insertion Delete the slowest, you have to move the entire array
Linkedlist:add,remove fast, Get,set slow, each get must start traversing O (n) from the table header

The list includes all the implementation classes of the list interface and the list interface. Because the list interface implements the collection interface, the list interface has all the common methods provided by the collection interface, and since list is a list type, the list interface also provides some common methods that are appropriate for itself:


The difference between the ①add (int index, Object obj) method and the set (int index, object obj) method
When using the list collection, it is important to distinguish between the Add (int index, Object obj) method and the set (int index, Object obj) method, which adds an object to the specified index location, which is the object that modifies the specified index position.
② object that obtains the specified index position using the Get (int index) method
The difference between the ③indexof (object obj) method and the LastIndexOf (object obj) method
When using the list collection, it is important to distinguish between the indexof (object obj) method and the LastIndexOf (object obj) method, which is the smallest index position to get the specified object, which is the largest index position for the specified object. The precondition is that the specified object has duplicate objects in the list collection, otherwise the index position obtained by both methods is the same if there is only one specified object in the list collection.
④sublist (int fromIndex, int toindex) method
When using the sublist (int fromIndex, int toindex) method to intercept some objects in an existing list collection to generate a new list collection, it is important to note that the newly generated collection contains the object that the starting index position represents, but does not contain the object that the terminating index position represents.


Collections in Java (SET,LIST,MAP)

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.