Collections of common Java classes

Source: Internet
Author: User

Note that it is not a collection (Interface), but a collection (s cannot be dropped ).
The definition of the collections class can be described in one sentence. "This class only provides some static methods (there are actually three constants). These methods can be used to operate on the collection object or return the collection object. "Briefly introduces the methods provided by OSS.
The collections class provides methods for querying, copying, filling, sorting, out-of-order, inversion, and switching List objects (that is, collection classes that implement the list interface.
It should be noted that for Java, it is necessary to have a deep understanding of its internal mechanisms and principles. You can understand the API to a certain extent. If you are more familiar with it, you will naturally become familiar with it. At the same time, we should look at the highly handwritten program. It is found to be used when it is used. But the basic requirement is to understand Java
API framework.
① Public static int binarysearch (list, object OBJ)
② Public
Static void copy (list DEST, list SRC)
③ Public static void fill (list
List, object OBJ)
④ Public static void shuffle (list) // disordered
⑤ Public static
Void reverse (list) // Invert
⑥ Public swap (list, int I, int
J) // exchange

The Collection class provides the maximum and minimum values for the Collection object (that is, the collection class that implements the collection interface. It is defined as follows:
① Public
Static object Max (Collection Coll)
② Public static object Max (Collection
Coll, comparator comp)
③ Public static object min (Collection Coll)
④ Public
Static object min (Collection Coll, comparator comp)
Comparator will be described later.

Synchronization
The methods provided by the list, set, and map classes in the "set framework" are non-synchronous, that is, they are not thread-safe, which makes the operation on the set object faster and more efficient, this is also their difference from the previous collection class vector and hashtable. However, if you apply these non-synchronous collection objects to a multi-threaded environment, that is, when multiple threads simultaneously access a collection object and at least one thread needs to change this object, the Set object must be synchronized. This external synchronization operation
This is implemented by using the synchronized () method provided by the collections class. This synchronization operation is usually completed during the constructor of the collection object. A typical example is as follows:
For the arraylist class, the external synchronization is implemented as follows:
List
List = collections. synchronizedlist (New
Arraylist ());
In addition, when performing iterative operations on synchronized collection objects
Keyword synchronizeed: Synchronize the obtained iterator object,
Otherwise, the program will produce uncertain results. A typical example is as follows:
List
List = collections. synchronizedlist (New
Arraylist ());
...
...
Synchronized (list)
{
Iterator
I = List. iterator ();
While (I. hasnext ())
{

Foo (I. Next ());
}
}

Read-Only (unchangeable)
Read-Only processing of a set refers to modifying a set to avoid unexpected operations after all necessary elements are added to the set,
The set cannot be modified. The collections class can be used for collection objects, List objects,
Set object, sortedset object, map object, sortedmap object. The method description
As follows:
① Public
Static collection unmodifiablecollection (Collection Coll)
② Public static set
Unmodifiableset (Set set)
③ Public static sortedset
Unmodifiablesortedset (sortedset)
④ Public static list
Unmodifiablelist (list List)
⑤ Public static map unmodifiablemap (Map
Map)
⑥ Public static sortedmap unmodifiablesortedmap (sortedmap
Sortedmap)

In addition, collections can also create unchangeable orders (singleiton set) for set, list, and map objects ),
It will be supplemented later.
The sample program will be supplemented next time.

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.