简体   繁体   中英

How specific should my method header be about it's return type?

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.

I would go with

Set<Integer> returnSortedIntegers()

OR:

SortedSet<Integer> returnSortedIntegers()

if the callers need to directly access methods related to the fact that the Set is sorted.

My rule is to be as general as possible while preserving type safety.

I would choose Option C.

My rule of thumb is to always use an interface in my Parameters/returned object. This way, you can benefit more of polymorphism.

And using Generics is always a good idea since it is cear to the user of your method that the Set contains Integers. Before Generics, we had to know the implementation to find out what kind of Object was stored :-(

Option E:

public SortedSet<Integer> returnSortedIntegers()

This is giving the caller the minimum information they need to know about what is returned while still being helpful:

  • It is a Set
  • It contains Integer
  • It is sorted

The most commonly accepted approach is to return the generic version of the appropriate interface.

So in your case it would most likely be C .
Or, as other have suggested,

public SortedSet<Integer> returnSortedIntegers()

since it is a sorted set.

Depending on what the intended purpose of your method is though, that could also be

public Collection<Integer> returnSortedIntegers()

since you haven't specifically defined whether it should have the properties of a Set.

That being said, there are occasions where the most explicit case (D) would be correct as well, assuming that you want the users of the method to know that it is and always will be a TreeSet. This would assume that there are specific properties of this data structure that will always be required, and thus no other return type would be appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM