Java --- some static methods provided by collections

Source: Internet
Author: User

1. collection is a top-level collection class.InterfaceWhich directly inherits the list and set interfaces.

Collections is a tool class/help of the Collection class.Class,It provides a series of static methods for sorting, searching, and thread security of elements in a set.

 

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)
Hybrid sorting Algorithm This is exactly the opposite of sort: it breaks the trace of any sort 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 the 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.