Working with java Collections class

In this section we will learn how to wok with Collections class of java.util package.

Working with java Collections class


In this section we will learn how to wok with Collections class of java.util package. Actually java collections framework is a set of classes and interfaces that are used to implement reusable collection data structures.

Code given below shows that how to use various methods of Collections class. For example- add element, copy elements, reverse data structure etc.

import java.util.*;
import java.util.Collections;

public class Collection {
	public static void main(String[] args) {
        List list = Arrays.asList("komal girish mahendra amit 
                    mahendra".split(" "));
        List sublist = Arrays.asList("vineet");
        List searchList = Arrays.asList("mahendra");
        System.out.println("Elements in list : " + list);
        // create a copy of defined list and print objects of copy list.
        Collections.copy(list, sublist);
        System.out.println("copy of list : " + list);
        // find and display maximum and minimum object value from list.
        System.out.println("object of max value : " + Collections.max(list));
        System.out.println("object of min value : " + Collections.min(list));
        // find and display index of first occurance of sublist in the list.
        System.out.println("First index of 'mahendra': " + 
                         Collections.indexOfSubList(list, searchList));
        // find and display index of last occurance of sublist in the list.
        System.out.println("Last index of 'mahendra': " +
                     Collections.lastIndexOfSubList(list, searchList));
        // replace all objects in list by a new given object.
        Collections.replaceAll(list, "mahendra", "replaced");
        System.out.println("After replace all 'mahendra': " + list);
        // list in reverse order.
        System.out.println("List in reverse order: " + list);
        // rotate the given number of objects in list,here 4
        Collections.rotate(list, 4);
        System.out.println("After rotation : " + list);
        // find size of the list
        System.out.println("Size of the list : " + list.size());
        /* Swap element in list. here swap specified element with 
           element at 0th(first) position */
        Collections.swap(list, 0, list.size() - 1);
        System.out.println("List after swapping : " + list);
        // Replace all the element with given element using fill()
        Collections.fill(list, "mahendra");
        System.out.println("After filling all 'mahendra' in list : "
                                 + list);
        /* ncopies() returns immutable list consisting of copies 
             of the specified object. */
        List raviList = Collections.nCopies(3, "Ravi");
        System.out.println("List created by ncopy() " + raviList);

        // getting an enum type of specified list through enumeration().
        Enumeration e = Collections.enumeration(raviList);
        Vector v = new Vector(); // create a vector object.
        while (e.hasMoreElements()) {
            // add elements in vector from enum type.
        ArrayList arrayList = Collections.list(v.elements());
        System.out.println("arrayList: " + arrayList);

Output :

Elements in list : [komal, girish, mahendra, amit, mahendra]
copy of list : [vineet, girish, mahendra, amit, mahendra]
object of max value : vineet
object of min value : amit
First index of 'mahendra': 2
Last index of 'mahendra': 4
After replace all 'mahendra': [vineet, girish, replaced, amit, replaced]
List in reverse order: [replaced, amit, replaced, girish, vineet]
After rotation : [amit, replaced, girish, vineet, replaced]
Size of the list : 5
List after swapping : [replaced, replaced, girish, vineet, amit]
fill all 'mahendra' in list : [mahendra, mahendra, mahendra, mahendra, 
List created by ncopy() [Ravi, Ravi, Ravi]
arrayList: [Ravi, Ravi, Ravi]

Download Source Code