Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i return 2 ArrayList From same method

Tags:

java

arraylist

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"

like image 792
VICKY-TSC Avatar asked Oct 18 '12 05:10

VICKY-TSC


2 Answers

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;
}
like image 128
Subhrajyoti Majumder Avatar answered Sep 29 '22 13:09

Subhrajyoti Majumder


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.

like image 40
Scorpio Avatar answered Sep 29 '22 14:09

Scorpio