-2

Hi I have problem with creating a generic TreeSet with its Iterator. I want to use it to three different kind of Types.

Can anyone help me how to do that?

Mohammed Mohammed
  • 403
  • 1
  • 9
  • 18

1 Answers1

1

If you want to create a TreeSet which contains objects of type ClassA, ClassB and ClassC you'd have to use

TreeSet<MostSpecificSupertypeOfABC>

If if there's no suitable type to use, you could create one as I have described in an answer to the question below:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • What I'm trying is that, I have 3 TreeSets each with different type and I want to create a new generic TreeSet inside a method and passing those TreeSets to the method and create a generic Iterator and work on it. – Mohammed Mohammed Mar 18 '12 at 21:56
  • public void method(TreeSet> set) { if(set.equals("SetA")) Iterator iter = SetA.iterator(); else if(set.equals("SetB")) Iterator iter = SetB.iterator(); else if(set.equals("SetC")) Iterator iter = SetC.iterator(); while(iter.hasNext()) iter.next().getName();} getName is inside each of the three TreeSet – Mohammed Mohammed Mar 18 '12 at 22:06
  • I refrained from writing an answer when you first posted your question because I didn't understand what you want to do. After seeing this code in your comment, I still do not understand what you want to do. Can you explain once more, in detail, what you are trying to do? – Irfy Mar 18 '12 at 22:16
  • Are you simply trying to add all elements from `SetA`, `SetB` and `SetC` to a new `Set`? – Irfy Mar 18 '12 at 22:17
  • I'm reading the name of the treesets from a file and I have to check each time which treeset its, after finding its name I have to work on it. – Mohammed Mohammed Mar 18 '12 at 22:32