// Algorithms2.java
// Using algorithms addAll, frequency and disjoint.
package chapter36;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Algorithms2
{
    private String[] colors = { "red", "white", "yellow", "blue" };
    private List< String > list1;
    private List< String > list2 = new ArrayList< String >();

    // create List and list2
    // and manipulate them with methods from Collections
    public Algorithms2()
    {
        // initialize list and list2
        list1 = Arrays.asList( colors );
        list2.add( "black" );
        list2.add( "red" );
        list2.add( "green" );

        System.out.println( "Before addAll, list2 contains:" );

        // display elements in list2
        for ( String s : list2 )
            System.out.printf( "%s ", s );

        // Invoke method addAll to add elements in array colors to list2
        Collections.addAll( list2, colors );

        System.out.println( "\n\nAfter addAll, list2 contains: " );

        // display elements in list2
        for ( String s : list2 )
            System.out.printf( "%s ", s );

        // Get the frequency of String “red” in Collection list2 using method frequency
        int frequency = Collections.frequency( list2, "red" );
        System.out.printf(
                "\n\nFrequency of red in list2: %d\n", frequency );

        // Invoke method disjoint to test whether Collections list1 and list2
        // have elements in common
        boolean disjoint = Collections.disjoint( list1, list2 );

        System.out.printf( "\nlist1 and list2 %s elements in common\n",
                ( disjoint ? "do not have" : "have"  ) );
    } // end Algorithms2 constructor

    public static void main( String args[] )
    {
        new Algorithms2();
    } // end main
} // end class Algorithms2

