Is it Possible to Return Two ArrayList values in One method in java?

不想你离开。 提交于 2019-12-22 08:27:02

问题


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????
   }
}

Edit:1

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

回答1:


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)




回答2:


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;
}



回答3:


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.




回答4:


You could create a custom class like this

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

and return that Object.




回答5:


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.




回答6:


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);
       }
     }
   }
}


来源:https://stackoverflow.com/questions/13878263/is-it-possible-to-return-two-arraylist-values-in-one-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!