When to return an Iterable <String> rather than List, Set, Collection?

So, I have worked quite a lot with the Neo4j API, and I noticed that almost always they will have functions that return

Iterable<Class> 

whereas I always understood that it is better to return one of Set, List or Collection, if it has no good reason for this. Set to indicate to the user that he will never have duplicate items, a List when the order is important or contains duplicates, or a Collection if you do not have a policy.

I prefer them Iterable, as they have useful utilities like .contains () ,. add (), etc. I found that very often I have to write code to convert Iterable to a collection.

So my question is: did I miss something important? Is there any good reason to use Iterable over Collection? When will you do it? Obviously, the guys from Neo4j seem like programmers at first-rate speed, so I guess they have a reason for this, but I don't see it.

+6
source share
1 answer

It just says that they give you something to sort out. They do not make any promises about what is implemented under the covers.

In particular, there cannot be a real collection class behind this. Elements can be evaluated and / or retrieved lazily when you iterate over them (imagine how you could return a collection of, say, 100M elements). In this case, you should carefully collect all the elements in the reified collection, as this will be quite significant.

Since Neo4J is a graph database, this makes sense. It is similar to the JDBC ResultSet , in which you do not get a collection of results, but rather row by row, and if you really want to save all the results, you need to explicitly create the collection yourself.

+8
source

Source: https://habr.com/ru/post/972175/


All Articles