Collection and collections

Source: Internet
Author: User
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);
 
    }









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.