Collections Tools / Help

Collections is a tool / help class for collections , which provides a series of static methods for sorting, searching, and thread-safety operations on elements in the collection.

1) Sort (Sort) Use the sort method to sort the specified list in ascending order according to the natural order of the elements. All elements in the list must implement the Comparable interface. All elements in this list must be specified using the comparator to be mutually comparable
Double Array [] = {112, 111, 23 is, 456, 231};
for (int I = 0; I <be 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) );
}
// results: 112,111,23,456,231
2) shuffling (shuffling Accelerate)
shuffling algorithm does exactly the opposite and sort: it may have any upset traces arranged in a List. That is, the List is rearranged based on the input of the random source, and such an arrangement has the same possibility (assuming that the random source is fair). This algorithm is very useful in implementing a game of chance. For example, it can be used to shuffle a List of Card objects representing a deck of cards. In addition, it is very 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));
}
// Results: 112,111,23,456,231
3) Reverse
      Use the Reverse method to sort the specified list in descending order according to 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));
}
// Results: 231,456,23,111,112
4 ) Replace all elements (Fill)
with the specified elements to replace all elements in the specified list.
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, aaa, aaa, aaa

5) Copy
with two parameters, a target list and a source list, the elements of the source Copy to the target and overwrite its contents. The target list is at least as long as the source. If it is longer, the remaining elements in the target List are not affected.
Collections.copy (list, li): The last parameter is the target list, the previous one 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]));


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 smallest element (min) in the Collections
according to the order generated by the specified comparator, and returns the smallest element of the given collection. All elements in the collection must be comparable to each other through 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 <
System.out.println ("list [" + i + "] =" + list.get (i));
}
// Result: 23
7) Returns the order of the smallest element (max) in the Collections
according to the specified comparator, Returns the largest element of a given collection. All elements in the collection must be comparable to each other through 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 last occurrence of the specified target list in the specified source list Starting position
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);
//结果 3
9) IndexOfSubList
返回指定源列表中第一次出现指定目标列表的起始位置
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
Cyclically move the elements in the specified list according to the specified distance
Collections.rotate (list,- 1);
If it is a negative number, then move in the positive direction, positive numbers move in the direction
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));
}
//结果:111,23,456,231,112

Published 20 original articles · Likes2 · Visits 10,000+

Collections is a tool / help class for collections , which provides a series of static methods for sorting, searching, and thread-safety operations on elements in the collection.

1) Sort (Sort) Use the sort method to sort the specified list in ascending order according to the natural order of the elements. All elements in the list must implement the Comparable interface. All elements in this list must be specified using the comparator to be mutually comparable
Double Array [] = {112, 111, 23 is, 456, 231};
for (int I = 0; I <be 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) );
}
// results: 112,111,23,456,231
2) shuffling (shuffling Accelerate)
shuffling algorithm does exactly the opposite and sort: it may have any upset traces arranged in a List. That is, the List is rearranged based on the input of the random source, and such an arrangement has the same possibility (assuming that the random source is fair). This algorithm is very useful in implementing a game of chance. For example, it can be used to shuffle a List of Card objects representing a deck of cards. In addition, it is very 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));
}
// Results: 112,111,23,456,231
3) Reverse
      Use the Reverse method to sort the specified list in descending order according to 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));
}
// Results: 231,456,23,111,112
4 ) Replace all elements (Fill)
with the specified elements to replace all elements in the specified list.
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, aaa, aaa, aaa

5) Copy
with two parameters, a target list and a source list, the elements of the source Copy to the target and overwrite its contents. The target list is at least as long as the source. If it is longer, the remaining elements in the target List are not affected.
Collections.copy (list, li): The last parameter is the target list, the previous one 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]));


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 smallest element (min) in the Collections
according to the order generated by the specified comparator, and returns the smallest element of the given collection. All elements in the collection must be comparable to each other through 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 <
System.out.println ("list [" + i + "] =" + list.get (i));
}
// Result: 23
7) Returns the order of the smallest element (max) in the Collections
according to the specified comparator, Returns the largest element of a given collection. All elements in the collection must be comparable to each other through 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 last occurrence of the specified target list in the specified source list Starting position
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);
//结果 3
9) IndexOfSubList
返回指定源列表中第一次出现指定目标列表的起始位置
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
Cyclically move the elements in the specified list according to the specified distance
Collections.rotate (list,- 1);
If it is a negative number, then move in the positive direction, positive numbers move in the direction
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));
}
//结果:111,23,456,231,112

Guess you like

Origin blog.csdn.net/qq_28335347/article/details/54233589