• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections Framework Interface Reference Types  RSS feed

 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

For the following code as displayed on line 4 it is possible to create the TreeSet using the interface type SortedSet. Is there any reason why using line 3 is better than using SortedSet as the type on line 4? Line 3 is the way it was done in some example code. Thanks.

public static void main(String[] args){

Set mySet = new TreeSet(); //3
//SortedSet mySet = new TreeSet(); //4

// Populate the Set
boolean added;
for (int i = 0; i < myArgs.length ; i++){
added = mySet.add(myArgs[i]);
if (!added){
System.out.println("Duplicate found: " + myArgs[i]);
}
}
//Print the Set
Iterator i = mySet.iterator();
while(i.hasNext()){

System.out.println("TreeSet Element : " + i.next());

}

}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Brian L,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question.

This pattern of "programming to the interface" allows you the flexibility to change the actual type later without breaking other code that is expecting a Set -- for example, a method that takes a Set as an argument.

In this case, the instance is a TreeSet, which is a SortedSet, and even more broadly, a Set. By treating this as a Set rather than a SortedSet, you keep the option of changing the actual type to a HashSet or a LinkedHashSet (both of which are Sets, but not SortedSets) without breaking other code that treats this instance as a Set.

Does that make sense?

(PS: Thanks for changing your display name so quickly!)
[ January 01, 2006: Message edited by: marc weber ]
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, that makes good sense. Using the interface Set (which I think is the Super Type ?), allows for greater interoperability. Thanks for your fast reply.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Lyons:
Hi,

Yes, that makes good sense. Using the interface Set (which I think is the Super Type ?), allows for greater interoperability. Thanks for your fast reply.


It will help if you go through the javadocs of the collections class. A read of the tutorials will help too.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!