如何从相同的方法返回2个数组列表

2022-09-04 23:51:58

我有一个如下方法,此方法包含两个数组列表(“eventList”和“emailList”)。

List<EmailUID> emailid=SharedEvent.getEmailUid(filter, uri, exchWD, EmailShare);

public static List<EmailUID> getEmailUid(Filter filter, String uri, NexWebDav exchWD,
            List<String> emailShare)
List eventsToday = null;
List<EmailUID> arrayList = new ArrayList<EmailUID>();
List<EmailUID> emailList = new ArrayList<EmailUID>();
List<EmailUID> eventList = new ArrayList<EmailUID>();

        for (String email : emailShare) {
            String uris = uri + email + "/events/";
            InputStream stream = null;
            try {
                stream = exchWD.get(uris);
                BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                CalendarBuilder builder = new CalendarBuilder();
                net.fortuna.ical4j.model.Calendar calendar = builder.build(br);

                //eventsToday.add(email);

                eventsToday = (List<?>) filter.filter(calendar.getComponents(Component.VEVENT));
                arrayList=getEmailUID(eventsToday,email);
                emailList.addAll(arrayList);//
                eventList.addAll(eventsToday);//

            } catch (ParserException e) {
                LOGGER.error("Parse Exception"+e.getMessage());
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }
    //return eventList;
    return emailList;
    }

如何获取列表“事件列表”和“电子邮件列表”


答案 1

不可能从单个函数中两个,但您可以包装在新的或并且可以返回两个 ArrayList。return statementMapList

public Map<String,List<EmailUID>> getList()
  List<EmailUID> emailList = new ArrayList<EmailUID>();
  List<EmailUID> eventList = new ArrayList<EmailUID>();
  ...
  Map<String,List<EmailUID>> map =new HashMap();
  map.put("emailList",emailList);
  map.put("eventList",eventList);
  return map;
}

答案 2

好吧,如果你真的必须这样做,你可以把它们包装成一个对象,只有2个List字段。

或者,您可以返回 2 个列表的 Map,每个列表都有一个唯一的键。


推荐