The difference between collection and collections in Java

Source: Internet
Author: User

1. Collection is a top-level interface to the collection class, and its direct inheritance interface has a list and set

Collections is a tool class/helper class for the collection class, which provides a series of static methods for sorting, searching, and line Cheng of elements in the collection.

1) Sorting (sort)
Use the Sort method to sort the specified list in ascending order based on the natural ordering of the elements. All elements in the list must implement the comparable interface. All elements within this list must be compared using the specified comparer.
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));
}
Results: 112,111,23,456,231
2) mixed row (Shuffling)
The blending algorithm does exactly the opposite of sort: it disrupts the traces of any permutation that may be in a list. That is, the List is rearranged based on the input of the random source, which has the same probability (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 mix a List of card objects that represent a deck of cards. In addition, it is also 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) Inversion (Reverse)
Use the reverse method to sort the specified list in descending order based on the natural ordering 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 so the element (Fill)
Replaces 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));

}
Results: AAA,AAA,AAA,AAA,AAA

5) Copy (copy)
With two parameters, a target list and a source list, the element of the source is copied to the target, and its contents are overwritten.

The target List is at least as long as the source. If it is longer, the remaining elements in the target list are unaffected.
Collections.copy (List,li): The following 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]));
}
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));
}
Results: 1131,333,23,456,231
6) returns the smallest element in the collections (min)
Returns the smallest element of a given collection, based on the order produced by the specified comparer. All elements in the collection must be compared by specifying the comparer to compare to each other.
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));
}
Results: 23
7) returns the smallest element (max) in collections
Returns the largest element of a given collection, based on the order produced by the specified comparer. All elements in the collection must be compared by specifying the comparer to compare to each other.
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));
}
Results: 456
8) Lastindexofsublist
Returns the starting position of the specified target list in the specified source list for the last occurrence
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);
Results 3
9) Indexofsublist
Returns the starting position for the first occurrence of the specified target list 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);
Results 1
) Rotate
Moves an element in a specified list by a specified distance loop
Collections.rotate (list,-1);
If it is a negative number, it moves forward and the positive direction moves.
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));
}
Results: 111,23,456,231,112

The difference between collection and collections in Java

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.