3

Let's say I have a method in java, which looks up a user in a database and returns their address and the team they are on.

I want to return both values from the method, and don't want to split the method in two because it involves a database call and splitting involves twice the number of calls.

Given typical concerns in a moderate to large software project, what's the best option?

whatGoesHere getUserInfo(String name) {
  // query the DB
}

I know the question smells of duplication with existing ones, but each other question had some element that made it different enough from this example that I thought it was worth asking again.

1
  • some suggestions... create a struct yourself / use something like key-value pair / pass pointer in parameter Commented Jul 4, 2012 at 3:28

6 Answers 6

9

you have some options. The most OOP it will be create a class to encapsulate those 2 properties, something like that

private class UserInfo {
  private Address address;
  private Team team;
}

Or if you want a simple solution you can return an array of objects:

Object[] getUserInfo(String name) {
  // query the DB
  return new Object[]{address,team};
}

Or if you want to expose this method to some library you can have some interface that it will consume those properties, something like this:

class APIClass{
  interface UserInfo{
    public Address getAddress();
    public Team getTeam();
  }

  UserInfo getUserInfo(String name) {
    // query the DB
    return new UserInfo(){
         public Address getAddress(){ return address; }
         public Team getTeam(){ return team; }
    };
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I don't want to create a class just for this purpose, since it creates extra noise. Also, the user's team and name aren't "logically bound" - should I create a different class to represent every combination of values I might want to return from a query?
If the caller knows what to expect back, you could put it in a Map<String, Object> where your keys would be Address and Team. This depends heavily on the caller knowing which keys to look for though.
so, you have 2 options, the second solution that I showed you(the array one), or get a bit fancier returning a HashMap, as @Thomas had suggested .
I think getUserInfo of the API approach should return UserInfo not Object[].
me too. Dont return an array. its a messy solution. Use the "UserInfo" class. you can even add values later without changing everything. And as Thomas already said... the caller knows what to expect. so a UserInfo class is ok.
|
0

cant a map help , A MultivalueMap. Where the key is the user name and the 2 values are the adress and the team name. I am assuming both your Address and team are String variables, You can know more about Multivalue Map here

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html

http://apachecommonstipsandtricks.blogspot.in/2009/01/multi-value-map-values-are-list.html

3 Comments

How would I tell which is the user name and which is the address?
@BeeOnRope: You can do with just the normal Map. You can map a String to an Object. This is less flaky than Object[], but still not as good as creating a class.
The first value is always the key.. which is your user name and teh values are address and team names. your map will look like <UserName1 -->,Address1,Team1> You can then easily do a iteration over teh values and get the address and team names
0

First model your abstractions, relationships and multiplicity well (see an e.g. below). Then you can model tables accordingly. Once these two steps are performed you can either leverage JPA that can be configured to load your object graph or you write JDBC code and create the graph your self by running a SQL query with proper SQL JOINs.

  • A User has an Address
  • A Team can have 1 or more Users (and can a User play for more teams?)

Comments

0

You can return a String array with user name and group name in it . The method looks like :

public String[] getUserInfo(String name) {
    String[] result = new String[2];
    // query the DB
    ...
    result[0] = userName;
    result[1] = groupName;

    return result;
}

Comments

0

A common solution to this kind of issue is to create a custom object with as many attributes as the values you want to return. If you can't create a new class for this, you can use a Map<String, Object>, but this approach is not type-safe.

Comments

0

I thought Guava had a generic Pair class already, but I cannot find it. You can build your own using generics if you're on Java 1.5+.

public class Pair<X,Y>
{
    public final X first;
    public final Y second;

    public Pair(X first, Y second) {
        this.first = first;
        this.second = second;
    }
}

Feel free to make the fields private and add getters. :) Using it is easy:

return new Pair<Address,Team>(address, team);

Update

Apache Commons Lang has Pair. See this SO question for more options.

1 Comment

In java 8, we have built in Pair<k,v> class in javafx.util package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.