Home » Java programming language

Java Collections checkedCollection() Method with Example

Collections Class checkedCollection() method: Here, we are going to learn about the checkedCollection() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on January 07, 2020

Collections Class checkedCollection() method

  • checkedCollection() Method is available in java.lang package.
  • checkedCollection() Method is used to return the typesafe view of the given collection at runtime.
  • checkedCollection() Method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • checkedCollection() Method does not throw an exception at the time of returning Collection.

Syntax:

    public static Collection checkedCollection(Collection co, Class ele_ty);

Parameter(s):

  • Collection co – represent the collection for which to get a typesafe view at runtime.
  • Class ele_ty – represent the element type that given collection is allowed to store.

Return value:

The return type of the method is Collection, it returns typesafe view of the given collection dynamically.

Example:

// Java Program is to demonstrate the example
// of Collection checkedCollection(Collection co, Class ele_ty) of 
// Collections class

import java.util.*;

public class CheckedCollection {
    public static void main(String args[]) {
        // Create a linked list object    
        LinkedList < Integer > link_list = new LinkedList < Integer > ();

        // By using add() method is to add the
        // given elements in linked list
        link_list.add(20);
        link_list.add(10);
        link_list.add(30);
        link_list.add(40);
        link_list.add(50);

        // Display LinkedList
        System.out.println("link_list: " + link_list);

        // By using checkedCollection() method is to 
        // represent the type safe view of the given Collection

        Collection < Integer > co = Collections.checkedCollection(link_list, Integer.class);
        System.out.println();
        System.out.println("Collections.checkedCollection(link_list, Integer.class) :");

        // Display collection
        System.out.println("co: " + co);
    }
}

Output

link_list: [20, 10, 30, 40, 50]

Collections.checkedCollection(link_list, Integer.class) :
co: [20, 10, 30, 40, 50]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.