Home arrow static arrow Java Programming [Archive] - The evil secret in the Set & Map families
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - The evil secret in the Set & Map families
This topic has 6 replies on 1 page.

Posts:3,999
Registered: 00-05-26
The evil secret in the Set & Map families  
Jun 22, 2004 7:52 PM



 
Hi folks, it's me again, complainin' about something that is of little importance, but just doin so to annoy those who are silly enough to read on.

A good number of you, at least the subset of purists out there, would argue that most probably, this function definition is wrong.

public void processApplications(  TreeSet applications ){     // fill in a nifty algorithm here}

The argument of course, is that, the api used by processApplications is totally defined by the Set interface, and the argument goes, that to allow the most flexibility, one should define the method as:

public void processApplications(  Set applications ){     // fill in a nifty algorithm here}


In this way, the client may pass any type set, and it would still work.
Even more likely is a method such as:

public TreeSet getApplications(){   // more niftyness}


Here we have locked ourselves into returning a TreeSet, when the client will only be using the interface of Set. What if TreeSet at some point turns out to be a undesirable choice?

----

The problem of course is, that inheritance hierarchy for HashSet and TreeSet is too abstract in my opinion. Sometimes we can easily substitute any Set for the Set interface, however sometimes we want the special-non-interface-specified-characteristics of the TreeSet to be required, because our algorithm expects an ordering.

Seems to me that it should be
class TreeSet implements SortedSet {...}


and

interface SortedSet extends Set { ... }

even if SortedSet is just a marker.

In this way we can document requirements, without unduely limiting ourselves to a class.

What'ya think?

 

Posts:3,999
Registered: 00-05-26
Re: The evil secret in the Set & Map families  
Jun 22, 2004 7:59 PM (reply 1 of 6)



 
Hmmm, before posting this thread, perhaps I should go double check to make sure there is no SortedSet interface....
 

Posts:3,999
Registered: 00-05-26
Re: The evil secret in the Set & Map families  
Jun 22, 2004 7:59 PM (reply 2 of 6)



 
D'oh!
 

Posts:945
Registered: 3/13/02
Re: The evil secret in the Set & Map families  
Jun 22, 2004 8:12 PM (reply 3 of 6)



 
That was worth reading. Funny :-)

Thanks. Glad you came full circle.
 

Posts:2,909
Registered: 13.8.2003
Re: The evil secret in the Set & Map families  
Jun 22, 2004 10:38 PM (reply 4 of 6)



 
I wondered why SortedSet sounded so familiar. But if you say it's not in the API, then it's not in the **** API! :)

A very good morning read
 

Posts:1,577
Registered: 00-02-25
Re: The evil secret in the Set & Map families  
Jun 23, 2004 6:01 AM (reply 5 of 6)



 
Perhaps you should ask why RandomAccess doesn't extend List :-)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/RandomAccess.html
 

Posts:6,750
Registered: 1/25/04
Re: The evil secret in the Set & Map families  
Jun 23, 2004 8:22 AM (reply 6 of 6)



 
Now that's evil. It's just a marker interface, so this is a valid class:
public class NotAList implements RandomAccess {}
Nice, huh?
 
This topic has 6 replies on 1 page.