Collections Class in Java

In Java, the Collections framework simplifies working with a list of objects. At the core of this is the Collections class, a utility class available in Java.util package that offers plenty of methods to perform various operations of collections.

Collections Class Key Methods

The collections class defines several methods of collection objects for searching, sorting, etc.

sort(List l)

public static <T extends Comparable<? super T>> void sort(     @NotNull  java.util.List<T> list )

This method is used to sort based on the default natural sorting order. In this case, it is compulsory that list should contain homogenous and comparible objects only, otherwise we will get RE: ClassCastException

import java.util.*;
import java.util.ArrayList;

public interface CollectionJava {
    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("The");
        list.add("Code");
        list.add("Data");
        list.add("Java");
        System.out.println("Before sorting "+list);
        Collections.sort(list);
        System.out.println("After sorting "+list);

    }
}
sorting a list

sort(List l, Comparator c)

@Contract(mutates = "param1")  
public static <T> void sort(     @NotNull  java.util.List<T> list,
    @Nullable  java.util.Comparator<? super T> c ) 

This method is used to sort based on customized requirements

import java.util.*;
import java.util.ArrayList;

public interface CollectionJava {
    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("The");
        list.add("Code");
        list.add("Data");
        list.add("Java");
        System.out.println("Before sorting "+list);
        Collections.sort(list, new MyComparator());
        System.out.println("After sorting "+list);
    }
}

class MyComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        String s1 = o1.toString();
        String s2 = o2.toString();
        return s2.compareTo(s1);
    }
}
sort(List l, Comparator c)

reverse()

public static void reverse(     @NotNull  java.util.List<?> list )

This method is used to reverse the order of a given list.

reverse() Example

import java.util.*;
import java.util.ArrayList;

public interface CollectionJava {
    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("The");
        list.add("Code");
        list.add("Data");
        list.add("Java");
        System.out.println("Before reverse "+list);
        Collections.reverse(list);
        System.out.println("After reverse "+list);
    }
}
reverse method

shuffle()

public static void shuffle(     @NotNull  java.util.List<?> list )

binarySearch()

public static <T> int binarySearch(     @NotNull  List<? extends Comparable<? super T>> list,
 T key )

addAll()

public static <T> boolean addAll(     @NotNull  java.util.Collection<? super T> c,
 @NotNull  T... elements )

Similar Java Tutorials

Leave a Comment