Collections

Previous Home Next

 



 


 
package r4r.co.in;
import java.util.*;


public class CollectionExample {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub


ArrayList<Integer> al=new ArrayList<Integer>(); // creating an ArrayList of Integer elements
al.add(56); // adding elements to the arraylist
al.add(72);
al.add(97);
System.out.println("The contents of the ArrayList are: "+al);

HashSet<Integer> set=new HashSet<Integer>(); // creating a HashSet named set
set.add(1); // adding elements to the HashSet
set.add(3);
set.add(4);
set.add(5);
set.add(7);
set.add(1);
set.add(4);
set.add(7);
System.out.println("The contents of the set are:"); //displaying the contents of the HashSet
System.out.println(set);

set.addAll(al); // Adding the ArrayList to the HashSet set
System.out.println("The contents of set after adding Arraylist is: "+set); // displaying the resultant collection HashSet

}


}
The output of the above program is as follows:

The contents of the ArrayList are: [56, 72, 97]
The contents of the set are:
[1, 3, 4, 5, 7]
The contents of set after adding Arraylist is: [1, 3, 4, 5, 97, 7, 56, 72]
Previous Home Next