是否可以在 Java 中的一种方法中返回两个 ArrayList 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13878263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:18:11  来源:igfitidea点击:

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

javacollectionsreturn

提问by Java Developer

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 StartDateand endDateinto Database here these are Two different Columns

我将此方法调用到我的服务类中,在存储StartDateendDate数据库中,这里有两个不同的列

**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

回答by Juvanis

You cannotreturn separate structures via one method call, but you can return a compositeof 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 startDateby calling twoLists.get(0)and similarly endDatewith twoLists.get(1)

您可以get()稍后使用方法来检索这些列表。假设你打了一个电话,List<List<Date>> twoLists = returnTwoArrayList();然后你可以startDate通过调用twoLists.get(0)和类似的方式endDate获得twoLists.get(1)

回答by Subhrajyoti Majumder

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

回答by Peter Lawrey

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
  • 提供一个或两个列表作为要填充的参数。
  • 有两个列表,它们是方法实例的字段,并通过 getter 访问它们。
  • 返回一个包含两个列表的数组或一个列表列表。
  • 返回包装两个列表的自定义类型。我不会使用 getter,只是公开这些字段。
  • 有一个间隔列表

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 Intervalclass which is more efficient than creating two Date objects, but you can also create your own.

Joda-time 有一个Interval类,它比创建两个 Date 对象更有效,但您也可以创建自己的。

回答by Harsha Kasturi

Instead you can use Map like the following,

相反,您可以使用 Map 如下所示,

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

回答by Jeroen Vannevel

You could create a custom class like this

你可以像这样创建一个自定义类

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

and return that Object.

并返回该对象。

回答by Omaha

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

直接,不,除非你返回两个数组ListS,或者ListLists的既定认识到主叫方,这将正好包含两个条目,什么珍视每一个将代表。

Other than that, you could always write a Pairclass 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 Pairtype comes in handy in many, many situations.

除此之外,您总是可以编写一个Pair类并使用它,这会更好,因为它消除了对返回值维度的假设的依赖,并且您会发现Pair类型在很多很多情况下都派上用场。