/* Author : Michael Robinson Program : collectionsClass.java it4 Purpose : A lot like an Array that holds stuff "holds references to other objects unlike an array Collections are DYNAMIC" A list or a Set (types of Collections) do not require a size to initialize they adjust by themselves as you work with them. List = CAN contain duplicates Updated : October 28th, 2099 */ import java.util.*; public class collectionsClass { public static void removeFrom( Collection listOne, Collection listTwo ) { Iterator removeIt = listOne.iterator(); while( removeIt.hasNext() ) { if( listTwo.contains( removeIt.next() ) ) { removeIt.remove(); } } }//end public static void removeFrom(Collection listOne, //Collection listTwo) public static void displayList(Collection collectionObj) { //display data in namesListOne Iterator iterCollection = collectionObj.iterator(); while( iterCollection.hasNext() ) { System.out.print( " " + iterCollection.next() ); } System.out.println(); }//end public static void displayList( Collection //collectionObj ) public static void createList( String namesArray1[], String namesArray2[] ) { //************************** // listOne //************************** //add array namesArray1 to List namesListOne System.out.println( "\n Copying the names in the first " + "array into a first list" ); //create a List of Strings List namesListOne = new ArrayList(); //copy array namesArray2 to List namesListTwo for( String w : namesArray1 ) { namesListOne.add(w); } displayList( namesListOne ); //add more items into List namesListOne System.out.println( "\n Adding Napoleon" ); namesListOne.add( "Napoleon" ); System.out.println( " Adding Nathalie" ); namesListOne.add( "Nathalie" ); System.out.print( "\n Printing items in first list : " ); displayList( namesListOne ); //************************** // listTwo //************************* //add array namesArray2 to List namesListTwo System.out.println( "\n Copying the names in the second " + "array into a second list" ); //create a List of Strings List namesListTwo = new ArrayList(); //copy array namesArray2 to List namesListTwo for( String w : namesArray2 ) { namesListTwo.add(w); } System.out.print( "\n Printing items in second list : " ); displayList( namesListTwo ); System.out.println( "\n Removing items in second list " + "from first list" ); removeFrom( namesListOne, namesListTwo ); System.out.print( "\n Printing items in first list : " ); displayList( namesListOne ); System.out.print( "\n Printing items in second list : " ); displayList( namesListTwo ); }//end public static void createList( String namesArray1[], //String namesArray2[] ) public static void createArrays() { //employees in graphics group System.out.println( "\n Creating array with employees " + "names" ); //create an array of Strings String namesArray1[] = { "Lisa", "Dan", "Mike", "Jane", "Phil" }; //create an array of Strings String namesArray2[] = { "Dennis", "Lisa", "Juan", "Jane", "Manny" }; createList( namesArray1, namesArray2 ); }//end public static void createArrays() public static void main( String arg[] ) { createArrays(); }//end public static void main( String arg[] ) }//end public class collectionsClass