Java. util. Collections class package Learning

Source: Internet
Author: User

Java. util. Collections class package Learning
1. Description:
1.1 Overview
The Java. util. Collections class contains many useful methods that can make the programmer's work easier, but these methods are generally not fully utilized. Javadoc provides the most complete description of the collections class: "This class contains a dedicated static class that can be used to operate or return a set.

Methods contained in "1.2"
Iterator, arraylist, elements, buffer, MAP, Collections

Column:
Import java. util. arraylist;
Import java. util. collection;
Import java. util. collections;
Import java. util. comparator;
Import java. util. List;

Public class collectionssort {
Public collectionssort (){

}

Public static void main (string [] ARGs ){
Double array [] = {111,111, 23,456,231 };
List list = new arraylist ();
List li = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
// List. Add ("" + array [I]);
}
Double arr [] ={ 111 };
For (Int J = 0; j <arr. length; j ++ ){
Li. Add (New Double (ARR [J]);
}
}

2. Specific operations
1) sort (SORT)
The sort method can be used to sort the specified list in ascending order based on the natural order of elements. All elements in the list must implement the comparable interface. All elements in this list must be comparable with each other using the specified comparator.

Double array [] = {112,111, 23,456,231 };
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Sort (list );
For (INT I = 0; I <array. length; I ++ ){
System. Out. println (Li. Get (I ));
}
// Result: 112,111, 23,456,231
2) shuffling)
The mixed sorting algorithm is the opposite of sort: It disrupts any trace of the arrangement that may exist in a list. That is to say, the list is rearranged based on the input of the random source. Such an arrangement has the same possibility (assuming that the random source is fair ). This algorithm is very useful in implementing a lucky game. For example, it can be used to represent a list of card objects of a deck. In addition, it is useful when generating test cases.

Collections. Shuffling (list)
Double array [] = {112,111, 23,456,231 };
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Shuffle (list );
For (INT I = 0; I <array. length; I ++ ){
System. Out. println (Li. Get (I ));
}
// Result: 112,111, 23,456,231
3) reverse (reverse)
The reverse method can be used to sort the specified list in descending order based on the natural order of elements.
.
Collections. Reverse (list)
Double array [] = {112,111, 23,456,231 };
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Reverse (list );
For (INT I = 0; I <array. length; I ++ ){
System. Out. println (Li. Get (I ));
}
// Result: 231,456, 23,111,112
4) replace all elements (fill)
Replace all elements in the specified list with the specified element.
String STR [] = {"DD", "AA", "BB", "cc", "ee "};
For (Int J = 0; j <Str. length; j ++ ){
Li. Add (new string (STR [J]);
}
Collections. Fill (Li, "AAA ");
For (INT I = 0; I <li. Size (); I ++ ){
System. Out. println ("list [" + I + "] =" + Li. Get (I ));

}
// Result: AAA, AAA, and AAA

5) Copy)
Use two parameters, one target list and one source list, to copy the source element to the target and overwrite its content. The target list must be at least the same length as the source list. If it is longer, the remaining elements in the target list are not affected.

Collections. Copy (list, Li): the following parameter is the target list, and the first parameter is the source list.
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
List li = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Double arr [] ={ 1131,333 };
String STR [] = {"DD", "AA", "BB", "cc", "ee "};
For (Int J = 0; j <arr. length; j ++ ){
Li. Add (New Double (ARR [J]);
}
Collections. Copy (list, Li );
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. println ("list [" + I + "] =" + list. Get (I ));
}
// Result: 1131,333, 23,456,231
6) returns the minimum element (min) in collections)
Returns the minimum element of a given collection based on the Generation sequence of the specified comparator. All elements in the collection must be compared by the specified comparator.
Collections. Min (list)
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Min (list );
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. println ("list [" + I + "] =" + list. Get (I ));
}
// Result: 23
7) returns the smallest element (max) in collections)
Returns the maximum element of a given collection based on the Generation sequence of the specified comparator. All elements in the collection must be compared by the specified comparator.
Collections. Max (list)
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Max (list );
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. println ("list [" + I + "] =" + list. Get (I ));
}
// Result: 456
8) lastindexofsublist
Returns the starting position of the last time the specified target list appears in the specified source list.
Int COUNT = collections. lastindexofsublist (list, Li );
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
List li = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Double arr [] ={ 111 };
String STR [] = {"DD", "AA", "BB", "cc", "ee "};
For (Int J = 0; j <arr. length; j ++ ){
Li. Add (New Double (ARR [J]);
}
Int locations = collections. lastindexofsublist (list, Li );
System. Out. println ("=" + locations );
// Result 3
9) indexofsublist
Returns the starting position of the specified target list for the first time in the specified source list.
Int COUNT = collections. indexofsublist (list, Li );
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
List li = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Double arr [] ={ 111 };
String STR [] = {"DD", "AA", "BB", "cc", "ee "};
For (Int J = 0; j <arr. length; j ++ ){
Li. Add (New Double (ARR [J]);
}
Int locations = collections. indexofsublist (list, Li );
System. Out. println ("=" + locations );
// Result 1
10) rotate
Move the elements in the specified list cyclically Based on the specified distance
Collections. Rotate (list,-1 );
If it is a negative number, it is moving forward and positive.
Double array [] = {112,111, 23,456,231 };
List list = new arraylist ();
For (INT I = 0; I <array. length; I ++ ){
List. Add (New Double (array [I]);
}
Collections. Rotate (list,-1 );
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. println ("list [" + I + "] =" + list. Get (I ));
}
// Result: 456,231,112

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.