Home » Java programming language

Java Collections checkedList() Method with Example

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

Collections Class checkedList() method

  • checkedList() Method is available in java.lang package.
  • checkedList() Method is used to return the typesafe view of the given List at runtime.
  • checkedList() 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.
  • checkedList() Method does not throw an exception at the time of returning validated List.

Syntax:

    public static List checkedList(List l, Class ele_ty);

Parameter(s):

  • List l – represents the list for which to get typesafe view at runtime.
  • Class ele_ty – represents the element type (ele_ty) that the given List is allowed to store.

Return value:

The return type of the method is List, it returns typesafe or validated view of the given List dynamically.

Example:

// Java Program is to demonstrate the example
// of List checkedList(List l, Class ele_ty) of Collections class

import java.util.*;

public class CheckedList {
    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 checkedList() method is to 
        // represent the type safe view of the given
        // Collection linked list

        Collection < Integer > co = Collections.checkedList(link_list, Integer.class);

        System.out.println();
        System.out.println("Collections.checkedList(link_list, Integer.class) :");

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

Output

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

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.