KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Collection

java.util
Interface Collection<E>

All Superinterfaces:
Iterable<E>
All Known Subinterfaces:
BeanContext, BeanContextServices, BlockingQueue<E>, List<E>, Queue<E>, Set<E>, SortedSet<E>
All Known Implementing Classes:
AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayList, AttributeList, BeanContextServicesSupport, BeanContextSupport, ConcurrentLinkedQueue, CopyOnWriteArrayList, CopyOnWriteArraySet, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingQueue, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, Stack, SynchronousQueue, TreeSet, Vector
See Also:
Top Examples, Source Code, Object.hashCode(), contains(Object o), equals, addAll(Collection), Map, SortedMap, Collections

boolean add(E o)
See Also:
IllegalArgumentException, NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1240]Use new 1.5 For-Each Loop to iterate through an array
By Anonymous on 2005/05/27 12:16:16  Rate
public class Exemplo1  {  
   public static void main ( String args [  ]  )   {  
     String [  ]  nomes =  {  "pedra", "papel", "tesoura"  } ; 
     for  ( String nome : nomes )   {  
       System.out.println ( nome ) ; 
      }   
    }  
  }   
 


[1443]_
By mash on 2005/07/17 03:32:46  Rate
This will not work. Syntax Error in the foll line 
       for  ( String nome : nomes )   { 


[1677]_
By Anonymous on 2005/11/05 21:02:50  Rate
The code is OK, it is using new 1.5 for each loop. 
  
  
 see note at  
  
  
 http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html


public boolean add(Object o)
See Also:
IllegalArgumentException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean addAll(Collection<? extends E> c)
See Also:
add(Object), IllegalArgumentException, NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void clear()
See Also:
UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean contains(Object o)
See Also:
NullPointerException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean containsAll(Collection<?> c)
See Also:
contains(Object), NullPointerException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean equals(Object o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


int hashCode()
See Also:
Object.equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean isEmpty()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[598]Check for empty collection
By Joe User on 2005/02/05 07:30:36  Rate
Collection c= new Collection (  ) ; 
 //new collection, so it is empty 
 if  ( c.isEmpty (  )  )  {  
   System.out.println ( "Collection is empty" ) ; //this prints 
  } else {  
   System.out.println ( "Collection is NOT empty" ) ; //this does not print 
  } 


[832]_
By ajay on 2005/09/13 07:11:18  Rate
//Collection is abstract. so it can not be instantiated. to create an empty collection assign null to it. 
 // instantiating like -- >  Collection = new Collection (  ) ; ...will throw an error 
 Collection c = null; 
 //new collection, so it is empty 
 if  ( c.isEmpty (  )  )  {  
   System.out.println ( "Collection is empty" ) ; //this prints 
  } else {  
   System.out.println ( "Collection is NOT empty" ) ; //this does not print 
  } 


[1388]_
By darsoft { at } hotmail { dot } com on 2005/04/12 12:18:43  Rate
// ajay, this one does not throw null pointer exception. 
  
  
         Collection list= new ArrayList (  ) ; 
         if ( list.isEmpty (  )  )  
             System.out.println ( " Collection is empty " ) ; 
         else 
             System.out.println ( " Collection is not empty " ) ;


Iterator<E> iterator()
See Also:
Iterable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1783]Type casting of class while adding or reading from collection
By ijaz_rai { at } yahoo { dot } com on 2006/06/29 03:23:18  Rate
import java.util.*; 
  
  
 public class ArrayListGenericDemo {  
   public static void main ( String [  ]  args )   {  
    //this feature is intoduced in java 5.0 
    ArrayList < Integer >  data = new ArrayList < Integer >  ( 100 ) ; 
     for ( int i=0;i < 10;i++ )  
           data.add ( new Integer (  ( int )  ( Math.random (  )  * 100 )  )  ) ; 
  
  
     Iterator < Integer >  it = data.iterator (  ) ; 
  
  
     while  ( it.hasNext (  )  )  
      {  
   //u can type cast like  ( int ) it.next ratther Iterator < Integer >  
       int s =it.next (  ) ; 
       System.out.println ( "Value is: " + s ) ; 
      }  
    System.out.println ( "Size is: " + data.size (  )  ) ; 
  
  
    }  
  } 


boolean remove(Object o)
See Also:
UnsupportedOperationException, NullPointerException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[910]Remove an object
By dj28 { at } dj28 { dot } com on 2004/09/20 13:41:55  Rate
public boolean remove ( Object o )   {  
     Entry current = head.next, 
             previous = head; 
      
     // Case 1: collection is empty 
     if  ( isEmpty (  )  )   {  
       return false; 
      }  
     // Case 2: o equals first entry 
     if  ( o.equals ( head.element )  )   {  
           head = head.next; 
       return true; 
      }  
     // Case 3: otherwise, remove an element 
     else  {  
       while  ( !o.equals ( current.element )  )   {  
         if  ( current != null )   {  
           previous = previous.next; 
           current = current.next; 
          }  
        }  
       if  ( current != null )   {  
         previous.next = current.next; 
         return true;   
        }  
      }  
     return false; 
    }  // method remove


boolean removeAll(Collection<?> c)
See Also:
contains(Object), remove(Object), NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[911]remove all from a collection
By dj28 { at } dj28 { dot } com on 2004/09/20 13:42:25  Rate
public boolean removeAll ( Collection c )   {  
     Iterator itr = c.iterator (  ) ; 
     while  ( itr.hasNext (  )  )   {  
       remove ( itr.next (  )  ) ; 
      } 


boolean retainAll(Collection<?> c)
See Also:
contains(Object), remove(Object), NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


int size()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Object[] toArray()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object[] toArray(Object[] a)
See Also:
ArrayStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


<T> T[] toArray(T[] a)
See Also:
NullPointerException, ArrayStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags