Java set Introduction 2

Source: Internet
Author: User
Tags shuffle

3. List Interface


4. Queue Interface

Queue is used to simulate the data structure of a queue. A queue usually refers to a "first-in-first-out" (FIFO) container. The queue header stores the longest time elements in the queue, and the tail of the queue stores the shortest time elements in the queue. When a new element is inserted (offer) to the end of the queue, the access element (poll) operation will return the elements in the queue header. Generally, the queue does not allow random access to the elements in the queue.

The queue interface defines the following methods:

Void add (Object E): adds a specified element to the end of the queue.

Objeot element (): gets the element in the queue header, but does not delete this element.

"Boolean offer (Object E): adds the specified element to the end of the queue. This method is generally better than the add (Object E) method when a queue with a capacity limit is used.

Object PEEK (): gets the element in the queue header, but does not delete this element. If this queue is empty, null is returned.

Object poll (): gets the element in the queue header and deletes the element. If this queue is empty, null is returned.

"Object remove (): gets the element in the queue header and deletes the element.

  Queue has two common implementation classes: queue list and priorityqueue, Shortlist It is a two-way queue and can be used as a "stack" because it contains Pop (Out stack) and Push (In the stack) two methods, in addition, we can see that it also implements List Interface, so it can also be used List . But because Shortlist The implementation mechanism is different from Arraylist And Vector It stores the elements in the set in the form of a list, so the performance is poor when Random Access to the set elements, however, when inserting or deleting elements, the performance is very good (only change the address indicated by the pointer). In fact Vector Because of the synchronization function, the performance also lags behind Arraylist So we use more Arraylist Implementation class. We generally consider using it when there are special requirements. Shortlist . As Priorityqueue Is a standard queue implementation class, which has requirements on elements and Treeset The requirements are basically the same. I will not explain them here.

5. Map Interface


6. Operation set tool class: collections

Java provides a collection tool class for operations such as set, list, and map: collections, which provides a large number of methods

Sorts, queries, and modifies set elements. You can also set objects to immutable and synchronize set objects.

Such as control, to make up for the failure of synchronization such as hashmap and arraylist.

Sort operation--Collections provides the following methods for sorting list set elements:

Static void reverse (list IIST): reverses the order of elements in a specified list set.

Static void shuffle (list): random sorting of elements in the list set (the shuffle method simulates the "shuffling" action ).

Static void sort (list): sort the elements of a specified list set in ascending order based on the natural order of the elements.

Static void sort (list IiSt, comparator C): sorts the elements in the list set according to the order produced by the specified comparator.

Static void swap (list, int I, Int J): switches the elements at I and j in the specified list set.

Static void rotate (list IiSt, int distance): exchanges the elements at I and at list. length 1-I in the specified set.

QueryFind,ReplaceOperation--CollectiONS also provides the following common methods for searching and replacing set elements:

Static int binarysearch (list, object key): Use the Binary Search Method to search for the specified list set to obtain the index of the specified object in the list set. If you want this method to work properly, make sure that the elements in the list are already in order.

Static object Max (Collection Coll): returns the largest element in a given set in the natural order of elements.

Static object Max (Collection Coll, comparator comp): returns the maximum element of a given set based on the order in which comparator is generated.

Static object min (coliection Coll): returns the minimum element in a given set in the natural order of elements.

Static object min (Collection Coll, comparator comp): returns the minimum element of a given set based on the order in which comparator is generated.

Static void fill (list IiSt, object OBJ): replace all elements in the specified list set with the specified Element obj.

Static int frequency (collection C, object O): returns the number of elements in the specified set that are equal to the specified object.

Stetic int indexofsublist (list source, list target): returns the location index of the sublist object that appears for the first time in the parent list object. If such a sublist does not appear in the parent Liat, -1 is returned.

Stetic int iastlndexofsublist (list source, list target): returns the location index of the Child list object in the parent list object for the last time: if the parent list does not contain such a child Liat, -1 is returned.

Static Boolean replaceall (list IiSt, object oldval, object newval): Use a new value newval to replace all oldval values of the list object.

Synchronization Control:

Multiple synchronizedxxx methods are provided in the collections class. This method returns the synchronization object corresponding to the specified set object, which can solve the thread security problem when multiple threads access the set concurrently. Perform the following exercises:

 
Public class testsynchronized {public static void main (string [] ARGs) {// belowProgramFour synchronized collection objects are created. Collection c = collections. synchronizedcollection (New arraylist (); List list = collections. synchronizedlist (New arraylist (); Set S = collections. synchronizedset (New hashset (); map M = collections. synchronlzedmap (New hashmap ());}}

SetImmutable set--Collections provides the following three methods to return an unchangeable set:

Emptyxxx (): returns an empty and unchangeable set object. The set here can be a list, a set, or a map.

"Singletonxxx (): returns an immutable set object that only contains the specified object (only one or one element). The set here can be a list or set, or map.

"Unmodifiablexxx: returns the immutable view of the specified set object. The set here can be either list, set, or map.

 

Even if the Java Collection is basically finished, we will refer to the API documentation for more details...

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.