RandomQuestion
RandomQuestion

Reputation: 6988

Returning two different values from method

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?

Upvotes: 3

Views: 1254

Answers (7)

jddsantaella
jddsantaella

Reputation: 3687

You can return a complex object containing the list and all the information you need.

Upvotes: 1

Marko Viitanen
Marko Viitanen

Reputation: 51

Your question has already been discussed (see this for example: Should Java method arguments be used to return multiple values? ). I personally think that you should either make two method calls, if the returned data is not related. If they are, then you should create a "wrapper" class as you call it. If they really are related data then they probably belong in the same class anyway.

I don't personally favor modifying passed in objects because to me it is a side effect, and it is not clear to see what the method really does.

Another way to think of it is to use the factory pattern (see http://en.wikipedia.org/wiki/Factory_method_pattern) if the object you are building is complex.

Upvotes: 5

uzilan
uzilan

Reputation: 2624

This is a very common problem for many people who develop using Java. In other languages, such as Scala, one can create tuples, which are anonymous objects which can hold multiple values, and use them as arguments or return values.

Upvotes: 0

Bogdan
Bogdan

Reputation: 944

A wrapper class is the standard way of returning more information from a function. Another alternative would be to pass another parameter by reference and modify it in your function, thus effectively returning new information to the caller. For example, in your case you would pass an empty list and add all the parsed elements in that list. The return type could be a metric or an error code. Thus the caller will have both pieces of information.

Upvotes: 0

picciano
picciano

Reputation: 22701

Create a ParseResult object. You could include the List, number of records parsed, errors, etc. Make it generic enough so that it could be returned from different methods. You could even make it a base class and return classes that extend from it. Just keeping thinking in terms of objects.

Upvotes: 1

Santiago V.
Santiago V.

Reputation: 1118

I was going to suggest to you some kind of 'c++ pair' type, but then I found this: What is the equivalent of the C++ Pair<L,R> in Java?

Upvotes: 0

Alexander Corwin
Alexander Corwin

Reputation: 1157

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.

Upvotes: 5

Related Questions