Summarize the Collections in java

Source: Internet
Author: User
Tags addall comparable

Java's Collection class consists of two parts: one is the class that implements the Interface Collection, and the other is the class that implements the interface Map. note that although the latter does not implement the Collection interface, they are all part of Collections (not Collections.
The following content is based on jdk1.5 and above. Generic is used.
The reason for listing them is to let everyone know their rules.
First, the Collection class is implemented.
Its methods can be divided into: 1. add, delete 2. query 3. Other
1. add and delete: boolean add (Object), boolean addAll (Collection <? Extends E>)
Boolean remove (Object), boolean removeAll (Collection <?> ), Boolean retainAll (Collection <?> ), Void clear ()
2. query. query operations cannot be performed directly, but can only be performed using Iterator. method iterator () is used to return an Iterator implemented as an internal class <T>. use hasNext () and next () of the returned Iterator to query the content in the set.
3. other size () with query size, including Object [] toArray () and <T> T [] toArray (T []) that convert the set to an array, equals () and hashCode () inherited from the Object class, And isEmpty (), boolean contains (Object o), boolean containsAll (Collection <?> C)
15 Methods in total

1. Let's take a look at its subinterface List <T>
The main feature of List <T> is that it can store repeated elements, compared with another sub-interface Set <T> of Collection, the elements are stored in order. based on these features, in addition to the methods inherited from Collection above, List <T> also adds the following methods to delete indexes:
Add (int index, Object), addAll (int index, Collection <? Extends E> c)
Remove (int index)
Because of the index, the query can be performed directly, instead of using Iterator <T>.
Get (int index), set (int index, E element)
IndexOf (Object) lastIndexOf (Object)
ListIterator <E> listIterator (), ListIterator <E> listIterator (int index)
ListIterator <E> compared with Iterator <E>, ListIterator has the function of traversing from the back to the front. Therefore, it has two main methods: previous () and hasPrevious (). In addition
NextIndex () and previusindex (); and set (E o), remove ();
List <E> subList (int fromIndex, int toIndex)
Let's take a look at the List <T> class.
1.1 ArrayList <T>
It adds the following methods based on List <T>:
Void ensuerCapacity (int minCapacity)
Void trimToSize ()
Object clone ()
Protected void removeRange (int from, int)
The ArrayList <T> feature (compared with other List <T> Implementation classes mentioned below) Stores elements in the order they are added.
1.2 sort list <T>
Like ArrayList <T>, elements are stored in the order of addition. it is relatively slow in obtaining random elements, while it is faster than ArrayList in adding and deleting elements. because it has the following methods:
Void addFirst (Object), void addLast (Object), E removeFirst (), E removeLast ()
E getFirst (), E getLast ()
E element () finds but does not remove the header of this list (the first element ).
E peek () finds but does not remove the header of this list (the first element ).
E poll () find and remove the header (first element) of this list ).
Boolean offer (E o) adds the specified element to the end of the list (the last element ).
1.3 Vector <T>
This also has the same characteristics as the above two classes, that is, repeated elements can be stored, and elements are stored in the order of addition. the difference is that the implementation of this class is thread-safe. in the four operations of modifying, removing, adding, and retrieving elements, it is based on the sorted list <T> (but it is not a subclass of the sorted list <T>. this is based on "it", just because their methods are similar.) Some methods are added. These methods have different names, parameter sequences, and return values, however, the functions are the same.
Modify: void setElementAt (E, int index) [E set (int index, E o)]
Remove: void removeElementAt (int index) [E remove (int index)]
Add: void insertElementAt (E, int index) [E add (int index, E o)]
Void addElement (E o)
Boolean removeElement (Object)
Void removeAllElements ()
Obtain:
E elementAt (int index) [E get (int index)]
E firstElement (), E lastElement ()
The method defined in List <T> is in square brackets.
Each of the above three classes has a nonargument and a Collection <? Extends E> c is the constructor of the parameter.

List <T> there are other implementation classes, so I won't talk about them here.
Let's look at the Set <T>
Set <T> is similar to a Set in mathematics. It cannot store duplicate elements, which is different from List <T>. another difference between it and List <T> is that most of its implementation classes store elements in an ordered manner, rather than in the order of adding elements.
The Set <T> method is inherited from its parent Interface Collection <T>.
The following shows its implementation class.
2.1 HashSet <E>
It uses the hashCode () of an element to sort the elements in descending order and store them in an orderly manner.
All of its methods are in the interface set <E>
2.1.1 linkedhashset <E>
If you want to store elements in the order of addition, you can use this class. linkedhashset <E> is a subclass of hashset <E>.
2.2 sortedset <E>
This is an interface, not a class. Its function is to use the comparator <E> class or comparable <E> interface to customize the storage sequence of elements.
Besides inheriting from the parent interface, the methods include:
Comparator <E> comparator ()
E first (), e last ()
Sortedset <E> headset (E toelement), sortedset <E> tailset (E fromelement)
Sortedset <E> subset (E toelement, e fromelement)
2.2.1 treeset <E>
Implementation class of sortedset <E>. Its methods are inherited from the interface sortedset <E>.
2.3 enumset <E>
This is a special set <E> Implementation class used with enumeration types.
It has no constructor, and all its methods are static.
Its methods include:
<E extends enumset <E> allof (class <E> elementtype)
<E extends EnumSet <E> noneOf (Class <E> elementType)
<E extends EnumSet <E> complementOf (EnumSet <E> enumSet)
<E extends EnumSet <E> copyOf (EnumSet <E> enumSet)
<E extends EnumSet <E> copyOf (Collection <E> c)
<E extends EnumSet <E> of (E)
<E extends EnumSet <E> of (E e1, E e2)
<E extends EnumSet <E> of (E first, E... rest)
<E extends EnumSet <E> range (E from, E)

The sub-interfaces and implementation classes of Collection come to an end. Now let's look at Map <K, V>
Map <K, V> is used to Map keys to values. Similar to Set <E>, Map cannot contain duplicate keys (the values mapped by keys can be the same)
Its methods include:
Add:
V put (K key, V value)
Void putAll (Map <? Extends K ,? Extends V> t)
Delete:
V remove (Object key), void clear ()
Query:
V get (Object key)
Conversion from Collection <E>:
Set <K> keySet (), Collection <V> values ()
Note that as mentioned above, it cannot contain duplicate keys, and the values mapped by keys can be the same. therefore, the Set interface with no repeated elements is returned for the key, and the Collection with repeated elements can be returned for the value. <E>
Set <Map. Entry <K, V> entrySet ()
A Map. Entry <K, V> class instance represents a key-value ing. Its methods include:
Boolean equals (Object o)
K getKey ()
V getValue ()
Int hashCode ()
V setValue (V value)
Other Map <K, V> methods:
Boolean containsKey (Object key), boolean containsValue (Object value)
Boolean equals (), int hashCode (), boolean isEmpty ()
Int size ()

The following describes the implementation classes of Map <K, V>.
1.1 HashMap <K, V>
It corresponds to HashSet <E> and features the same, that is, the order in which elements are stored is in descending order of key values.
Its methods are inherited from Map <K, V>.
1.1.1 LinkedHashMap <K, V>
It corresponds to javashashset <E> and stores elements in the order of addition.
1.2 SortedMap <K, V>
It corresponds to SortedSet <E> and uses the Comparator <E> class or Comparable <E> interface to customize the storage sequence of elements.
The method is the same as SortedSet <E>. Basically, you only need to replace the set in the method with map, as shown below:
Comparator <E> comparator ()
K firstKey (), K lastKey ()
SortedMap <K, V> headMap (K toKey), SortedMap <K, V> tailSet (K fromKey)
SortedMap <K, V> subMap (K toKey, E fromKey)
1.2.1 TreeMap <K, V>
Corresponding to the implementation class of TreeSet <E> and SortedMap <K, V>, its methods are inherited from the interface SortedMap <K, V>.
1.3 EnumMap <K extends Enum <K>, V>
The key must be an element of an enumeration type.

Map <K, V> has many other implementation classes.

Note the following two classes: collections class and arrays class.
Here we only talk about the arrays class:
It is mainly used to operate arrays. Its methods are static and there are several categories of methods.
1. Search
Int binarysearch (T [] A, T valuetofind)
T can be any native type or class type.
You can also specify a comparator,
<T> int binarysearch (T [] A, T key, comparator <? Super T> C)
2. Fill
Fill in the array
Void fill (T [] A, T value)
T can be any native type or class type.
You can also specify the filling range.
Void fill (T [] A, int fromindex, int toindex, t Val)
3. Compare whether the two arrays contain identical elements.
Boolean equals (T [] A, T [] B)
4. Sort Arrays
Void sort (T [])
Void sort (T [] A, int fromindex, int toindex)
Void sort (T [] A, comparator C)
Void sort (T [] A, int fromindex, int toindex, comparator C)
T can be any native type.
5. Convert the array to a string
String tostrint (T [])
T can be any native type.
6. Convert the array to list <E>
<T> List <t> aslist (T...)

In other articles on my blog, I have provided examples of the content mentioned above.

========================================================
Thank you for your corrections, comments, and exchanges.

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.