All Packages Class Hierarchy This Package Previous Next Index
HashSet1 - Construction, enumeration, rejection of duplicates.
HashSet2 - Union, intersection, difference, symmetric difference, subset.
HashSet3 - Counting, finding, removing.
HashSet4 - Construction, enumeration, acceptance of duplicates.
HashSet5 - Counting, finding, removing with duplicates allowed.
HashSet6 - Bounds. Duplicates allowed.
HashSet7 - Using another BinaryPredicate for matching
HashSet1 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, rejection of duplicates.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @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 );
}
}
}
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,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Union, intersection, difference, symmetric difference, subset.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @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 ) );
}
}
set1 = HashSet( ape, bat, cat ), set2 = HashSet( ape, bat, fox )
set3 = set1.union( set2 ) = HashSet( ape, bat, cat, fox )
set4 = set1.intersection( set2 ) = HashSet( ape, bat )
set5 = set1.difference( set2 ) = HashSet( cat )
set6 = set1.symmetricDifference( set2 ) = HashSet( cat, fox )
set4.subsetOf( set3 ) = true
set3.subsetOf( set4 ) = false
HashSet3 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Counting, finding, removing.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @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: 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: if ( j.equals( set.end() ) ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + j.get() );
}
}
HashSet( dog, ape, bat, cat )
set.count( dog ) = 1
Found dog
set.remove( dog ) = 1
Could not find dog.
HashSet4 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import java.util.Enumeration;
/**
* Construction, enumeration, acceptance of duplicates.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class HashSet4
{
public static void main( String[] args )
{
HashSet set = new HashSet( true ); // allow duplicates
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 );
}
}
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
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Counting, finding, removing with duplicates allowed.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class HashSet5
{
public static void main( String[] args )
{
HashSet set = new HashSet( true ); // allow duplicates
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: 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: if ( j.equals( set.end() ) ) ...
System.out.println( "Could not find dog." );
else
System.out.println( "Found " + j.get() );
}
}
HashSet( dog, dog, ape, bat, cat )
set.count( dog ) = 2
Found dog
set.remove( dog ) = 2
Could not find dog.
HashSet6 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Bounds. Duplicates allowed.
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class HashSet6
{
public static void main( String[] args )
{
HashSet set = new HashSet( true ); // allow duplicates
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() );
}
}
HashSet( 2, 3, 3, 8, 10, -2 )
match @ 3
match @ 3
HashSet7 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
import java.util.Enumeration;
/**
* Using another BinaryPredicate for matching
*
* @see com.objectspace.jgl.HashSet
* @version 3.0.0
* @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 );
}
}
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 )
All Packages Class Hierarchy This Package Previous Next Index