简体   繁体   中英

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?

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.

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.

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.

您可以返回包含列表的复杂对象以及所需的所有信息。

我打算向你推荐一种'c ++ pair'类型,但后来我发现了这个: Java中C ++ Pair <L,R>的等价物是什么?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM