The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : Collections (CheckedList)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : Collections (CheckedList) Posted: Jul 8, 2015 9:10 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : Collections (CheckedList)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=xe3VJT4rc7M&list=UUhwKlOVR041tngjerWxVccw

CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
Method:

public static <E> List<E> checkedList(List<E> list, Class<E> type)

Parameters:

list - the list for which a dynamically typesafe view is to be returned
type - the type of element that list is permitted to hold

Returns:

a dynamically typesafe view of the specified list.
*/


public class CollectionsExample
{

public static void main(String[] args)
{

List myList = new ArrayList();
myList.add("one");
myList.add("two");
myList.add("three");
myList.add("four");

System.out.println("myList : " + myList + "\n");

/*
* Returns a dynamically typesafe view of the specified list.
*/

List chkList = Collections.checkedList(myList, String.class);

System.out.println("Checked list : " + chkList + "\n");

/*
* you can add any type of elements to myList object.
*/

myList.add(10);

System.out.println("myList : " + myList + "\n");

/*
* you cannot add any type of elements to chkList object, doing so
* throws ClassCastException.
*/


chkList.add(10);

System.out.println("Checked list : " + chkList + "\n");
}
}
Output
myList : [one, two, three, four]

Checked list : [one, two, three, four]

myList : [one, two, three, four, 10]

Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type class java.lang.String
at java.util.Collections$CheckedCollection.typeCheck(Collections.java:3037)
at java.util.Collections$CheckedCollection.add(Collections.java:3080)
at CollectionsExample.main(CollectionsExample.java:53)
Click the below Image to Enlarge

Java : Collection Framework : Collections (CheckedList) 
To Download CollectionsDemoCheckedList Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoCheckedList.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: Java : Collection Framework : Collections (CheckedList)

    Topic: R: Wimbledon – How do the seeds get on? Previous Topic   Next Topic Topic: Scaling To Infinity with Docker Swarm, Docker Compose and Consul (Part 4/4) – Scaling Individual...

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use