If I have a method that is to return a TreeSet of integers how should I declare it?
Option A
public Collection returnSortedIntegers()
Option B
public Set returnSortedIntegers()
Option C
public Set<Integer> returnSortedIntegers()
Option D
public TreeSet<Integer> returnSortedIntegers()
The reason I ask is because I was getting the -Xlint:unchecked message when I compiled a class that used this method in one of it’s methods. I had no problems compiling it when it looked like Option A, it was only when I compiled the different class that used the method that I got the xlint message which is why I have changed it to Option C and the message has gone away.
Thank you for your answers, it would appear that this is winning
Option E
public SortedSet<Integer> returnSortedIntegers()
I am studying Java at the moment so it’s quite new to me and I wasn’t aware of the SortedSet interface, I was only aware that TreeSet was a subclass of Set. As always Stack Overflow has enlightened me.
Option E:
This is giving the caller the minimum information they need to know about what is returned while still being helpful:
SetInteger