package com.mindprod.example;

import java.util.Arrays;
import java.util.HashSet;

/**
 * example use of java.util.HashSet. Sample code to TEST and demonstrate the use of HashSet. requires JDK 1.5+
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
public final class TestHashSet
    {
    // --------------------------- main() method ---------------------------

    /**
     * Sample code to TEST and demonstrate the use of HashSet. requires JDK 1.5+
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        // create a new HashSet containing a list of all the legitimate state
        // and province abbreviations.
        HashSet<String> regions = new HashSet<String>( 149
                /* capacity */, 0.75f
                /* loadfactor */ );

        // add some elements, the abbreviations of states and provinces

        regions.add( "WA" );
        regions.add( "NY" );
        regions.add( "RI" );
        regions.add( "BC" );
        regions.add( "ON" );
        // ...

        // look up a key in the HashSet to see if item is there
        boolean isLegit = regions.contains( "NY" );

        // prints "true"
        System.out.println( isLegit );

        // Note, there is no HashSet method to get you a reference to the
        // canonical unique String Object for each region String,
        // similar to what String.intern does.

        System.out.println( "enumerate all the elements in the regions HashSet, unordered" );
        for ( String region : regions )
            {
            // prints lines of the form NY
            // in effectively random order
            System.out.println( region );
            }
        System.out.println( "enumerate all the elements in the regions HashSet, sorted" );

        String[] sortedRegions = regions.toArray( new String[regions.size()] );
        Arrays.sort( sortedRegions );

        for ( String region : sortedRegions )
            {
            // prints lines of the form NY
            // in alphabetical order
            System.out.println( region );
            }
        }// end main
    }