Talking about the collection framework five - the extension of the collection framework: the use of the Collections tool class, the custom comparator

I just finished learning the collection framework recently, and I want to organize some of my study notes and ideas, so there may be some inaccurate or incorrect content in this blog, please point out. Beginners only recommend this blog as a reference, welcome to leave a message to learn together.

  I have introduced the system concept of the collection framework before ( http://www.cnblogs.com/yjboke/p/8761195.html ), this article introduces the collection extension content: the use of the Collections tool class; variable parameters; collection array mutual conversion method;


 

The difference between Collection and Collections:

Collection is an interface under java.util, which is the parent interface of List, Set and other interfaces.

Collections is a helper class for collection classes. It provides a series of static methods to implement operations such as searching, sorting, and synchronizing collections. Next, the basic usage will be briefly introduced:

1. Search and replace (mainly for Collection related interfaces):

  max(Collection c); returns the maximum value;

  min(Collection c); returns the minimum value;

  fill(List list, Object obj); replace all elements in the list collection with the specified element (obj);

  frequency(Collection Object o); returns the number of occurrences of the specified object in the specified collection;

  replaceAll(list, Object obj, Object newObj); replace the specified element with a new element;

  copy(List list1, List list2); Copies all the elements in the collection List2 into list1 and overwrites the elements with their corresponding indices.

  binarySearch (List list; Object obj); to find the subscript according to the element, you need to use sort to sort first, if there is no such element, it will return a negative value);

2. Sorting (mainly for List related interfaces):

  sort(List list); sort the elements in the list in natural ascending order;

  reverse(List list); reverse sort;

  shuffle(List list); Randomly sort the elements in the collection;

  swap(List list, int i1, int i2); swap the positions of the two subscripts;

  rotate(List list, Int distance); Shift all elements to the right by a specified length, if distance is equal to size, it will remain unchanged.

3. Synchronization

  synchronizedCollection returns the synchronized collection supported by the specified Collection;

  Can also be applied to Set, Map, List.

4. Make the collection immutable

  emptyList(); returns an empty immutable collection object;

  singletonList(); returns an immutable collection object containing only the specified object;

  unmodiflableList(); returns an immutable view of the specified collection object;

5. Comparator

Comparator; Implement the Comparator interface, override the compare method, and customize the comparator.

-------------------------------------------------------------

The following will pick a few methods for code examples:

First the code for the normal sort or replace method:

public static void main(String[] args) {
        sortDemo ();
        StrSortDemo ();
    }
    
    public  static  void sortDemo() {
         // Define the collection and add elements 
        List<String> list = new ArrayList<String> ();
        list.add("abc");
        list.add("aaa");
        list.add("bcde");
        list.add( "bc" );
         // The original order output    
        System.out.println(list);         // The output result is: [abc, aaa, bcde, bc]
        
        // Use the sort method to sort 
        Collections.sort(list);
         // Output the sorted collection    
        System.out.println(list);         // The output result is: [aaa, abc, bc, bcde]
        
        // Find the subscript according to the element "bcde" 
        int i = Collections.binarySearch(list, "bcde" );
        System.out.println(i);             // The output result is: 3
        
        // Reverse sorting 
        Collections.reverse(list);
        System.out.println(list);         // The output result is: [bcde, bc, abc, aaa]
        
        // Swap subscript positions 
        Collections.swap(list, 2, 3 );
        System.out.println(list);         // The output result is: [bcde, bc, aaa, abc]
        
        // Output the maximum value 
        String max = Collections.max(list);
        System.out.println(max);             // The output is: bcde
        
        // Replace all elements in the collection with over 
        Collections.fill(list, "over" );    
        System.out.println(list);         // The output result is: [over, over, over, over] 
        
    }

  Next is to override the comparator method to compare the length of the elements:

class StrComparator implements Comparator<String>{
    
    public int compare(String s1, String s2) {
        
        if(s1.length() > s2.length()) {
            return 1;
        }
        if (s1.length() == s2.length()) {
             // If the strings have the same length, compare the contents 
            return s1.compareTo(s2);
        }
        return -1;
    }
    
}

  The sorting method after adding the length comparator:

    public  static  void StrSortDemo() {
         // Define the collection and add elements 
        List<String> list = new ArrayList<String> ();
        list.add("abc");
        list.add("aaaaa");
        list.add("bcde");
        list.add("bc");
        
        // Sort by string length 
        Collections.sort(list, new StrComparator());
        System.out.println(list);         // The output result is: [bc, abc, bcde, aaaaa] 
        
        String max = Collections.max(list, new StrComparator());
        System.out.println(max);             // The output result is: aaaaa 
    }

 

above.

PS: If you are not rigorous, please bring it up and learn together.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734497&siteId=291194637