Saturday, 19 April 2014

TreeSet : addAll : Add Collection of data to TreeSet

public boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set.

import java.util.*;
class TreeSetAddAll{
 public static void main(String args[]){
  TreeSet<Integer> mySet1 = new TreeSet<> ();
  TreeSet<Number> mySet2 = new TreeSet<> ();
  
  /* Add data to mySet1 */
  for(int i=0; i<10; i++)
   mySet1.add(i);
   
  System.out.println("Data in mySet1 is");
  System.out.println(mySet1);
  
  System.out.println("\nData in mySet2 is");
  System.out.println(mySet2);
  
  System.out.println("\nAbout to add mySet1 data to mySet2");
  mySet2.addAll(mySet1);
  
  System.out.println("\nData in mySet2 is");
  System.out.println(mySet2); 
 }
}

Output
Data in mySet1 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data in mySet2 is
[]

About to add mySet1 data to mySet2

Data in mySet2 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1. throws ClassCastException if the elements provided cannot be compared with the elements currently in the set

class A{

}

class B extends A{
 int id;
 B(int id){
  this.id = id;
 }
 
 public String toString(){
  return "Object B : " + id;
 }
}

import java.util.*;
class TreeSetAddAllClassCast{
 public static void main(String args[]){
  TreeSet<A> mySet = new TreeSet<> ();
  Collection<B> myList = new ArrayList<> ();
  
  myList.add(new B(1));
  myList.add(new B(2));
  
  System.out.println("Elements in myList is ");
  System.out.println(myList);
  
  System.out.println("About to add myList to mySet");
  mySet.addAll(myList);
 }
}

Output
Elements in myList is
[Object B : 1, Object B : 2]
About to add myList to mySet
Exception in thread "main" java.lang.ClassCastException: B cannot be cast to jav
a.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.TreeSet.add(TreeSet.java:255)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:334)
        at java.util.TreeSet.addAll(TreeSet.java:312)
        at TreeSetAddAllClassCast.main(TreeSetAddAllClassCast.java:14)

2. throws NullPointerException if the specified collection is null or if any element is null and this set uses natural ordering, or its comparator does not permit null elements

import java.util.*;
class TreeSetAddAllNullPointer{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  Collection<Integer> myList = new ArrayList<> ();
  
  myList.add(null);
  
  System.out.println("Elements in myList is ");
  System.out.println(myList);
  
  System.out.println("About to add myList to mySet");
  mySet.addAll(myList);
 }
}

Output
Elements in myList is
[null]
About to add myList to mySet
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.TreeSet.add(TreeSet.java:255)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:334)
        at java.util.TreeSet.addAll(TreeSet.java:312)
        at TreeSetAddAllNullPointer.main(TreeSetAddAllNullPointer.java:13)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment