Is it possible to return two ArrayList values ​​in one method in java?

Is there a 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???? } } Edit:1 

I call this method in the My Service class and in this store StartDate and endDate to the database there 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 
+4
source share
6 answers

You cannot return individual structures through a single method call, but you can return a compound . 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 the get() method to get these lists later. Suppose you made a call as List<List<Date>> twoLists = returnTwoArrayList(); , then you can get startDate by calling twoLists.get(0) and similarly endDate using twoLists.get(1)

+13
source

No, you cannot return two values ​​from a method.

The best way is to create your own class with two fields and return this 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; } 
+6
source

You can

  • specify one or both of the lists as argument (s) to populate.
  • have two lists, which are the fields of the method instance and access to them through getters.
  • returns an array of two lists or a list of lists.
  • returns a non-standard type that carries two lists. I would not use getters, I just made the fields public.
  • has one interval list

I believe the latter is the best solution.

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

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

+2
source

You can create your own class like this

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

and return this object.

+1
source

Directly, no, unless you count the return array of two List s or List of List with the specified notion of the caller that it will contain exactly two records and what value each of them will represent.

In addition, you can always write a Pair class and use it, which would be better because it removes the dependency on this assumption regarding the size of the return value, and you will find that the Pair type is useful in many, many situations.

+1
source

Instead, you can use Map, as shown below,

 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 get both dates, a simple iteration of 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); } } } } 
+1
source

All Articles