Java Collection (5): Understanding Collection

Source: Internet
Author: User
Tags addall comparable

Collection is the common interface for list, Set, and queue. The main methods of collection are:

int size (): Returns the number of elements in the current collection

Boolean Add (E E): Add object to collection

Boolean remove (Object o): Deletes the specified object

Boolean contains (Object O): finds whether the specified object is in the collection

Boolean containsall (collection<?> C): finds whether the collection has elements in set C

Boolean isEmpty (): Determines whether the collection is empty

Iterator Iterator (): returns an iterator

Boolean AddAll (collection<? extends e> c): adds all the elements of collection C to the collection

Boolean RemoveAll (collection<?> C): removes elements from the collection that are also in the C collection

Boolean retainall (collection<?> C): removes elements not contained in collection C from the collection

void Clear (): Delete all elements in the collection

Object ToArray (): Returns an array that contains all the elements in the container

<T> t[] ToArray (t[] a): returns an array that contains all the elements in the container, and the run-time type of the returned result is the same as the parameter a type

List<e> set<e> queue<e> are all interfaces that inherit collection<e>, and they also have their own methods:

(1) list<e>

A list is an ordered, repeatable, nullable collection of elements. The implementation class of the list interface is arranged according to the index when it implements the insert element. In addition to some of the methods that inherit collection, the list provides the following actions:

Read Insert Delete at specified location

E get (int index)

e Set (int index, e Element)

void Add (int index, E element);

Boolean addall (int index, COLLECTION<? extends e> c)

E Remove (int index)

Find the location of an object

int indexOf (Object o)

int lastIndexOf (Object o)

Use iterator's extended version iterator Listiterator for iterative operations. With Listiterator, the list can be traversed forward and backward, while the add, set, remove, and other operations are allowed.

Listiterator<e> Listiterator ()

Use the Sublist method to perform any range of operations on a list

list<e> sublist (int fromIndex, int toindex)

Some of the most commonly used list implementation classes in the Java Collection Framework are arraylist,linkedlist and vectors. ArrayList as the default selection. when insertions and deletions are frequent, use LinkedList. vector supports thread synchronization, which means that only one thread at a time can write vectors and avoid inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so accessing it is slower than accessing ArrayList.

(2) set<e>

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. Set has exactly the same interface as collection, so the set interface cannot maintain the order of the elements.

HashSet, as the default choice, is designed for quick find, and the elements that are stored in it must define HASHCODE (). TreeSet is a set of holding order, the underlying structure is a tree structure, it can extract an ordered sequence from set, the element must implement the comparable interface. Linkedhashset has a hashset query speed, while internally using a list to maintain the order in which elements are inserted , the elements stored in it must also be defined hashcode ().

(3) queue<e>

A queue is a special linear table that allows for deletion only at the front end of the table (front), and in the back end of the table (rear). The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. Follow the FIFO principle. The bottom of the queue uses an array.

Boolean Add (E): adds elements to the queue, which is equivalent to entering the tail queue (Collection style)

Boolean offer (E): adds an element to the queue, which is equivalent to queuing (queue style)

E Remove (): Remove team head element (Collection style)

E poll (): Remove Team header element (queue style)

E element (): Gets the element that does not remove the queue header (Collection style)

E Peek (): Gets the element (queue style) without removing the header

In addition to concurrent applications, Queue has several implementations in the current Java:linkedlist-->deque-->queue,priorityqueue-->queue,arraydeque--> Deque-->queue.

Deque is a two-way queue, which means that both sides can do the head/tail of the team. Thus increased AddFirst (e)/offerfirst (e E), AddLast (e E)/offerlast (e), Removefirst ()/pollfirst (), Removelast ()/polllast (), GetFirst ()/peekfirst (), GetLast ()/peeklast () method.

Priorityqueue is a priority queue, and elements need to implement the comparable interface. The high priority in the CompareTo () method is first taken out.

Java Collection (5): Understanding Collection

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.