Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

list

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.

like image 949
Mr Morgan Avatar asked Jul 27 '11 15:07

Mr Morgan


People also ask

Can you get the length of a list in Java?

You can use the size() method of java. util. ArrayList to find the length or size of ArrayList in Java. The size() method returns an integer equal to a number of elements present in the array list.

How do you count a list in Java?

The size() method of the List interface in Java is used to get the number of elements in this list. That is, this method returns the count of elements present in this list container. Parameters: This method does not take any parameters. Return Value: This method returns the number of elements in this list.

Can you have a list of lists in Java?

Using List.You can declare a List of Lists in Java, using the following syntax. It uses the new operator to instantiate the list by allocating memory and returning a reference to that memory. To add elements to it, call the add() method.

How do I check the size of a list?

Object of any Python sequence data type including list uses a built-in function len() which returns its size i.e. number of elements in it. Built-in list class has a special method called __len__() which also returns size of list.


2 Answers

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 
like image 182
Jon Skeet Avatar answered Sep 19 '22 08:09

Jon Skeet


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);      } } 
like image 37
Ricardo Padua Soares Avatar answered Sep 18 '22 08:09

Ricardo Padua Soares