javaclassreferencereturnkeyvaluepair

How to return 2 ArrayLists in java


I need to make a method that can return 2 ArrayLists.

I have read some answers and came up with a few options:

  1. return Pair(arrList 1, arrList 2);

    This is the easier option, but I dont know if it will work, and I dont know how to extract the ArrayLists from the Pair in the method that calls this method

  2. Try making a class that holds both of those ArrayList. I don't know if this would work, but I think it should.

Does any one know if any of these options would work, or if there is any other options I could use?


Solution

  • Create a class with property both heterogeneous list

    public Class MyClass
    {
       public List<> list1;
       public List<> list2;
    }
    

    Return the new object of the class from your method. If the lists are homogeneous, return a list holding both.
    NOTE: both of your solutions are correct. It will work for sure, if you are doing it right.