HashSet Examples

HashSet Examples

HashSet 1 - Construction, enumeration, rejection of duplicates.

HashSet 2 - Union, intersection, difference, symmetric difference, subset.

HashSet 3 - Counting, finding, removing.

HashSet 4 - Construction, enumeration, acceptance of duplicates.

HashSet 5 - Counting, finding, removing with duplicates allowed.

HashSet 6 - Bounds. Duplicates allowed.

HashSet 7 - Using another BinaryPredicate for matching


HashSet1 Example Code

// Copyright(c) 1996 ObjectSpace, Inc. import java.util.Enumeration; import jgl.*; /** * Construction, enumeration, rejection of duplicates. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet1 { public static void main( String[] args ) { HashSet set = new HashSet(); set.add( new Integer( 6 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 4 ) ); System.out.println( set ); System.out.println(); System.out.println( "Enumerate the HashSet" ); Enumeration e = set.elements(); while( e.hasMoreElements() ) System.out.println( e.nextElement() ); System.out.println(); System.out.println( "Iterate through the HashSet" ); for( HashSetIterator i = set.begin(); !i.atEnd(); i.advance() ) System.out.println( i.get() ); System.out.println(); System.out.println( "Show that duplicates cannot be added." ); Object value = set.add( new Integer( 8 ) ); if( value != null ) System.out.println( "Could not add 8." ); else { System.out.println( "Added 8" ); System.out.println( "New contents are " + set ); } value = set.add( new Integer( 4 ) ); if( value != null ) System.out.println( "Could not add 4." ); else { System.out.println( "Added 4." ); System.out.println( "New contents are " + set ); } } }

HashSet1 Example Output

HashSet( 1, 4, 6 ) Enumerate the HashSet 1 4 6 Iterate through the HashSet 1 4 6 Show that duplicates cannot be added. Added 8 New contents are HashSet( 1, 4, 6, 8 ) Could not add 4.

HashSet2 Example Code

// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; /** * Union, intersection, difference, symmetric difference, subset. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet2 { public static void main( String[] args ) { HashSet set1 = new HashSet(); set1.add( "ape" ); set1.add( "cat" ); set1.add( "bat" ); HashSet set2 = new HashSet(); set2.add( "bat" ); set2.add( "fox" ); set2.add( "ape" ); System.out.println( "set1 = " + set1 + ", set2 = " + set2 ); HashSet set3 = set1.union( set2 ); System.out.println( "set3 = set1.union( set2 ) = " + set3 ); HashSet set4 = set1.intersection( set2 ); System.out.println( "set4 = set1.intersection( set2 ) = " + set4 ); HashSet set5 = set1.difference( set2 ); System.out.println( "set5 = set1.difference( set2 ) = " + set5 ); HashSet set6 = set1.symmetricDifference( set2 ); System.out.println( "set6 = set1.symmetricDifference( set2 ) = " + set6 ); System.out.println( "set4.subsetOf( set3 ) = " + set4.subsetOf( set3 ) ); System.out.println( "set3.subsetOf( set4 ) = " + set3.subsetOf( set4 ) ); } }

HashSet2 Example Output

set1 = HashSet( ape, bat, cat ), set2 = HashSet( ape, bat, fox ) set3 = set1.union( set2 ) = HashSet( ape, bat, fox, cat ) set4 = set1.intersection( set2 ) = HashSet( ape, bat ) set5 = set1.difference( set2 ) = HashSet( cat ) set6 = set1.symmetricDifference( set2 ) = HashSet( fox, cat ) set4.subsetOf( set3 ) = true set3.subsetOf( set4 ) = false

HashSet3 Example Code

// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; /** * Counting, finding, removing. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet3 { public static void main( String[] args ) { HashSet set = new HashSet(); set.add( "cat" ); set.add( "ape" ); set.add( "dog" ); set.add( "bat" ); System.out.println( set ); System.out.println( "set.count( dog ) = " + set.count( "dog" ) ); HashSetIterator i = set.find( "dog" ); if( i.equals( set.end() ) ) // A simpler way of saying this is: if( i.atEnd() ) ... System.out.println( "Could not find dog." ); else System.out.println( "Found " + i.get() ); System.out.println( "set.remove( dog ) = " + set.remove( "dog" ) ); HashSetIterator j = set.find( "dog" ); if( j.atEnd() ) // A simpler way of saying: if( j.equals( set.end() ) ) ... System.out.println( "Could not find dog." ); else System.out.println( "Found " + j.get() ); } }

HashSet3 Example Output

HashSet( ape, dog, bat, cat ) set.count( dog ) = 1 Found dog set.remove( dog ) = dog Could not find dog.

HashSet4 Example Code

import java.util.Enumeration; import jgl.*; /** * Construction, enumeration, acceptance of duplicates. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet4 { public static void main( String[] args ) { HashSet set = new HashSet( true ); set.add( new Integer( 6 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 4 ) ); set.add( new Integer( 1 ) ); System.out.println( set ); System.out.println(); System.out.println( "Enumerate the HashSet" ); Enumeration e = set.elements(); while( e.hasMoreElements() ) System.out.println( e.nextElement() ); System.out.println(); System.out.println( "Iterate through the HashSet" ); for( HashSetIterator i = set.begin(); !i.atEnd(); i.advance() ) System.out.println( i.get() ); System.out.println(); System.out.println( "Show that duplicates can be added." ); set.add( new Integer( 8 ) ); System.out.println( "set = " + set ); set.add( new Integer( 4 ) ); System.out.println( "set = " + set ); } }

HashSet4 Example Output

HashSet( 1, 1, 4, 6 ) Enumerate the HashSet 1 1 4 6 Iterate through the HashSet 1 1 4 6 Show that duplicates can be added. set = HashSet( 1, 1, 4, 6, 8 ) set = HashSet( 1, 1, 4, 4, 6, 8 )

HashSet5 Example Code

import jgl.*; /** * Counting, finding, removing with duplicates allowed. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet5 { public static void main( String[] args ) { HashSet set = new HashSet( true ); set.add( "cat" ); set.add( "ape" ); set.add( "dog" ); set.add( "bat" ); set.add( "dog" ); System.out.println( set ); System.out.println( "set.count( dog ) = " + set.count( "dog" ) ); HashSetIterator i = set.find( "dog" ); if( i.equals( set.end() ) ) // A simpler way of saying this is: if( i.atEnd() ) ... System.out.println( "Could not find dog." ); else System.out.println( "Found " + i.get() ); System.out.println( "set.remove( dog ) = " + set.remove( "dog" ) ); HashSetIterator j = set.find( "dog" ); if( j.atEnd() ) // A simpler way of saying: if( j.equals( set.end() ) ) ... System.out.println( "Could not find dog." ); else System.out.println( "Found " + j.get() ); } }

HashSet5 Example Output

HashSet( ape, dog, dog, bat, cat ) set.count( dog ) = 2 Found dog set.remove( dog ) = dog Could not find dog.

HashSet6 Example Code

import jgl.*; /** * Bounds. Duplicates allowed. *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet6 { public static void main( String[] args ) { HashSet set = new HashSet( true ); set.add( new Integer( 3 ) ); set.add( new Integer( 8 ) ); set.add( new Integer( 2 ) ); set.add( new Integer( -2 ) ); set.add( new Integer( 3 ) ); set.add( new Integer( 10 ) ); System.out.println( set ); Range range = set.equalRange( new Integer( 3 ) ); while( !range.begin.equals( range.end ) ) System.out.println( "match @ " + range.begin.nextElement() ); } }

HashSet6 Example Output

HashSet( 2, 3, 3, 8, 10, -2 ) match @ 3 match @ 3

HashSet7 Example Code

import java.util.Enumeration; import jgl.*; /** * Using another BinaryPredicate for matching *

* @see jgl.HashSet * @version 1.1 * @author ObjectSpace, Inc. */ public class HashSet7 { public static void main( String[] args ) { HashSet set = new HashSet( new IdenticalTo() ); set.add( new Integer( 6 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 4 ) ); // this WILL work because it is a seperate object than the other Integer(1) set.add( new Integer( 1 ) ); System.out.println( set ); System.out.println(); System.out.println( "Add an object Integer(100)" ); Integer tryit = new Integer(100); System.out.println( "add returns: " + set.add( tryit ) ); System.out.println( "set = " + set ); System.out.println( "Try to add the EXACT same object Integer(100)" ); System.out.println( "add returns: " + set.add( tryit ) ); System.out.println( "set = " + set ); } }

HashSet7 Example Output

HashSet( 1, 1, 4, 6 ) Add an object Integer(100) add returns: null set = HashSet( 1, 1, 4, 6, 100 ) Try to add the EXACT same object Integer(100) add returns: 100 set = HashSet( 1, 1, 4, 6, 100 )