Java has learned twenty-four (collection tool class Collections) from scratch, and tool class collections
I. Collections introduction in Application Development of Collections, several interfaces and several subclasses of a set are the most commonly used. However, in JDK, a tool class for collection operations is provided-Collections, you can directly use this convenient operation set 2. Common methods and constants of the Collections class
No. |
Method |
Type |
Description |
1 |
Public static final List EMPTY_LIST |
Constant |
Returns an empty List set. |
2 |
Public static final Set EMPTY_SET |
Constant |
Returns an empty Set. |
3 |
Public static final Map EMPTY_MAP |
Constant |
Returns an empty Map set. |
4 |
Public static <T> boolean addAll (Collection <? Super T> c, T...) |
Normal |
Add content to a set |
5 |
Public static <T extends Object & Comparable <? Super T> T max (Collection <? Extends T> coll) |
Normal |
Find the maximum content, sort by comparator, and return the maximum element of the given set in the natural order of the elements |
6 |
Public static <T extends Object & Comparable <? Super T> T min (Collection <? Extends T> coll) |
Normal |
Find the minimum content in the set and return the minimum elements of the given set in the natural order of the elements by the comparator. |
7 |
Public static <T> boolean replaceAll (List <T> list, T oldVal, T newVal) |
Normal |
Replace the specified content of the set with a new value to replace all the specified values in the set with another value. |
8 |
Public static void reverse (List <?> List) |
Normal |
Reverses the order of elements in a specified List set. |
9 |
Public static <T> int binarySearch (List <? Extends Comparable <? Super T> list, T key) |
Normal |
Find the specified content in the Set Use the binary search algorithm to find the specified List set to obtain Determines the index location of an object. |
10 |
Public static final <T> List <T> emptyList () |
Normal |
Returns an empty List set. |
11 |
Public static final <K, V> Map <K, V> emptyMap () |
Normal |
Returns an empty Map set. |
12 |
Public static final <T> Set <T> emptySet () |
Normal |
Returns an empty Set. |
13 |
Public static <T extends Comparable <? Super T> void sort (List <T> list) |
Normal |
Sort the set by the Comparable interface. Based on the natural order of elements Sort. |
14 |
Public static void swap (List <?> List, int I, int j) |
Normal |
Swap elements at a specified position |
3. Reverse the element order in the specified List set
Package com. pb. demo2; import java. util. collections; import java. util. using list; import java. util. list; public class CollectionsTest1 {public static void main (String [] args) {// declare the set and add the element 'LIST' <String> list = new struct list <String> (); List. add ("one"); list. add ("two"); list. add ("three"); System. out. println ("======= normal traversal =============="); for (String s: list) {System. out. print (s + "\ t");} System. out. println (); System. out. println ("======= execute the reverse method =============="); Collections. reverse (list); for (String s: list) {System. out. print (s + "\ t ");}}}
4. The sort method is used to sort the specified List set in ascending order.
Package com. pb. demo2; import java. util. arrayList; import java. util. collections; import java. util. list; public class CollectionsTest2 {public static void main (String [] args) {// declare the set and add the set List <Integer> list = new ArrayList <Integer> (); list. add (7); list. add (2); list. add (5); list. add (3); list. add (1); System. out. println ("======= normal traversal =============="); for (Integer I: list) {System. out. print (I + "\ t");} System. out. println ("\ n ======= execute the sort method ============="); Collections. sort (list); for (Integer I: list) {System. out. print (I + "\ t");} System. out. println ("\ n ======= execute the reverse method =============="); Collections. reverse (list); for (Integer I: list) {System. out. print (I + "\ t ");}}}
Result:
========= Normal traversal ================= 7 2 5 3 1 ======= execute the sort method ==== ========= 1 2 3 5 7 ======= execute the reverse method =================== 7 5 3 2 1
5. Set search and Replacement Methods
Package com. pb. demo2; import java. util. arrayList; import java. util. collections; import java. util. list; public class CollectionsTest3 {public static void main (String [] args) {// declare the set and add the set. // create the set List <String> list = new ArrayList <String> (); // Add 10 different word lists. add ("this"); list. add ("is"); list. add ("collection"); list. add ("test"); list. add ("and"); list. add ("we"); list. add ("can"); list. add ("learn"); list. add ("how"); list. add ("to"); System. out. println ("======= normal traversal =============="); for (String s: list) {System. out. print (s + "\ t");} // print the maximum and minimum elements in the output set System. out. println ("\ n ========= print the maximum and minimum elements in the output set ========="); String max = Collections. max (list); String min = Collections. min (list); System. out. println ("maximum value in the list set:" + max); System. out. println ("minimum value in the list set:" + min); System. out. println ("\ n ======= execute the sort method ============="); Collections. sort (list); for (String s: list) {System. out. print (s + "\ t");} // binarySearch can be used only when sorting is required. out. println ("\ n =============== binarySearch search index by content =========================" ); system. out. println ("we index in the list is:" + Collections. binarySearch (list, "we"); System. out. println ("=============== replaceAll method ======================== "); // replaceAll Collections. replaceAll (list, "we", "our"); for (String s: list) {System. out. print (s + "\ t");} // swap System. out. println ("====== swap ======"); Collections. swap (list, 0, 9); for (String s: list) {System. out. print (s + "\ t ");}}}
Result:
========= Normal traversal ================= this is collection test and we can learn how to =========== print maximum and minimum elements in the output set ======== maximum values in the list set: minimum value in the welist set: and ======= execute the sort method ============== and can collection how is learn test this to we ========== ======= binarySearch search index by content ====================== the index in the list of we is: 9 =============== replaceAll method ========================== and can collection how is learn test this to our ====== swap ====== our can collection how is learn test this to and
6. Use Arrays. asList to generate a set
Package com. pb. demo2; import java. util. arrays; import java. util. list; public class Test1 {public static void main (String [] args) {String str = "a, B, c, d, e, f "; list <String> list1 = Arrays. asList (str); System. out. println ("length of list1:" + list1.size (); for (String s: list1) {System. out. print (s + "\ t");} System. out. println ("\ n is separated by commas to generate a set"); List <String> list2 = Arrays. asList (str. split (","); System. out. println ("length of list2:" + list2.size (); for (String s: list2) {System. out. print (s + "\ t ");}}}
Result:
List1 length: 1a, B, c, d, e, f separated by commas (,), the length of list2 is generated: 6a B c d e f