0

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);
    }
dimo414
  • 47,227
  • 18
  • 148
  • 244
gSingh
  • 57
  • 1
  • 14
  • 1
    Why do you have to type cast the result back to `List`? – Rohit Jain Apr 08 '14 at 05:37
  • @Rohit so what sud i do to get back the List again ? can you please show me as example – gSingh Apr 08 '14 at 05:39
  • and typecasting causing any performance issues ?? – gSingh Apr 08 '14 at 05:39
  • You need to add more details. What is the type of `listofuserdetailsperAccount`? And why are you using raw type instead of parameterized type? Typecasting rather depicts design issues. – Rohit Jain Apr 08 '14 at 05:40
  • listofuserdetailsperAccount is nothing but a object of ArrayList . something like this List listofuserdetailsperAccount = new ArrayList(); and than something like this ---- listofuserdetailsperAccount .add(List1(String)); listofuserdetailsperAccount .add(List2(String)); – gSingh Apr 08 '14 at 05:44

3 Answers3

3

I'll go with for each loop

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

}
Yogesh Patil
  • 908
  • 4
  • 14
0

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.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • so are you saying if i will use type specific list than i can do something like this in for loop for(int i=0;i<=listofuserdetailsperAccount.size();i++){ List list= listofuserdetailsperAccount.get(0); } – gSingh Apr 08 '14 at 05:47
  • `List list= listofuserdetailsperAccount.get(0); ` where E is type of list – Subhrajyoti Majumder Apr 08 '14 at 05:48
  • Well do not compromise your design to avoid it, that is a overhead in runtime, because the two type must be checked and in case that casting is not feasible, JVM must throw a ClassCastException. – Subhrajyoti Majumder Apr 08 '14 at 05:56
0

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 :)
Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Thanks dimo..you explained very well and kept everything together...Thanks ! – gSingh Apr 08 '14 at 05:54
  • so while initializing i am doing something like this now, can you please let me know if this is correct : List> listofAccountdetailsLoggedinUser=null; listofAccountdetailsLoggedinUser=new ArrayList>(); OR i sud not put it as null and directly create a object like this List> listofAccountdetailsLoggedinUser=new ArrayList>(); – gSingh Apr 08 '14 at 06:02
  • @gSingh Note you can wrap `code` in comments with backticks `\`` - it makes it easier to read. I've updated my answer to address your comment. – dimo414 Apr 08 '14 at 06:49