Solution to b401: Generic sets
See code at solutions/code/tutorialquestions/questionb401
Making the solution to the question 8a61 generic is fairly straightforward: compare the sample solutions.
To make the method:
public static IntSet readIntegers(int n) throws IOException;
return instead a GenericSet
with element type Integer
you need to change the method's signature as
follows:
public static GenericSet<Integer> readIntegers(int n) throws IOException;
In the implementation of the method, where you would use:
IntSet
, MemoryEfficientIntSet
or SpeedEfficientIntSet
instead use:
GenericSet<Integer>
, MemoryEfficientGenericSet<Integer>
or
SpeedEfficientGenericSet<Integer>
respectively.
When your program prints the type of set that has been returned by the readIntegers
method you should find that
it either says:
SpeedEfficientIntSet
or
MemoryEfficientIntSet
.
The generic parameter
<Integer>
is not shown. This is because generic information is not present at runtime. Remember that
the compiler performs type erasure, which gets rid of this information during the compilation process.