I’m trying to do something like this:
public void <String,int> getItem
{
return <"Jen",23>;
}
I know I can use a custom class, but how I would I return two results in one function call.
1 – Is the above template function possible in java, and how would by caller get part 1 of it and part 2 later.
2 – Can I do it using an associative array like in actionscript?
3 – Can I do it using a hashmap of some sort?
4 – What are other possible ways are there
I attempted all three ways, but one way or another syntax is hitting me. So if anyone can give clear examples
Java functions always return a single value, so your only option is to return a “collection” object which contains multiple values, such as an Array or a proper Collection. For example:
Although, a typical pattern in Java is to return a custom type which encapsulates your values, e.g.:
Or more generally:
Of course, there are serious implications regarding hashing (equality and hash code) if you want to use these custom types as hash keys.