Friday 9 January 2015

Java : Collection Framework : TreeSet (null)


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

TreeSetExample.java
import java.util.TreeSet;

/*
 * TreeSet doesn't allow null and throw java.lang.NullPointerException
 * when you will insert null into TreeSet. 
 * Since TreeSet uses compareTo() method of respective elements to compare them  
 * which throws NullPointerException while comparing with null.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Ram");
        treeSet.add(null);
        
        System.out.println("\ntreeSet : " + treeSet);

    }
}
Output
Exception in thread "main" java.lang.NullPointerException
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at TreeSetExample.main(TreeSetExample.java:17)
To Download TreeSetDemoNull Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoNull.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment