Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning two different values from method

Tags:

java

I have method that parses a list of String records into objects and returns List of objects. So my method signature is like this.

public List<ParsedObject> parse(String[] records);

But I also want to return, other metrics like number of string records that were not parsed successfully. Now I get confused, how to return this metric. One option would be to create another wrapper class that holds both list of parsed records and members to store these metrics.

But I face this situation very often and this way I would end up in creating many wrapper classes.

Not sure if I explained well. Any suggestions here?

like image 952
RandomQuestion Avatar asked Jul 06 '26 22:07

RandomQuestion


1 Answers

Java does not support returning multiple values from a function, unfortunately. You can create a wrapper class, like you said. Another option is to pass in an array of integers or a "metrics" object or something like that and modify it inside Parse. Slightly better might be to have one or more instance variables (rather than method variables) to keep track of any sort of diagnostic information you need.

like image 157
Alexander Corwin Avatar answered Jul 10 '26 15:07

Alexander Corwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!