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.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • some suggestions... create a struct yourself / use something like key-value pair / pass pointer in parameter – Alvin Wong Jul 04 '12 at 03:28

6 Answers6

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; }
    };
  }
}
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
  • 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? – BeeOnRope Jul 04 '12 at 03:30
  • 2
    If the caller knows what to expect back, you could put it in a Map where your keys would be Address and Team. This depends heavily on the caller knowing which keys to look for though. – Thomas Jul 04 '12 at 03:34
  • 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 . – Arthur Neves Jul 04 '12 at 03:37
  • 2
    I think `getUserInfo` of the API approach should return `UserInfo` not `Object[]`. – RP- Jul 04 '12 at 04:21
  • 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. – some_coder Jul 04 '12 at 06:03
  • @BeeOnRope: Why is the same query returning both values if they aren't logically related? – casablanca Jul 04 '12 at 06:04
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

Raveesh Sharma
  • 1,486
  • 5
  • 21
  • 38
  • How would I tell which is the user name and which is the address? – BeeOnRope Jul 04 '12 at 03:32
  • @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. – nhahtdh Jul 04 '12 at 03:34
  • 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 ,Address1,Team1> You can then easily do a iteration over teh values and get the address and team names – Raveesh Sharma Jul 04 '12 at 03:34
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?)
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
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;
}
Sabbath
  • 91
  • 3
  • 26
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.

davioooh
  • 23,742
  • 39
  • 159
  • 250
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.

Community
  • 1
  • 1
David Harkness
  • 35,992
  • 10
  • 112
  • 134