Java Notes-9

Source: Internet
Author: User

Summary:introduce the collection feature of Java. Includes Set. List and Queue

-collection interface only define some basic common functions for all kinds of collections, Includs:

Boolean Add (E), remove (E), contains (e),

int size (),

-collection can easily convert RO array as the example bellow:

Public object[] ToArray
Collection <String> mycollection; string[] mystrings = Mycollection.toarray (new string[0]);


-iterator very often used in Java as a squencial access of elements

E Next ()//get next element

Boolean hasnext ()//in case of non-last element, Rteturn True

Example:

Pirnt all the elements of one collection

Remove () removes the most recent obj by next ()

-special for loop for collection:

.

-set Collection:duplicate not allowed

List Collection:elements in specific order

Queue Collection is working as buffer

Set:sortedset, Java7 added Navigableset, which the Add method can find most closest value to target then fulfil the A Dding

List similar to array, but can operate eaily on position of element

public void Add (int index, e element);p ublic void Remove (int index);p ublic E get (int index);p ublic Object Set (int index, E Element);

-queue can be FIFO or LIFO

Boolean offer (E element)//place the element to the the queue, but only return faulse instead of throw an Exceptione poll// Removes element at head of the "queue", in case of failure return null instead of throwing Exceptione peek ()//return head WI Thout removing


Java 7 added Dequeue, which means can operate from 2 ends of the the queue, (head or tial). Corresponding method changed As:offerlast ();p ollfirst ();p olllast ();p eelfirst ();p eeklast ();

-blockingqueue

Enhance The capablity of timed wait, so this can adapt to Multipul thread access.

Public boolean offer (E element, long time, timeunit units)


This method attempts the element into the queue, just like the method of
The base Queue interface, but blocks for up to the specified period of time as it waits
For space to become available.


Public E Poll (long time, timeunit unit)


This method attempts to remove the element at the head of the queue, just like the
Method of the base Queue interface, but blocks for the specified period of time
As it waits for a element to become available.


Public E Take ()


This method retrieves the element at the head of the queue, blocking if necessary
Until one becomes available.


public void put (E element)


This method adds a element to the queue, blocking if necessary until space be‐
Comes available.


Next:map interface ...

Java Notes-9

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.