Collections class common method, and the difference between the interface and Comparator Collection Interface

1, Collections is a class;

public class Collections
extends Object

Such a collection of static methods, or a combination of return only . It contains polymorphic algorithms that operate on collections, "wrappers", returning a new collection by the specified collection support, as well as a number of other possible and the final.

2, commonly used methods: You can view API

 

 

CollectionDemo Package; 

Import of java.util.ArrayList; 
Import Classes in java.util *;. 

public class CollectionsDemo1 { 
    public static void main (String [] args) { 
        List <String> = new new arryList the ArrayList <> (); 
        arryList.add ( " Chenhao "); 
        arryList.add (" yangyang "); 
        arryList.add (" Yangzi "); 
        arryList.add (" Zhang "); 
        System.out.println (" before the operation: "); 
        for (String str: arryList ) { 
            of System.out.print (STR + ""); 
        } 
        System.out.println (); 
        // exchange order: switching the specified position specified in the list of elements. 
        System.out.println ( "after switching the order:"); 
        the Collections.
            Of System.out.print (STR + ""); 
        } 
        System.out.println (); 

        // Sort: sort the list according to a specified natural ordering of its elements 
        System.out.println ( "natural ordering after:") ; 
        Collections.sort (arryList); 
        for (String str: arryList) { 
            System.out.print (str + ""); 
        } 
        System.out.println (); 


        // a binary search, you can use after the natural ordering half Find method: using a binary search algorithm to search a specified list of the specified object. 
        System.out.println ( "binary search:"); 
        System.out.println (Collections.binarySearch (arryList, "Zhang")); 

        // scrambled; default random source arranged randomly specified list 
        System. out.println ( "disrupt the order:");
        for (String S: arryList) { 
        } 
        System.out.println (); 


        // reverse the sort, which element will be reversed; reversing the order specified in the list of elements 
        System.out.println ( "after reverse the sort:" ); 
        Collections.reverse (arryList); 
        for (String STR: arryList) { 
            of System.out.print (STR + ""); 
        } 
        System.out.println (); 

        // filled; instead of the specified list with the specified elements All elements 
        System.out.println ( "fill:"); 
        Collections.fill (arryList, "yangppp"); 
        for (String S: arryList) { 
            of System.out.print (S + ""); 
        } 
        the System.out. the println (); 


    } 


}

2, Collection is an interface;

 

       Collection hierarchy root interface . It represents a group set which is called the element object. Some collections allow duplicate elements, and other collections is not allowed. Some of the commands and other disorder. JDK does not provide any of this interface directly realized: it provides a more specific implementation of sub-interfaces, such as Set and List . This interface is typically set for delivery, and operates in the case where the required maximum versatility.  

compact1, compact2, compact3

java.util

Interface Collection<E>

  • Parameter Type

    E - this type of elements in a set

3, Comparator is an interface;

compact1, compact2, compact3

java.util

Interface Comparator<T>

  • Parameter Type

    T - Compares this object type may comparator

  • Compare function, a collection of objects imposes a total ordering . Comparator may be passed to the sorting method (e.g., Collections.sortor Arrays.sort), so precise control of the sort order. The comparator can also be used to control certain data structures (e.g., sequential sorted setsor sorted maps), or for the collection does not have to sort objects provide natural ordering.

    method:

   

int compare(T o1, T o2)

Comparison of the order of two parameters.

 

 

Published 45 original articles · won praise 8 · views 5842

Guess you like

Origin blog.csdn.net/wenyunick/article/details/104341155