简体   繁体   中英

How can i return 2 ArrayList From same method

I have a method as below,this method contains two arraylist ("eventList" and "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;
    }

How to get both the list "eventList" and "emailList"

It is not possible two return statement from single function but you can wrap in new Map or List and can return two ArrayList.

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

Well if you really have to, you could wrap them up into an object, that just has 2 List fields.

Alternativly you could return a Map of the 2 Lists, with an unique key for each.

您可以创建一个具有两个列表作为其成员的类,然后可以使用您的列表返回此类对象。

Create a list of List<EmailUID> objects, ie List<List<EmailUID>> . Add your lists ( eventList and emailList ) to this super list . And return this super list.

To access evenlist later on, use this superList.get(0); //refers to eventlist superList.get(0); //refers to eventlist

(Supposing superList was returned from your method and evenlist is the first item in that list):

There is no easy way to do this in Java. You can create a small wrapper class to contain both elements, you can return a list of both lists, a set of both lists, or a Map> containing 2 entries with both lists.

You can just return them in an array.

public static List[] getEmailUid(Filter filter, String uri, NexWebDav exchWD,
           List<String> emailShare) {
    //
    // Method body 
    //
    return new List[] { eventList, emailList };

}

You'll need to change the signature of your method. Change the return type to

ArrayList<String>[] 

OR

ArrayList<ArrayList<String>>

Based on the return type selected, edit the code.

If array is selected as return type, before return add following lines:

ArrayList<String>[] arr = new ArrayList<String>[2];
arr[0] = eventList;
arr[1] = emailList;
return arr;

Similarly, you can add code for 2nd option. Let me know if you need further help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM