I need to return / update a boolean by returning a list of material from a method. Java cannot return tuples, and I did not want to create a separate class for this
I ran into the same problem and the best IMHO solution is usually:
Just forget about your worries and make a separate class.
I used to hesitate in this matter, but classes are designed to encapsulate values, so go ahead and do it.
Most likely, as soon as your function returns an instance of a custom class, you will find that there are additional functions that fit well into this class or other methods that can use the class as a parameter or return value, and soon the class will be quite useful :-) .
If you really do not want to do this, you can always put everything in java.util.List or java.util.Map and return it. But this is really ugly :-( I did it too and regret it. At first it may seem simple, but as the code develops and grows, readability suffers (was that List of Integer, then Double or vice versa?) Classes are much more useful.
Note. If you think that a regular top-level class is redundant, you can use a nested class that is good for classes for "local use".
sleske
source share