I have a situation where I am reading a database and returning a List<String>, where each string is selected and added to the list according to some criteria. The method signature is:
public List<String> myMethod(String query, int limit)
The second parameter provides an upper bound on the size of the returned list (setting limit=-1 will remove any size restriction). To avoid making this method memory-intensive, I have written an equivalent method that returns Stream<String> instead of a list. ( Note: I don't need random access to the returned elements or any other list-specific functionality. )
However, I am a bit skeptical about returning a Stream<>, especially since the method is public. Is it safe to have a public method returning a Stream<> in Java?