Tuesday, 1 April 2014

retainAll : Retain Elements

boolean retainAll(Collection<?> c)
    Retains only the elements in this list that are contained in the specified collection. Return true if this list changed as a result of the call.


import java.util.*;
class ListRetainAll{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet1 =new HashSet<> ();
  Set<Integer> mySet2 =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet1 */
  for(int i=5; i < 15; i++){
   mySet1.add(i);
  }
  
  /* Add Elements to mySet2 */
  for(int i=10; i < 15; i++){
   mySet2.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet1 are ");
  System.out.println(mySet1 +"\n");
  System.out.println("Elements in mySet2 are ");
  System.out.println(mySet2 +"\n");
  
  myList.retainAll(mySet1);
  System.out.println("After retaining mySet1 from myList");
  System.out.println(myList +"\n");
  
  myList.retainAll(mySet2);
  System.out.println("After retaining mySet2 from myList");
  System.out.println(myList +"\n");
  
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet1 are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in mySet2 are
[10, 11, 12, 13, 14]

After retaining mySet1 from myList
[5, 6, 7, 8, 9]

After retaining mySet2 from myList
[]

1. throws UnsupportedOperationException if the retainAll operation is not supported by this list

import java.util.*;
class ListRetainAllUnsupported{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  Set<Integer> mySet1 =new HashSet<> ();

  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet1 */
  for(int i=5; i < 15; i++){
   mySet1.add(i);
  }
  
  unmodifiable = Collections.unmodifiableList(myList);
  
  System.out.println("Elements in unmodifiable List are ");
  System.out.println(unmodifiable +"\n");
  System.out.println("Elements in mySet1 are ");
  System.out.println(mySet1 +"\n");
  
  unmodifiable.retainAll(mySet1);
  
 }
}

Since Java don't permit to add any data to unmodifiable lists, Below error thrown while running the program.
Output
Elements in unmodifiable List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet1 are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.retainAll(Unknown Source)
        at ListRetainAllUnsupported.main(ListRetainAllUnsupported.java:26)


2. throws ClassCastException if the class of an element of this list is incompatible with the specified collection

3. throws NullPointerException if this list contains a null element and the specified collection does not permit null elements or if the specified collection is null.











Prevoius                                                 Next                                                 Home

removeAll : Remove collection of elements

boolean removeAll(Collection<?> c)
    Removes from this list all of its elements that are contained in the specified collection (optional operation).

import java.util.*;
class ListRemoveAll{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Remove mySet Data from myList */
  myList.removeAll(mySet);
  
  System.out.println("Elements in myList after removing mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after removing mySet are
[0, 1, 2, 3, 4]

1. throws UnsupportedOperationException if the removeAll operation
is not supported by this list.
import java.util.*;
class ListRemoveAllUnsupport{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  unmodifiable = Collections.unmodifiableList(myList);
  
  System.out.println("Elements in unmodifiable list are ");
  System.out.println(unmodifiable +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Remove mySet Data from unmodifiable list */
  unmodifiable.removeAll(mySet);
  
  System.out.println("Elements in unmodifiable after removing mySet are");
  System.out.println(unmodifiable); 
 }
}

Output
Elements in unmodifiable list are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.removeAll(Unknown Source)
        at ListRemoveAllUnsupport.main(ListRemoveAllUnsupport.java:26)


2. throws ClassCastException if the class of an element of this list is incompatible with the specified collection

3. throws NullPointerException if this list contains a null element and the specified collection does not permit null elements or if the specified collection is null



Prevoius                                                 Next                                                 Home

addAll : Add Collection of Data to List from specific index

boolean addAll(int index, Collection<? extends E> c)
   Inserts all of the elements in the specified collection into this list at the specified position. Shifts the element currently at that position and any subsequent elements to the right.

import java.util.*;
class ListAddAllFromIndex{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Add mySet Date to myList */
  myList.addAll(1, mySet);
  
  System.out.println("Elements in myList after adding mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after adding mySet are
[0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9]


1. throws UnsupportedOperationException if the <tt>addAll</tt> operation is not supported by this list.

import java.util.*;
class ListAddAllFromUnsupported{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unModifiable;
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  unModifiable = Collections.unmodifiableList(myList);
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in unModifiable List are ");
  System.out.println(unModifiable +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Add mySet Data to unModifiable List */
  unModifiable.addAll(1, mySet);
  
  System.out.println("Elements in myList after adding mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in unModifiable List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableList.addAll(Unknown Source)
        at ListAddAllFromUnsupported.main(ListAddAllFromUnsupported.java:26)


2. throws ClassCastException if the class of an element of the specified collection prevents it from being added to this list

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

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

5. throws IndexOutOfBoundsException if the index is out of range

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

Output
Elements in myList List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size:10
        at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
        at java.util.ArrayList.addAll(Unknown Source)
        at ListAddAllIndexBound.main(ListAddAllIndexBound.java:23)






Prevoius                                                 Next                                                 Home

addAll : Add Collection of Elements

boolean addAll(Collection<? extends E> c)
    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

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

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after adding mySet are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

1. throws UnsupportedOperationException if the addAll operation is not supported by this list
import java.util.*;
class ListAddAllUnsupported{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unModifiable;
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  unModifiable = Collections.unmodifiableList(myList);
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in unModifiable List are ");
  System.out.println(unModifiable +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Add mySet Data to unModifiable List */
  unModifiable.addAll(mySet);
  
  System.out.println("Elements in myList after adding mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in unModifiable List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.addAll(Unknown Source)
        at ListAddAllUnsupported.main(ListAddAllUnsupported.java:26)


2. throws ClassCastException if the class of an element of the specified
Collection prevents it from being added to this list

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

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




Prevoius                                                 Next                                                 Home

containsAll : Search for a Collection of elements

boolean containsAll(Collection<?> c)
    Returns true if this list contains all of the elements of the specified collection.

import java.util.*;
class ListContainsAll{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> myList1 = new ArrayList<> ();
  List<Integer> myList2 = new ArrayList<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to myList1 */
  for(int i=5; i < 10; i++){
   myList1.add(i);
  }
  
  /* Add Elements to myList2 */
  for(int i=5; i < 12; i++){
   myList2.add(i);
  }
 
  
  System.out.println("Elements in the myList are");
  System.out.println(myList+"\n");
  
  System.out.println("Elements in the myList1 are");
  System.out.println(myList1+"\n");
  
  System.out.println("Elements in the myList2 are");
  System.out.println(myList2+"\n");
  
  System.out.print("Is myList contains all the elements of myList1 ");
  System.out.println(myList.containsAll(myList1));
  
  System.out.print("Is myList contains all the elements of myList2 ");
  System.out.println(myList.containsAll(myList2));
 }
}

Output
Elements in the myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in the myList1 are
[5, 6, 7, 8, 9]

Elements in the myList2 are
[5, 6, 7, 8, 9, 10, 11]

Is myList contains all the elements of myList1 true
Is myList contains all the elements of myList2 false

1. throws ClassCastException if the types of one or more elements in the specified collection are incompatible with this list

2. throws NullPointerException if the specified collection contains one
or more null elements and this list does not permit null elements



Prevoius                                                 Next                                                 Home