Tuesday 18 March 2014

Methods in the Collection Interface

Method Description
int size()
return the number of elements in this collection
boolean isEmpty()
return true if the collection contains no elements
boolean contains(Object o)
return true if this collection contains the specified element
Iterator<E> iterator()
Returns an iterator over the elements in this collection
Object[] toArray()
Returns an Array which contains all the elements of the collection.
<T> T[] toArray(T[] a) Returns an array containing all of the elements in this collection. The runtime type of the returned array is that of the specified array
boolean add(E e)
Add an Element to this collection. Returns true, if this collection changed as a result of the call
boolean remove(Object o)
Removes from specified element from the collection. Returns true if the Element removed successfully
boolean containsAll(Collection<?> c)
Return true if this collection contains all of the elements of the specified collection
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection. Return true if this collection changed as a result of the call
boolean removeAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection
boolean retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified. collection. Return true if this collection changed as a result of the call.
void clear()
Remove all the elements from the Collection
boolean equals(Object o)
Compares the specified object with this collection for equality. Return true if the Object “o” equals with the collection
int hashCode()
Return the hash code of the collection

CollectionEx.java
import java.util.*;

public class CollectionEx {
    
    static void display(Collection c){
        Iterator iter1 = c.iterator();
        while(iter1.hasNext()){
            System.out.print(iter1.next() +" ");
        }
    }
    
    static void displayArray(Object arr[]){
        for(Object obj : arr)
            System.out.print(obj +" ");
    }
    
    public static void main(String args[]){
        Collection<Integer> coll1 = new ArrayList<>();
        Collection<Integer> coll2 = new ArrayList<>();
        Collection<Integer> coll3 = new ArrayList<>();
        
        boolean flag;
        int noElements;
        Object arr[];
        Integer intArr[];
        
        flag = coll1.isEmpty();
        noElements = coll1.size();
        
        System.out.println("Is Collection Empty " + flag);
        System.out.println("Number Of Elements in the Collection " + noElements);
        
        /*Adding Elements to the collection */
        coll1.add(1);
        coll1.add(2);
        coll1.add(3);
        coll1.add(4);
        coll1.add(5);
        coll1.add(6);
        
        System.out.println("\nHash Code for coll1 is " +coll1.hashCode());
        
        flag = coll1.isEmpty();
        noElements = coll1.size();
        
        System.out.println("\nIs Collection Empty " + flag);
        System.out.println("Number Of Elements in the Collection " + noElements);
        System.out.println("\nElements in the Collection coll1 are");
        display(coll1);
        
        /* Copying all the Elements to the Array Of type Object */
        arr = coll1.toArray();
        System.out.println("\n\nArray arr Contains all the Elements of Collections");
        displayArray(arr);
        
         /* Copying all the Elements to the Array Of type Integer */
        System.out.println("\n\nArray intArr Contains all the Elements of Collections");
        noElements = coll1.size();
        intArr = new Integer[noElements];
        coll1.toArray(intArr);
        displayArray(intArr);
        
        /* Removing 4 from collection */
        coll1.remove(4);
        System.out.println("\n\nAfter Removing Element 4 from the collection");
        display(coll1);
        
        /* Adding Elements to the Collection coll2 */
        coll2.add(2);
        coll2.add(3);
        
         /* Adding Elements to the Collection coll3 */
        coll3.add(10);
        coll3.add(15);
        
        System.out.println("\n\nElements in the Collection coll1 are");
        display(coll1);
        System.out.println("\n\nElements in the Collection coll2 are");
        display(coll2);
        System.out.println("\n\nElements in the Collection coll3 are");
        display(coll3);
        
        flag = coll1.containsAll(coll2);
        System.out.println("\n\nIs collection coll1 contains coll2 " + flag);
        flag = coll1.containsAll(coll3);
        System.out.println("Is collection coll1 contains coll3 " + flag);
        
        /*Add Collection coll3 to coll1*/
        System.out.println("\nBefore adding coll1 has");
        display(coll1);
        coll1.addAll(coll3);
        System.out.println("\nAfter adding coll1 has");
        display(coll1);
        
        /* Removing the Elemens from the coll1 */
        System.out.println("\n\nBefore Removing coll2 from coll1 ");
        display(coll1);
        coll1.removeAll(coll2);
        System.out.println("\nAfter removing coll2 from coll1 ");
        display(coll1);
        
        /* Retaing element of coll3 from coll1 */
        System.out.println("\n\nBefore Retaining coll3 from coll1 ");
        display(coll1);
        coll1.retainAll(coll3);
        System.out.println("\nAfter Retaining coll3 from coll1 ");
        display(coll1);
        
        /* Checks for Equality of Collections */
        flag = coll1.equals(coll2);
        System.out.println("\n\nIs coll1 and coll2 are Equal " + flag);
        flag = coll1.equals(coll3);
        System.out.println("Is coll1 and coll3 are Equal " + flag);
        
        /* Clearing all the Collections */
        System.out.println("\n\nBefore Clearing");
        System.out.print("coll1: ");
        display(coll1);
        System.out.println();
        System.out.print("coll2: ");
        display(coll2);
        System.out.println();
        System.out.print("coll3: ");
        display(coll3);
        
        coll1.clear();
        coll2.clear();
        coll3.clear();
        System.out.println("\n\nAfter Clearing");
        System.out.print("coll1: ");
        display(coll1);
        System.out.println();
        System.out.print("coll2: ");
        display(coll2);
        System.out.println();
        System.out.print("coll3: ");
        display(coll3);
        
        System.out.println("\n\nHash Code for coll1 After clearing " +coll1.hashCode());

    }
}

OUTPUT:

Is Collection Empty true
Number Of Elements in the Collection 0

Hash Code for coll1 is 918073252

Is Collection Empty false
Number Of Elements in the Collection 6

Elements in the Collection coll1 are
1 2 3 4 5 6

Array arr Contains all the Elements of Collections
1 2 3 4 5 6

Array intArr Contains all the Elements of Collections
1 2 3 4 5 6

After Removing Element 4 from the collection
1 2 3 5 6

Elements in the Collection coll1 are
1 2 3 5 6

Elements in the Collection coll2 are
2 3

Elements in the Collection coll3 are
10 15

Is collection coll1 contains coll2 true
Is collection coll1 contains coll3 false

Before adding coll1 has
1 2 3 5 6
After adding coll1 has
1 2 3 5 6 10 15

Before Removing coll2 from coll1
1 2 3 5 6 10 15
After removing coll2 from coll1
1 5 6 10 15

Before Retaining coll3 from coll1
1 5 6 10 15
After Retaining coll3 from coll1
10 15

Is coll1 and coll2 are Equal false
Is coll1 and coll3 are Equal true


Before Clearing
coll1: 10 15
coll2: 2 3
coll3: 10 15

After Clearing
coll1:
coll2:
coll3:

Hash Code for coll1 After clearing 1




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment