Tuesday 26 May 2015

How to use retailAll in vector

Retains only the elements in vector that are contained in the specified collection. In other words, removes from vector all of its elements that are not contained in the specified collection.


import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class RetainAll {

    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
           
        List<String> list = new ArrayList<String>();
      
        list.add("hockey");
        list.add("php");
        list.add("java");
      
        /*
         * Retains only the elements in this
         *  Vector that are contained in the
         *   specified Collection.
         */
        vector.retainAll(list);
      
        System.out.println("After retainAll vector is = "+vector);
      
    }
   
   
}

Output:

Vector = [cricket, hockey, football, tennish]

After retainAll vector is = [hockey]
 

No comments:

Post a Comment