Click here to watch in Youtube :
https://www.youtube.com/watch?v=xD85MBP1aPE&list=UUhwKlOVR041tngjerWxVccw
TreeSetExample.java
Output
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); } }
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)
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoNull.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment