Source of CollectionTest.java


  1: // Fig. 19.3: CollectionTest.java
  2: // Using the Collection interface.
  3: import java.util.List;
  4: import java.util.ArrayList;
  5: import java.util.Collection;
  6: import java.util.Iterator;
  7: 
  8: public class CollectionTest 
  9: {
 10:    private static final String[] colors = 
 11:       { "MAGENTA", "RED", "WHITE", "BLUE", "CYAN" };
 12:    private static final String[] removeColors = 
 13:       { "RED", "WHITE", "BLUE" };
 14: 
 15:    // create ArrayList, add Colors to it and manipulate it
 16:    public CollectionTest()
 17:    {
 18:       List< String > list = new ArrayList< String >();      
 19:       List< String > removeList = new ArrayList< String >();
 20: 
 21:       // add elements in colors array to list
 22:       for ( String color : colors )
 23:          list.add( color );       
 24: 
 25:       // add elements in removeColors to removeList
 26:       for ( String color : removeColors )
 27:          removeList.add( color ); 
 28: 
 29:       System.out.println( "ArrayList: " );
 30: 
 31:       // output list contents
 32:       for ( int count = 0; count < list.size(); count++ )
 33:          System.out.printf( "%s ", list.get( count ) );
 34: 
 35:       // remove colors contained in removeList
 36:       removeColors( list, removeList );
 37: 
 38:       System.out.println( "\n\nArrayList after calling removeColors: " );
 39: 
 40:       // output list contents
 41:       for ( String color : list )
 42:          System.out.printf( "%s ", color );
 43:    } // end CollectionTest constructor
 44: 
 45:    // remove colors specified in collection2 from collection1
 46:    private void removeColors( 
 47:       Collection< String > collection1, Collection< String > collection2 )
 48:    {
 49:       // get iterator
 50:       Iterator< String > iterator = collection1.iterator(); 
 51: 
 52:       // loop while collection has items
 53:       while ( iterator.hasNext() )         
 54: 
 55:          if ( collection2.contains( iterator.next() ) )
 56:             iterator.remove(); // remove current Color
 57:    } // end method removeColors
 58: 
 59:    public static void main( String args[] )
 60:    {
 61:       new CollectionTest();
 62:    } // end main
 63: } // end class CollectionTest
 64: 
 65: /**************************************************************************
 66:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 67:  * Pearson Education, Inc. All Rights Reserved.                           *
 68:  *                                                                        *
 69:  * DISCLAIMER: The authors and publisher of this book have used their     *
 70:  * best efforts in preparing the book. These efforts include the          *
 71:  * development, research, and testing of the theories and programs        *
 72:  * to determine their effectiveness. The authors and publisher make       *
 73:  * no warranty of any kind, expressed or implied, with regard to these    *
 74:  * programs or to the documentation contained in these books. The authors *
 75:  * and publisher shall not be liable in any event for incidental or       *
 76:  * consequential damages in connection with, or arising out of, the       *
 77:  * furnishing, performance, or use of these programs.                     *
 78:  *************************************************************************/