VICKY-TSC
VICKY-TSC

Reputation: 415

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"

Upvotes: 14

Views: 56150

Answers (7)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

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

Upvotes: 28

Juvanis
Juvanis

Reputation: 25950

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):

Upvotes: 2

Azodious
Azodious

Reputation: 13872

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.

Upvotes: 3

rjb1290
rjb1290

Reputation: 101

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

}

Upvotes: 3

Ankit HTech
Ankit HTech

Reputation: 1893

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

Upvotes: 5

Cory Kendall
Cory Kendall

Reputation: 7304

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.

Upvotes: 2

Scorpio
Scorpio

Reputation: 2327

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.

Upvotes: 16

Related Questions