Key points: Java20java. util. Collections

Source: Internet
Author: User
Java. util. collections collection help class example Program (JUnit demo) sorting @ TestpublicvoidtestSort () {ListIntegerdemoListnewArrayListInteger (Arrays. asList (3, 2, 1); assertEquals (3, demoList. get (0 ). intValue (); publicstaticTextendsComparable

Java. util. collections collection help class example Program (JUnit demo) sorting @ Test public void testSort () {ListInteger demoList = new ArrayListInteger (Arrays. asList (3, 2, 1); assertEquals (3, demoList. get (0 ). intValue (); // public static T extends Comparable

Java. util. Collections collection help class example Program (JUnit demo) sorting
@ Test public void testSort () {List
  
   
DemoList = new ArrayList
   
    
(Arrays. asList (3, 2, 1); assertEquals (3, demoList. get (0). intValue (); // public static
    
     
> Void sort (List
     
      
List) Collections. sort (demoList); assertEquals (1, demoList. get (0). intValue (); // public static void shuffle (List
      List) // random sorting // public static void reverse (List
      List) // Reverse Order}
     
    
   
  
Search
@ Test public void testBinarySearch () {List
   
    
DemoList = new ArrayList
    
     
(Arrays. asList (3, 2, 1); // you must first sort Collections. sort (demoList); assertEquals (1, demoList. get (0 ). intValue (); // Binary Search location // public static
     
      
Int binarySearch (List
      > List, T key) assertEquals (0, Collections. binarySearch (demoList, 1); // public static int indexOfSubList (List
      Source, List
      Target) // public static int lastIndexOfSubList (List
      Source, List
      Target )}
     
    
   
Replacement
@ Test public void testSwap () {List
    
     
DemoList = new ArrayList
     
      
(Arrays. asList (3, 2, 1); // public static void swap (List
      List, int I, int j) Collections. swap (demoList, 0, 2); assertEquals (1, demoList. get (0 ). intValue (); // public static void rotate (List
      List, int distance) // specify the distance rotation // public static
      
       
Boolean replaceAll (List
       
        
List, T oldVal, T newVal) // specifies the replacement}
       
      
     
    
Copy
@ Test public void testCopy () {List
     
      
DemoList = new ArrayList
      
       
(Arrays. asList (3, 2, 1); // public static
       
        
Void copy (List
        Dest, List
        Src) // note that the size of the destination List must be equal to the size List of src at least.
        
         
CopyList = new ArrayList
         
           (Arrays. asList (1, 2, 3); Collections. copy (copyList, demoList); assertEquals (3, copyList. size (); assertEquals (3, copyList. get (0 ). intValue (); copyList. add (4); assertEquals (3, demoList. size ());}
         
        
       
      
     
Comparison
    @Test    public void testCompary() {        List
      
        demoList = new ArrayList
       
        (Arrays.asList(3, 2, 1));        assertEquals(1, Collections.min(demoList).intValue());        assertEquals(3, Collections.max(demoList).intValue());        List
        
          compareList = new ArrayList
         
          (Arrays.asList(5, 6, 7));        //Returns true if the two specified collections have no elements in common        assertTrue(Collections.disjoint(demoList, compareList));    }
         
        
       
      
Create different sets
@ SuppressWarnings ("unused") @ Test public void testCreate () {List
       
        
DemoList = new ArrayList
        
         
(Arrays. asList (3, 2, 1); // empty object size = 0 No add method List
         
          
EmptyList = Collections. emptyList (); // returns an unchangeable list containing only the specified object. List
          
           
SingletonList = Collections. singletonList (1); // return a dynamic security view of the specified list. List
           
             CheckedList = Collections. checkedList (demoList, Integer. class); // return the unchangeable view of the specified list. List
            
              UnmodifiableList = Collections. unmodifiableList (demoList); // return the synchronization (thread-safe) List supported by the specified list. List
             
               SynchronizedList = Collections. synchronizedList (demoList); synchronized (synchronizedList) {Iterator
              
                I = synchronizedList. iterator (); // Must be in synchronized block while (I. hasNext () I. next ();} // map set sortedMap sortedSet}
              
             
            
           
          
         
        
       

Environment jdk1.6 window7 junit4

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.