function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
King KooKing Koo 

Fill in the blank: a list can contain up to ___ levels of nested collections inside it.

Hi guys

 

Reading the Apex Develpers Guide.  It says "A list can contain up to four levels of nested collections inside it." (p43 in the pdf file for Winter 13 version).   Since I wanted to be 100% sure, I went to the Developer Console and ran this code:

 

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

 

To me, that's 4 levels.  And as expected, the code compiled.

 

I added one more level, so my code became this, and I expected it to crash.

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

 

No, it didn't crash.

 

I finally added one more level:

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

List<List<List<List<List<List<Integer>>>>>> f = new List<List<List<List<List<List<Integer>>>>>>();
f.add(d);

 

And I finally got an error that said:

line -1, column -1: Nested type exceeds maximum level: 5

 

That is not quite what the book said.

 

I then googled a bit and realized according to Database.com page (here at http://docs.database.com/dbcom/en-us/db_apex/langCon_apex_collections_lists.htm?version=178.15), the list can contain up to 5 levels of nested collections.  There seems to be some inconsistency.  

 

Can anyone verify?  Has something been updated to Apex lately?

 

Thanks a lot.

King

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Katia HageKatia Hage

You made a good observation. The reason why one of the Database.com links shows the value of four (and not five) is because we updated the documentation and the Database.com link is showing an older version of the doc (pointing to version 178). This doc was updated in the version after that, and hence the second Database.com link, which points to a new version, shows the update made to four.

 

There is no difference in content between Database.com and Force.com docs. In fact, if you look at an older version of the Force.com Apex Dev Guide, you will see this change too:

 

(Old version)

http://www.salesforce.com/us/developer/docs/apexcode250/Content/langCon_apex_collections_lists.htm

 

(New version)

http://www.salesforce.com/us/developer/docs/apexcode260/Content/langCon_apex_collections_lists.htm

 

You can access old versions of the Force.com docs on http://wiki.developerforce.com/page/Documentation by clicking "Earlier Reference Documentation" towards the bottom of the page.

 

 

All Answers

Katia HageKatia Hage

Hi there,

 

The sentence from the doc ("A list can contain up to four levels of nested collections inside it.") means that there can be 4 lists inside a list, which gives you a total of 5 allowed. The element of confusion is the "nested" term. It didn't say you can have a total of 4 lists, but 4 nested lists inside a list, so a total of 5.

 

This is consistent with your tests, which show that 5 total is allowed but not 6.

 

The doc has been updated recently. The link you have points to a previous version of the doc, which said you can have up to 5 nested levels (-> total of 6), which was incorrect.

 

I guess it depends on whether you count the original parent list as nested.

King KooKing Koo

BTW

 

I noticed today these two links will bring you different information, one says 4, the other says 5.

 

Not sure if anyone knows about database.com, and how that impacts force.com.

 

http://docs.database.com/dbcom/en-us/db_apex/langCon_apex_collections_lists.htm?version=180.0

 

and

 

http://docs.database.com/dbcom/en-us/db_apex/langCon_apex_collections_lists.htm?version=178.15

 

Thanks

K.

King KooKing Koo

Hi Katia

 

Sorry I just noticed you've sent me a reply before I sent out my last reply.

 

There is still inconsistency.  In the pdf book, (Winter 13), it read:

A list can contain up to four levels of nested collections inside it.

 

And in the database.com documentation (version 178.15) it read:

A list can only contain up to five levels of nested collections inside it.

 

which, in version 180.0, was changed to

A list can contain up to four levels of nested collections inside it.

 

 

 

So my confusion is the inconsistency between the force.com Apex Deveoper's Guide documentation versus Database.com version 178.15.

 

By dissecting the sentence "a list can contain up to 4 levels of nested collections inside it" more carefully, however, I agree with you.

 

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();

This varible e is a list that has 4 lists (4 collctions) in it, and that's why it doesn't crash.

 

Thanks

King

 

Katia HageKatia Hage

You made a good observation. The reason why one of the Database.com links shows the value of four (and not five) is because we updated the documentation and the Database.com link is showing an older version of the doc (pointing to version 178). This doc was updated in the version after that, and hence the second Database.com link, which points to a new version, shows the update made to four.

 

There is no difference in content between Database.com and Force.com docs. In fact, if you look at an older version of the Force.com Apex Dev Guide, you will see this change too:

 

(Old version)

http://www.salesforce.com/us/developer/docs/apexcode250/Content/langCon_apex_collections_lists.htm

 

(New version)

http://www.salesforce.com/us/developer/docs/apexcode260/Content/langCon_apex_collections_lists.htm

 

You can access old versions of the Force.com docs on http://wiki.developerforce.com/page/Documentation by clicking "Earlier Reference Documentation" towards the bottom of the page.

 

 

This was selected as the best answer
Katia HageKatia Hage

I wanted to correct my earlier statement about Database.com docs. There is no difference in content between Database.com and Force.com docs, except that the Database.com docs exclude features that aren't in Database.com.

King KooKing Koo

I LOVE this discussion board.

 

Katia, so I'm going to take it that the correct sentence should be

 

A list can contain up to four levels of nested collections inside it.

 

That is reflected by this line of code:

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();

 

where e is a List that contains 4 nested lists in it.  And it's permissible.

 

King

Katia HageKatia Hage

Yep. This is correct.