Common usage of Java. util. Collections class

Source: Internet
Author: User
Tags addall

This class is a tool class for Java. util. collection.

Field:
Public static final list empty_list;
Public static final map empty_map;
Public static fianl set empty_set;

Method:
Boolean addall (collection C, object...)
Add all specified elements to the specified collection. You can specify the elements to be added, or specify them as an array. The same as C. addall (arrays. aslist (elements )).

Int binarysearch (list, object key)
The binary search algorithm is used to search for a specified list to obtain the specified object. before calling, you must sort the list in ascending order (by collections. sort (list )).
Int binarysearch (list, object key, comparator C)
The binary search algorithm is used to search for a specified list to obtain the specified object. before calling, you must sort the list in ascending order (by collections. sort (list, comparator )).

Collection checkedcollection (collection C, class type)
Returns a dynamic security view of the specified collection.
List checkedlist (list, class type)
Returns a dynamic security view of the specified list.
Map checkedmap (map m, class keytype, class valuetype)
Returns a dynamic security view of the specified ing.
Set checkedset (set S, class type)
Returns a dynamic security view of the specified set.
Sortedmap checkedsortedmap (sortedmap M, class keytype, class valuetype)
Returns a dynamic security view of the specified ing.
Sortedset checkedsortedset (sortedset S, class type)
Returns a dynamic security view of the specified set.

Void copy (list DEST, list SRC)
Copy all elements from one list to another. DEST target list and SRC Source list.

Public static void main (string [] ARGs) throws exception {
List lT = new arraylist ();
Lt. Add ("AA ");
Lt. Add ("BB ");
Lt. Add ("DD ");
Lt. Add ("cc ");
System. Out. println ("Before sorting, use binarysearch to return the Cc index value:" + Java. util. Collections. binarysearch (LT, "cc "));
For (INT I = 0; I <lt. Size (); I ++ ){
System. Out. println ("[" + I + "] Location Value:" + lt. Get (I ));
}
Collections. Sort (LT );
System. Out. println ("\ n after natural sorting, use binarysearch to return the Cc index value:" + Java. util. Collections. binarysearch (LT, "cc "));
For (INT I = 0; I <lt. Size (); I ++ ){
System. Out. println ("[" + I + "] Location Value:" + lt. Get (I ));
}
// Custom sorting
Collections. Sort (LT, new mycomparator ());
System. out. println ("\ n is sorted by a self-written sorting method, and then the Cc index value is returned using binarysearch:" + Java. util. collections. binarysearch (LT, "cc "));
For (INT I = 0; I <lt. Size (); I ++ ){
System. Out. println ("[" + I + "] Location Value:" + lt. Get (I ));
}
System. Out. println ("\ n test Collections ");
/* Checkedlist (list <E> list, class <E> type)
* List-the list of dynamic security views is returned.
* Type-allowed element types held by list
* Return a dynamic security view of the specified list. Inserting an element of the Error Type will immediately throw classcastexception.
* It is assumed that the list does not contain any element of the correct type before the dynamic type security view is generated, and all subsequent accesses to the list will pass
* This view is executed to ensure that the list does not contain elements with incorrect types.
*/
List lt2 = new arraylist ();
Lt2.add (integer. valueof (1 ));
Lt2.add (double. valueof (2.0 ));
Lt2.add (Boolean. valueof (true ));
List B = collections. checkedlist (lt2, Boolean. Class );
B. Add (Boolean. valueof (false); // No error is reported at this time;
B. Add ("CCC"); // the following error occurs:

}

Error message:

Exception in thread "Main" Java. Lang. classcastexception: Attempt to insert class java. Lang. String element into collection with element type class java. Lang. Boolean
At java. util. Collections $ checkedcollection. typecheck (collections. Java: 2202)
At java. util. Collections $ checkedcollection. Add (collections. Java: 2243)
At com. java. Collections. collectiontool. Main (collectiontool. Java: 36)

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.