14

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"

1
  • Thank you every one for the answers Commented Oct 18, 2012 at 5:34

7 Answers 7

28

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;
}
Sign up to request clarification or add additional context in comments.

Comments

16

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.

Comments

5

You can make a class with two list as its member and then can return this class object with your lists.

Comments

3

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

}

Comments

3

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.

Comments

2

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.

Comments

2

Create a list of List<EmailUID> objects, i.e. 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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.