gSingh
gSingh

Reputation: 67

List of list iteration in code below -- which is better approach?

I am iterating over a List of Lists. In my code listofuserdetailsperAccount is List<List>. I am considering the two methods below, please let me know which way is correct, more efficient and should be followed in java coding.

Way 1-----

for(int i=0;i<=listofuserdetailsperAccount.size();i++){
    List list=(List) listofuserdetailsperAccount.get(0);
}

Way 2---

for(int i=0;i<=listofuserdetailsperAccount.size();i++){
        List list= new ArrayList();
             list=(List) listofuserdetailsperAccount.get(0);
    }

Upvotes: 0

Views: 52

Answers (3)

dimo414
dimo414

Reputation: 48814

You have a couple of problems here, with both proposed solutions.

  1. Your List<List> listofuserdetailsperAccount object is not properly typed, as the inner List is a raw type, something to be avoided. Assuming your inner list holds UserDetail objects, your list of lists should be of type List<List<UserDetail>>.
  2. You don't use the for-each loop syntax to iterate over a List, which is Iterable.

    for(List<UserDetail> innerList : listofuserdetailsperAccount)
    
  3. In Way 2 you initialize List to a new ArrayList (which is a raw type, it should be new ArrayList<>() if you needed this) and then promptly overwrite this value with the contents of your outer list. This means you ask Java to construct a new object that is then immediately cleaned up by the garbage collector, unused. This is wasteful and unnecessary.

In summary, you likely want to do:

List<List<UserDetail>> listofuserdetailsperAccount = // initialize your list

for(List<userDetail> innerList : listofuserdetailsperAccount) {
  // do work with your innerList now
}

You commented (tidied up):

So while initializing I am doing something like this now, can you please let me know if this is correct:

List<List<String>> listofAccountdetailsLoggedinUser = null;
listofAccountdetailsLoggedinUser = new ArrayList<List<String>>(); 

OR I should not put it as null and directly create an object like this:

List<List<String>> listofAccountdetailsLoggedinUser = 
    new ArrayList<List<String>>();
  1. That is the right track, but you do not need to initialize the variable to null. It doesn't hurt anything, since it doesn't construct an unnecessary object, but there's no reason to - you can declare and initialize the variable in one line, like you do in your second example.
  2. Additionally, you don't need to specify the type of the ArrayList on the right hand side, simply use the diamond operator <>, like so:

    List<List<String>> listofAccountdetailsLoggedinUser = new ArrayList<>();
    
  3. Also, consider a shorter variable name, there's no need to use such a long one, and it's no fun to type :)

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Way 1 is better approach than Way 2. In Way 2 List list= new ArrayList(); it will create a extra ArrayList object which does not have any use, which will cause memory consumption for sometime.

And it is also recommended use type specific List<E> so that you dont cast at runtime it will be typesafe.

for(List<E> list : listOfUserDetailsPerAccount){
   ...
}

In Java 5 and above use for-each.

Upvotes: 0

Yogesh Patil
Yogesh Patil

Reputation: 888

I'll go with for each loop

for( List userDetailsPerAccount : listOfUserDetailsPerAccount ) {
    //anything you want to do with userDetailsPerAccount

}

Upvotes: 3

Related Questions