Thursday 20 March 2014

Add a Collection to Set

boolean addAll(Collection<? extends E> c)
    Adds all of the elements in the specified collection to this set if they're not already present. Returns true if this set changed as a result of the call.

import java.util.*;
class SetAddAll{
 public static void main(String args[]){
  Set<Integer> mySet = new HashSet<> ();
  List<Integer> myList = new ArrayList<> (); 
  
  /* Add Data to mySet */
  for(int i=0; i<10; i++)
   mySet.add(i);
   
  /* Add Data to myList */
  for(int i=5; i < 15; i++)
   myList.add(i);
   
  System.out.println("Data in mySet is\n" + mySet);
  System.out.println("\nData in myList is\n" + myList);
  
  /* Add Collection myList to mySet */
  mySet.addAll(myList);
  
  System.out.println("\nAfter adding myList to mySet Data in mySet is ");
  System.out.println(mySet);
 }
}
Output

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

Data in myList is
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

After adding myList to mySet Data in mySet is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

1. addAll() throws UnsupportedOperationException if the addAll operation is not supported by this set.

import java.util.*;
class SetAddAllUnsuport{
 public static void main(String args[]){
  Set<Integer> mySet = new HashSet<> ();
  List<Integer> myList = new ArrayList<> ();
  Set<Integer> unModifiableSet; 
  
  /* Add Data to mySet */
  for(int i=0; i<10; i++)
   mySet.add(i);
   
  /* Add Data to myList */
  for(int i=5; i < 15; i++)
   myList.add(i);
   
  System.out.println("Data in mySet is\n" + mySet);
  System.out.println("\nData in myList is\n" + myList);
  
  unModifiableSet = Collections.unmodifiableSet(mySet);
  
  System.out.println("\nData in unModifiableSet is\n" + unModifiableSet);
  
  /* Tring to add myList to unModifiableSet */
  unModifiableSet.addAll(myList);

 }
}
Output

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

Data in myList is
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Data in unModifiableSet is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.addAll(Unknown Source)
        at SetAddAllUnsuport.main(SetAddAllUnsuport.java:24)

You can't add data to unmodifiable Set. So Above program throws UnsupportedOperationException.

2. addAll() throws ClassCastException if the class of an element of the specified collection prevents it from being added to this set.

class Employee{
 int number;

 Employee(int number){
  this.number = number;
 } 
 
 public String toString(){
  return "Employee " + number;
 }
 
}

import java.util.*;
class SetAddAllClassCast{
 public static void main(String args[]){
  Set<Employee> mySet = new TreeSet<> ();
  List<Employee> myList = new ArrayList<> ();
  
  /* Add Data to myList */
  myList.add(new Employee(2));
   
  System.out.println("Data in mySet is\n" + mySet);
  System.out.println("\nData in myList is\n" + myList);
  
  /* Tring to add myList to mySet */
  mySet.addAll(myList);
  
  System.out.println(mySet);

 }
}

Output

Data in mySet is
[]

Data in myList is
[Employee 2]
Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at java.util.AbstractCollection.addAll(Unknown Source)
        at java.util.TreeSet.addAll(Unknown Source)
        at SetAddAllClassCast.main(SetAddAllClassCast.java:14)

To make the Program run Employee Class must implement the Comaparable interface.

3. addAll throws NullPointerException if the specified collection contains one or more null elements and this set does not permit null elements, or if the specified collection is null.

import java.util.*;
class SetAddAllNullPointer{
 public static void main(String args[]){
  Set<Integer> myTreeSet = new TreeSet<> ();
  Set<Integer> myHashSet = new HashSet<> ();
  
  /* Add Data to myTreeSet */
  myTreeSet.add(10);
  
  /* Add Data to myHashSet */
  myHashSet.add(null);
   
  System.out.println("Data in myTreeSet is\n" + myTreeSet);
  System.out.println("\nData in myHashSet is\n" + myHashSet);
  
  /* Tring to add myTreeSet to myHashSet */
  myTreeSet.addAll(myHashSet);

 }
}
Output

Data in myTreeSet is
[10]

Data in myHashSet is
[null]

Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at java.util.AbstractCollection.addAll(Unknown Source)
        at java.util.TreeSet.addAll(Unknown Source)
        at SetAddAllNullPointer.main(SetAddAllNullPointer.java:17)

4. throws IllegalArgumentException if some property of an element of the specified collection prevents it from being added to this set.

import java.util.*;
class SetAddAllIllegal{
 public static void main(String args[]){
  SortedSet<Integer> mySet1 = new TreeSet<> ();
  SortedSet<Integer> mySet2 = new TreeSet<> ();
  SortedSet<Integer> subSet1;
  SortedSet<Integer> subSet2;
  SortedSet<Integer> subSet3;
  
  /* Add Elements to mySet */
  for(int i=0; i < 25; i++){
   mySet1.add(i);
   i++;
   mySet2.add(i);
  }
  
  /* Get the subSet1 in the range of 10 to 20 */
  subSet1 = mySet1.subSet(10, 20);
  
  /* Get the subSet2 in the range of 10 to 15 */
  subSet2 = mySet2.subSet(10, 15);
  
  /* Get the subSet2 in the range of 20 to 25 */
  subSet3 = mySet2.subSet(20, 25);
 
   
  /* Display Elements */
  System.out.println("Elements in mySet1 are\n" + mySet1);
  System.out.println("\nElements in mySet2 are\n" + mySet2);
  System.out.println("\nElements in subSet1 are\n" + subSet1);
  System.out.println("\nElements in subSet2 are\n" + subSet2);
  System.out.println("\nElements in subSet3 are\n" + subSet3);
  
  /* Add subSet2 to subSet1 */
  subSet1.addAll(subSet2);
  System.out.println("\nsubset1 After adding subSet2 it \n" + subSet1);
  
  /* Add subSet3 to subSet1 */
  subSet1.addAll(subSet3);
  System.out.println("\nsubset1 After adding subSet3 it \n" + subSet1);
 }
}

Output

Elements in mySet1 are
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]

Elements in mySet2 are
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]

Elements in subSet1 are
[10, 12, 14, 16, 18]

Elements in subSet2 are
[11, 13]

Elements in subSet3 are
[21, 23]

subset1 After adding subSet2 it
[10, 11, 12, 13, 14, 16, 18]
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
        at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at java.util.AbstractCollection.addAll(Unknown Source)
        at java.util.TreeSet.addAll(Unknown Source)
        at SetAddAllIllegal.main(SetAddAllIllegal.java:39)

Below statement causes the IllegalArgumentException.
subSet1.addAll(subSet3)
Since subSet1 in the range of 10 to 20, where as subSet3 in the range of 20 to 25. So while trying to add subSet3, which is not in the range of subSet1, IllegalArgument Exception thrown.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment