0
 @RequestMapping(value="/portfolios", method = RequestMethod.GET)
    public @ResponseBody List<PortfolioVO>  getPortfolios(HttpServletRequest request, @RequestParam(required= false) String region) {
List<PortfolioVO> portfolios = gmiCacheService.getPortoliosForRegion(user, region);

        return portfolios;      
    }

I have another List from another function that I need to add.

to then just return portfolios. I am very new to Java and do not know how to implement this.

user7470849
  • 123
  • 1
  • 12
  • 1
    what is it exactly that you want to return? the 2 lists separately? 1 list with all the elements of both lists? something else? – njzk2 Feb 14 '17 at 21:38
  • @DanielPryden No, he wants return two **different** lists from method, not join them – Andremoniy Feb 14 '17 at 21:41
  • Yes, I need them to be separate as I will be sorting them differently – user7470849 Feb 14 '17 at 21:44
  • @user7470849 your second code in the question is confusing; might want to delete that. It suggests you are looking for an `intersection` of two lists. – boxed__l Feb 14 '17 at 21:53

3 Answers3

1

In c#, I would use Tuple for storing those two list separately. But in Java, I didn't get similar data structure. But you can make your custom tuple structure.

From this answer :

public class Tuple<X, Y> { 
  public final X x; 
  public final Y y; 
  public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
  } 
}

Using above structure, you can store two different list and use it.

If you are allowed to use third party library, you can have javatuples where you can use Pair structure

Community
  • 1
  • 1
Akash KC
  • 16,057
  • 6
  • 39
  • 59
1

Since Java doesn't have built-in pairs, the "Java way" of doing it is to return a custom class that has two List fields. Something like:

public class MyReturnType{
      public List<PortfolioV0> l1,l2;
}

Another option is to return a List<List<PortfolioV0>> (a list of lists), and have the first item be the first list, and the second item, the second list.

A third option is to give up, and write your own Pair class (or use Apache's), and return a Pair<List<PortfolioV0>,List<PortfolioV0>>

Malt
  • 28,965
  • 9
  • 65
  • 105
0

I see two ways:

1) return array of lists: public @ResponseBody List[]<PortfolioVO> getPortfolios( ... )

inside it: return new List[] { list1, list2 };

This is fast to implement, but it is not nice and little bit dirty.

2) More proper way, I think, create class for this case:

static class ListPairHolder {
    List<PortfolioVO> list1;
    List<PortfolioVO> list2;

    ListPairHolder(List<PortfolioVO> list1, List<PortfolioVO> list2){
       this.list1 = list1; this.list2 = list2;
    }
}

and return this type from method.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241