Collection and Collections of the difference

1、java.util.Collection

  It is a collection interfaces (top-level interface to a set of classes). It provides a common set of interface methods objects for basic operations. Collection interface has many specific implementation in the Java class libraries. Significance of the Collection interface is provided to maximize the unified mode of operation for a variety of specific collections that have a direct interface to inherit the Set List and Queue.

2、java.util.Collections

  It is a wrapper class (Tools / helper). It includes various static method of relating a set of multi-state operation. This class can not be instantiated, like a utility class for a collection of elements to sort, search, and thread-safe operations and other services in the Java Collection Framework.

Commonly used are:

(1) sorting sort (Collection)

如Collections.sort(List<T> list),Collections.sort(List<T> list, Comparator<? super T> c)。

Use sort method can be designated by the list according to the natural order of the elements in ascending order. All elements in the list must implement the Comparable interface, and must be specified using the comparator can be compared with each other.

1 @SuppressWarnings("unchecked")
2 public static <T extends Comparable<? super T>> void sort(List<T> list) {
3     list.sort(null);
4 }
1 @SuppressWarnings({"unchecked", "rawtypes"})
2 public static <T> void sort(List<T> list, Comparator<? super T> c) {
3     list.sort(c);
4 }

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.sort(list);
5         System.out.println(list);
6     }
7 }

operation result:

[Five, Four, One, Three, TWO]    // (in alphabetical order)

(2)混排 shuffle(Collection)

如Collections.shuffle(List<?> list)

Based on the input source of randomness random ordering of the List, this algorithm is very useful for the realization of a game of chance, is also useful in the generation of test cases.

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.shuffle(list);
5         System.out.println(list);
6     }
7 }

operation result:

[three, five, four, one, two]

(3) reverse reverse (Collection)

The Collections.reverse (List <?> List)
using the reverse () Reverse the order of elements in the set

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.reverse(list);
5         System.out.println(list);
6     }
7 }

operation result:

[five, four, three, two, one]

(4) replacing all of the elements fill (List list, Object o)

Alternatively the set of all elements in the specified element.

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.fill(list, "zero");
5         System.out.println(list);
6     }
7 }

operation result:

[zero, zero, zero, zero, zero]

(5) Copy copy (List list1, List list2)

The set of the elements list2 list1 copied to, and cover elements of the corresponding index. Target list1 least as long as the source list2.

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list1 = Arrays.asList("one two three four five".split(" "));
4         List list2 = Arrays.asList("一 二 三 四 五".split(" "));
5 Collections.copy(list1, list2);
6         System.out.println(list1);
7     }
8 }

Operating results :

[One, two, three, four, five]

(6)rotate(List list,int m)

Cyclic movement element in the list according to a specified distance m. Elements of a set of m set back position, the cover element to the front in later cycles.

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list1 = Arrays.asList("one two three four five".split(" "));
4         Collections.rotate(list1, 2);
5         System.out.println(list1);
6     }
7 }

operation result:

[four, five, one, two, three]

(7) the minimum (large) element min (), max ()

The comparator generates the order specified, returns the given minimum Collection (large) element.

min(Collection),min(Collection,Comparator)
max(Collection),max(Collection,Comparator)

A simple example:

 1 public class Test {
 2     public static void main(String[] args) {
 3         List list = new ArrayList();
 4         list.add(10);
 5         list.add(40);
 6         list.add(20);
 7         list.add(50);
 8         System.out.println(Collections.min(list));
 9         System.out.println(Collections.max(list));
10     }
11 }

operation result:

10     
50

(8)indexOfSublist(List list,List sublist)

Find the first time the index sublist position in the list. Return to the starting position of the specified target list, the list of the first occurrence of the specified source.

A simple example:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         List subList=Arrays.asList("three four five".split(" "));
5         System.out.println(Collections.indexOfSubList(list,subList));
6 
7     }
8 }

operation result:

2

(9)lastIndexOfSublist(List list,List sublist)

Return to the starting position of the specified target list of the last occurrence of a specified list.

(10)swap(List list,int m,int n)

Collection at the specified index switching element m, n position.

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.swap(list, 2, 3);
5         System.out.println(list);
6     }
7 }

operation result:

[one, two, four, three, five]

 

reference:

1、https://www.jianshu.com/p/0494cce4312a

2 https://www.cnblogs.com/kadaj174/p/11021730.html

 

 

Guess you like

Origin www.cnblogs.com/116970u/p/11497568.html