Collection
Collection is the interface of Set List queue and Deque
Queue: Advanced First Out queue
Deque: Two-way linked list
Note: There is no relationship between collection and map
Collection is a set of objects, and Map is a key value pair.
Collections is a class, a container's tool class, just as arrays is an array of tool classes
Reverse
Reverse the data in the list to flip
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
//initialization collection numbers
list<integer> Numbers = new arraylist<> ();
for (int i = 0; i < i++) {
numbers.add (i);
}
SYSTEM.OUT.PRINTLN ("Data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.reverse (numbers);
SYSTEM.OUT.PRINTLN ("Flip Data in a collection:");
SYSTEM.OUT.PRINTLN (numbers);
}
Confuse
Shuffle confuse the order of the data in the list
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
//initialization collection numbers
list<integer> Numbers = new arraylist<> ();
for (int i = 0; i < i++) {
numbers.add (i);
}
SYSTEM.OUT.PRINTLN ("Data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.shuffle (numbers);
SYSTEM.OUT.PRINTLN ("Confusing data in a collection:");
SYSTEM.OUT.PRINTLN (numbers);
}
Sort sorts to sort the data in a list
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
//initialization collection numbers
list<integer> Numbers = new arraylist<> ();
for (int i = 0; i < i++) {
numbers.add (i);
}
SYSTEM.OUT.PRINTLN ("Data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.shuffle (numbers);
SYSTEM.OUT.PRINTLN ("Confusing data in a collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.sort (numbers);
SYSTEM.OUT.PRINTLN ("Data in a sorted collection:");
SYSTEM.OUT.PRINTLN (numbers);
}
Exchange
Swap for two data locations
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
//initialization collection numbers
list<integer> Numbers = new arraylist<> ();
for (int i = 0; i < i++) {
numbers.add (i);
}
SYSTEM.OUT.PRINTLN ("Data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.swap (numbers,0,5);
SYSTEM.OUT.PRINTLN ("Exchange 0 and 5 subscript data, data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
}
Rolling
Rotate the data in the list to the right by scrolling the length of the specified unit (looping right)
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
//initialization collection numbers
list<integer> Numbers = new arraylist<> ();
for (int i = 0; i < i++) {
numbers.add (i);
}
SYSTEM.OUT.PRINTLN ("Data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
Collections.rotate (numbers,2);
SYSTEM.OUT.PRINTLN ("Roll the set to the right 2 units, after the marked data, the data in the collection:");
SYSTEM.OUT.PRINTLN (numbers);
}
Thread Safety
Synchronizedlist the non-thread-safe list into a thread-safe list
Package collection;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class Testcollection {public
static void Main (string[] args) {
list<integer> numbers = new ArrayList <> ();
SYSTEM.OUT.PRINTLN ("Convert non-thread-safe list to Thread-safe list");
List<integer> synchronizednumbers = (list<integer>) collections.synchronizedlist (numbers);
}