How do I get length of list of lists in Java?

JavaList

Java Problem Overview


If I have a List<List<String>> data in Java, I can get the length of the first list via code:

int lengthData = data.get(0).size();

But how do I get the number of lists in the structure without traversing the list of lists to find out?

Maybe I've been a bit unclear. I have the structure:

List<List<String>> data 

And I see that:

int i = data.size();

Will equal 1 because it is the root list. So what I want to know is how many sublists there are. Traversal of the structure like this:

for (List<String> l : data) {                     
     total ++;                
}

Only gives me a result of 1 which I find odd.

I have data of the form:

List 1 ==> 1, 2, 3, 4
List 2 ==> 3, 8. 9, 1

And so on where these are sublists of the root list.

Java Solutions


Solution 1 - Java

Just use

int listCount = data.size();

That tells you how many lists there are (assuming none are null). If you want to find out how many strings there are, you'll need to iterate:

int total = 0;
for (List<String> sublist : data) {
    // TODO: Null checking
    total += sublist.size();
}
// total is now the total number of strings

Solution 2 - Java

Java 8

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class HelloWorld{

     public static void main(String []args){
			List<List<String>> stringListList = new ArrayList<>();
			stringListList.add(Arrays.asList(new String[] {"(0,0)", "(0,1)"} ));
			stringListList.add(Arrays.asList(new String[] {"(1,0)", "(1,1)", "(1,2)"} ));
			stringListList.add(Arrays.asList(new String[] {"(2,0)", "(2,1)"} ));

			int count=stringListList.stream().mapToInt(i -> i.size()).sum();

		    System.out.println("stringListList count: "+count);
     }
}

Solution 3 - Java

import java.util.ArrayList;

public class TestClass {

public static void main(String[] args) {

	ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();
	ArrayList<String> List_1 = new ArrayList<String>();
	List_1.add("1");
	List_1.add("2");
	listOLists.add(List_1);

	ArrayList<String> List_2 = new ArrayList<String>();
	List_2.add("4");
	List_2.add("5");
	List_2.add("10");
	List_2.add("11");
	listOLists.add(List_2);
	for (int i = 0; i < listOLists.size(); i++) {
		System.out.print("list " + i + " :");
		for (int j = 0; j < listOLists.get(i).size(); j++) {
			System.out.print(listOLists.get(i).get(j) + " ;");
		}
		System.out.println();
	}

}

}

I hope this solution gives a better picture of list if lists

Solution 4 - Java

count of the contained lists in the outmost list

int count = data.size();

lambda to get the count of the contained inner lists

int count = data.stream().collect( summingInt(l -> l.size()) );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr MorganView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaRicardo Padua SoaresView Answer on Stackoverflow
Solution 3 - JavaAswin BahulayanView Answer on Stackoverflow
Solution 4 - JavaKaplanView Answer on Stackoverflow