4

Is there any way to return two values from one method....

Example:

public Class Sample
{
  public List<Date> returnTwoArrayList()
   {
      List<Date> startDate=new ArrayList<Date>();
      
      List<Date> endDate=new ArrayList<Date>();

     //So I want to Return Two values startDate and endDate ...It is Possible????
   }
}

i'm calling this method into My Service class and in that Storing StartDate and endDate into Database here these are Two different Columns

**StartDate      endDate**
2012-12-01     2012-12-05
2012-12-01     2012-12-15
2012-12-02     2012-12-10
2012-12-20     2012-12-25
2012-12-25     2012-12-31
 

7 Answers 7

14

You cannot return separate structures via one method call, but you can return a composite of them. For example, returning a list of your lists would be a possible solution:

   public List<List<Date>> returnTwoArrayList()
   {
      List<Date> startDates = new ArrayList<Date>();
      List<Date> endDates = new ArrayList<Date>();

      List<List<Date>> result = new ArrayList<List<Date>>();
      result.add(startDates);
      result.add(endDates);

      return result;
   }

You can use get() method to retrieve these lists later on. Suppose you have made a call like List<List<Date>> twoLists = returnTwoArrayList(); then you can get startDate by calling twoLists.get(0) and similarly endDate with twoLists.get(1)

Sign up to request clarification or add additional context in comments.

2 Comments

ok... then i want to separate StartDate and endDate in another Service Class in that time how to Do?
@NarasimhamK see my answer, i added explanation for that.
6

No you can not return two value from a method.

Best way would be create a custom class with two field and return that object.

class ObjectHolder{
    private List<Date> startDate=new ArrayList<Date>();
    private List<Date> endDate=new ArrayList<Date>();

    <getter & setter method>
}

and -

public ObjectHolder returnTwoArrayList(){
    ObjectHolder oh = new ObjectHolder();
    List<Date> startDate=new ArrayList<Date>();
    oh.setStartDate(startDate);
    List<Date> endDate=new ArrayList<Date>();
    oh.setEndDate(endDate);
    return oh;
}

2 Comments

Correct.. it is possible to create Bean class But is Their any other alternate way,i mean with in the class only ....Thanks for your replay..
The class can be a nested one and be within the same class.
2

You can

  • provide one or both lists as argument(s) to populate.
  • have two lists which are fields of the instance of the method and access these via getters.
  • return an array of two lists or a list of lists.
  • return a custom type which wrap the two lists. I wouldn't use getters, just makes the fields public.
  • have a single list of intervals

I believe the last is the best solution.

public class Sample {
  public List<Interval> returnListOfStartToEndDates() {
      List<Interval> intervals=new ArrayList<>();

      return intervals;
   }
}

Joda-time has an Interval class which is more efficient than creating two Date objects, but you can also create your own.

Comments

1

You could create a custom class like this

TimeSpan(ArrayList<Date> startDate, ArrayList<Date> endDate)

and return that Object.

Comments

1

Directly, no, unless you count returning an array of two Lists, or a List of Lists with the stated understanding to the caller that it would contain exactly two entries and what value each one would represent.

Other than that, you could always write a Pair class and use that, which would be better because it removes the dependence on that assumption about the dimension of the return value, and you will find that a Pair type comes in handy in many, many situations.

Comments

1

Instead you can use Map like the following,

public Map<String, List<Date>> getBothDates() {
  List<Date> startDate = new ArrayList<Date>();
  List<Date> endDate = new ArrayList<Date>();
  Map<String, List<Date>> bothDates = new HashMap<String, List<Date>>();
  bothDates.put("startDate", startDate);
  bothDates.put("endDate", endDate);    
  return bothDates;
}

To fetch both dates simple iterate the map,

public void printBothDates() {
   Map<String, List<Date>> bothDates = getBothDates();
   for(Entry<String, List<Date>> entry : bothDates.entrySet()) {
     if(StringUtils.equalsIgnoreCase("startDate", entry.getKey())) {
       for(Date d : entry.getValue()) {
          System.out.println("startDate---"+d);
       }
     } else if(StringUtils.equalsIgnoreCase("endDate", entry.getKey())) {
       for(Date d : entry.getValue()) {
          System.out.println("endDate---"+d);
       }
     }
   }
}

Comments

0
import java.util.ArrayList;

public class MyClass {
    public ArrayList<ArrayList> TwoArrayList() {

        ArrayList cars = new ArrayList();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");

        ArrayList fruits = new ArrayList();
        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Banana");
        fruits.add("Orange");

        ArrayList <ArrayList> twoArrayList = new ArrayList <ArrayList>();
        twoArrayList.add(cars);
        twoArrayList.add(fruits);

        return twoArrayList;

    }

    public static void main(String[] args) {
       MyClass obj = new MyClass();

        ArrayList <ArrayList> Arlist= obj.TwoArrayList();
        
        System.out.println(Arlist.get(0));/**FIRST ARRAY LIST*/
        
        System.out.println(Arlist.get(0).get(0));/**FIRST ARRAY LIST FIRST Element*/

        System.out.println(Arlist.get(1));/**SECOND ARRAY LIST*/
        
        System.out.println(Arlist.get(1).get(0));/**SECOND ARRAY LIST FIRST Element*/
    }  
}  

OUTPUT IS HERE

enter image description here

Comments

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.