Home > Software engineering >  Make a java method return different objects in one single call
Make a java method return different objects in one single call

Time:01-06

I have a java method which has to return both List<ObjectA> and a List<List<String>> for each and every call made to it.

One way I can think of achieving this is by returning Map<String, Object> where string will be key and object will be my lists. But I think returning a Map is not an elegant way of doing so in my scenario. Any help with this?

CodePudding user response:

With Java 14 , we have the possibility to use Records (JEP 395, openjdk.org). Without knowing anything of the semantics, I will demonstrate the usage with a generic record Pair, but we can of course always find more descriptive names.

We define a generic Pair of generic types T and U:

record Pair<T, U> (T first, U second) {}

And a dummy-implementation of ObjectA:

class ObjectA {
  private final String name;

  public ObjectA(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "ObjectA{"  
        "name='"   name   '\''  
        '}';
  }
}

We can then implement our method something like so:

public static Pair<List<ObjectA>, List<List<String>>> foo() {
  return new Pair<>(
      List.of(new ObjectA("one"), new ObjectA("two")),
      List.of(
          List.of("three", "four"),
          List.of("five", "six"),
          List.of("seven", "eight")));
}

And call it like so:

var fooResult = foo();
System.out.println(fooResult.first()); // prints the List<ObjectA>
System.out.println(fooResult.second()); // prints the List<List<String>>
System.out.println(fooResult);

This would produce the following output:

[ObjectA{name='one'}, ObjectA{name='two'}]
[[three, four], [five, six], [seven, eight]]
Pair[first=[ObjectA{name='one'}, ObjectA{name='two'}], second=[[three, four], [five, six], [seven, eight]]]

(Sorry, no Ideone.com demo, Ideone does not support Java 14 , only Java 12)

CodePudding user response:

I guess there's no real universal answer to this question.

One could argue that every time you use Object in Java, it's already somewhat bad since you haven't defined a proper Object containing all of the stuff you need, on the other hand, I really know how much of an annoyance this can be, therefore I try to stick to data structures already available, mainly Maps.

The main problem is that your method(s) calling the method returning an Object need to cast the results anyway - without the knowledge of what this function does, it's hard to give any real advice here.

Therefore my approach would be to look at what exactly are we returning and what these objects actually contain.

I guess the even easier way would be to define a List<List<String>> on your ObjectA and just check of the existence of that list and modify your other method accordingly, if that does make any sense in the context of ObjectA and your methods.

Other than that, I'd probably go with a Map<String,Object> or even Map<?,Object>.

CodePudding user response:

If I understood your question correctly, you should create a class with two fields List<ObjectA> and List<List<String>> as a model class. after filling the object with the required data, return it as a return type.
The model class sample :

public class TestModel{
  private ObjectA objectA;
  private List<List<String>> listOfStrings ; 
}

Your method will be :

public TestModel doSomething(....){

//do something
...

//in the end
TestModel testModel = new TestModel();
testModel.setObjectA(objectA);
testModel.setListOfStrings(listOfStrings);
return testModel;
}
  • Related