Java – linkedhashset as return type of API public method

Let's consider the following example

Write an API with a public method that returns a collection of unique objects I think it's a good setting to write the return type of this method. It's unique to show the item to the user If these items are unique and ordered, it is a correct idea to write a linkedhashset return type, or is it better to be a collection?

I know unique and classified collections I know it's a good idea to set the return type class (TreeSet, sortedset, linkedhashset) of public methods As far as OOP is concerned

Solution

I recommend not returning to linkedhashset (unless you have a very good reason) If you return to set, you can change the set implementation as needed, such as HashSet, TreeSet, etc

In this case, I think your suggestion to return set is good because it does indicate that these items are unique This also indicates that inclusion is usually fast (O (1) or O (log n))

On the other hand, collection is very general, but it tells the caller that it is a simple old group with no special restrictions on sorting or uniqueness Specifying set means that there is no confusion about uniqueness, and you can use it anywhere you can use collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>