Hi Fred

You have put your finger on a basic feature - important to understand.

You need a class performing a specific task (in your case you need something that behaves like a Set). There are several way to implement the overall behavior, but not all the ways are the most efficient for all the use case.

Let's take another (hypothetical) example: you wish to provide some classes performing Complex number arithmetic. Inside your Complex class you may store the Real part and the Imaginary part as two double values. Or you may store the modulus and the angle (as two double values too). In the first case additions would be faster. In the second case multiplications would be faster.

Your library class may be use in programs who perform mainly additions or in programs who perform mainly multiplications. What to do to always provide the best response times?

A solution is to provide a "ComplexNumber" interface, then to create two classes implementing it: "CNumbersForAddition" and "CNumbersForMultiply".

The "end user" of your library would write his/her program using as much as possible the interface. Such as "ComplexNumber c = new CNumbersForAdition();". Then he/she would run a test and see how fast it performs. Then replace all the "new" statements such as "ComplexNumber c = CNumbersForMultiply();" and compare the response times.

In the same way, it is a good idea to use "Set s = new HashSet();" and use only the "Set" features. If you find at a later moment that another set implementation is better for your case, you can easier modify everything later.

Of course, there is an exception: if you absolutely need the HashSet() supplementary features (that are not implementations of the Set interface methods).

Hope it Helps
Michel Mihai Dinca


On 05/01/2012 22:42, Friedrich Clausen wrote:
Hello,

In section 1.2, in exercise program Set-HashSet-FindDup we declare a
new HashSet like so -

Set s = new HashSet();

but I notice that

HashSet s = new HashSet();

also works. What is the recommended way to create a new HashSet
instance?

Thanks!

Fred.


--
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jpassion_java?hl=en.

Reply via email to