Collections common method in Java

// encapsulation method shortcut m Alt Shift
 // void return method of the original object is changed in general; the method returns worth will not change the original object 

Import of java.util.ArrayList;
 Import java.util.Collections;
 Import java.util.List; 

public  class CollectionsMethods { 

    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        
        the method1 (); 
        
        method2 (); 
        
        the method3 (); 
        
        method4 (); 
        
        method5 (); 
        
        of method6 (); 
        
        method7 (); 
        
    } 

    Private  static  void method7 () { 
        List<Integer> l1 = new ArrayList<>();
        
        l1.add(12);
        l1.add(0);
        l1.add(44);
        l1.add(22);
        l1.add(100);
        
        Collections.swap(l1, 1, 3);//交换指定索引位置的元素
        System.out.println(l1);
    }

    private static void method6() {
        List<Integer> l1 = new ArrayList<>();
        
        l1.add(12);
        l1.add(55);
        l1.add ( 44 is ); 
        l1.add ( 22 is ); 
        l1.add ( 100 ); 
        
        the Collections.sort (L1); // natural ordering 
        System.out.println (L1); 
    } 
    
    Private  static  void method5 () { 
        List <Integer> = L1 new new the ArrayList <> (); 
        
        l1.add ( 12 is ); 
        l1.add ( . 19 ); 
        l1.add ( 44 is ); 
        
        Collections.shuffle (L1); // random permutation 
        System.out.println (L1); 
    } 

    Private static void method4() {
        List<Integer> l1 = new ArrayList<>();
        
        l1.add(12);
        l1.add(19);
        l1.add(44);
        
        Collections.reverse(l1);//反转
        System.out.println(l1);
    }

    private static void method3() {
        List<String> l1 = new ArrayList<>();
        
        l1.add("yuan");
        l1.add("dai");
        l1.add("qi"); 
        
        Collections.fill (L1, "Hello" ); 
        System.out.println (L1); 
    } 

    Private  static  void method2 () { 
        List <String> L1 = new new the ArrayList <> (); 
        
        l1.add ( "Yuan" ); 
        l1.add ( "DAI" ); 
        l1.add ( "Qi" ); 
        
        List <String> L2 = new new the ArrayList <> ();
         // object set must be larger than the original size of the set size 
        l2.add ( "" ); 
        l2.add ( "" );
        l2.add(""); 
        L2.add ( "" ); 
        Collections.copy (L2, L1); 
        System.out.println (L2); 
    } 

    Private  static  void the method1 () { 
        List <Integer> = L1 new new the ArrayList <> (); 
        
        L1 .add ( 12 is ); 
        l1.add ( . 19 ); 
        l1.add ( 44 is ); 
        
        // use the specified element in the binary lookup index location specified list 
        int index = Collections.binarySearch (L1,. 19 ); 
        the System.out. the println (index); 
    } 

}

 

Guess you like

Origin www.cnblogs.com/noperx/p/11372546.html