Working With Collection Class in Java


Introduction

In this article we are going to describe the collection class and its commonly used methods in Java. The collection is frame in Java. And the collection class found with in util package. Actually the Java collections framework is a set of classes and interfaces that are used to implement reusable collection data structures.

What is Collection Framework
collectio.JPG

The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation. The collection framework provides better performance without complexity. It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse. The framework is based on nine collection interfaces. It includes implementations of these interfaces, and algorithms to manipulate them.     

The Collection framework mainly consists of the following:

  • interfaces, the abstract data types that the framework supports.
  • implementations, the concrete versions of these interfaces.
  • algorithms, the predefined actions that can be defined on either the interfaces or their implementations.
  • All implementations are unsynchronized.
  • All implementations are serializable and cloneable.
  • All implementations support having null elements.

Methods Explanation

  • asList()
    The asList method of the Arrays class returns a list and it takes an array as argument and the split method returns an array by breaking the string at spaces.

  • copy(list, sublist);
    The copy method of the collection class creates a copy of a defined list and prints objects of a copy list.

  • max(list) And min(list)
    The max or min methods of the collection class are useful for finding the max or min elements.

  • indexOfSubList(list,searchList)
    The indexOfSubList method returns an integer as index number where the SearchList is a match.

  • lastIndexOfSubList(list, searchList)
    This method is used to find and display indexes of the last occurance of a sublist in the list.

  • replaceAll(list, "Anoop", "god")
    The replaceAll method replace all manendra by god in whole list.

  • reverse(list)
    The reverse method is used for making a reverse of any list.

  • rotate(list, 3)
    This method is used to rotate the list and where the second argument rotates the given number of objects in the list.

  • size()
    This method tells the size of the list.

  • swap()
    The swap method is used to swap the elements at the specified positions in the specified list.

  • fill(List<? super T> list, T obj)
    The fill method is used to replace all of the elements of the specified list with the specified element.

Complete code
 

import java.util.*;

import java.util.Collections;

public class Collection

{

public static void main(String[] args)

{

List list = Arrays.asList("Abhishek Amit Alok Anrudha omji".split(" "));

List sublist = Arrays.asList("Arun");

List searchList = Arrays.asList("Alok");

System.out.println("Toatal Elements in list : " + list);

Collections.copy(list, sublist);

System.out.println("copied list : " + list);

System.out.println("max value in list: " + Collections.max(list));

System.out.println("min value in list : " + Collections.min(list));

System.out.println("First Occurence of 'Alok: " + Collections.indexOfSubList(list,searchList));

System.out.println("Last Occurence of Alok: " +

Collections.lastIndexOfSubList(list, searchList));

Collections.replaceAll(list, "Alok", "God");

System.out.println("After replace Alok by God :" + list);

Collections.reverse(list);

System.out.println("Reverse order of List " + list);

Collections.rotate(list, 3);

System.out.println("Rotated by 3: " + list);

System.out.println("Size of the list : " + list.size());

Collections.swap(list, 0, list.size() - 1);

System.out.println("After swapping List is: " + list);

Collections.fill(list, "Mother");

System.out.println("After filling all 'Mother' in list : " + list);

List abhiList = Collections.nCopies(3, "Abhishek");

System.out.println("List created by ncopy() " + abhiList);

Enumeration e = Collections.enumeration(abhiList);

Vector v = new Vector();

while (e.hasMoreElements())

{

v.addElement(e.nextElement()); 

}

ArrayList arrayList = Collections.list(v.elements());

System.out.println("arrayList: " + arrayList);

}

}

 

Output

cmd.jpg


Resources

Interfaces of collection in JAVA
Introduction to Jdbc

How to use FileWriter and FileReaderClass in JAVA

Abstract Class in JAVA

Standalone JAVA Class using Apache HTTP Client

Up Next
    Ebook Download
    View all
    Learn
    View all