helen
helen

Reputation: 587

how to check if the lists in a list have the same length

I have a list of lists in Java. Here is the code:

List<List<Integer>> myList = new ArrayList<>();
myList.add(new ArrayList<Integer>());
myList.add(new ArrayList<Integer>());
myList.add(new ArrayList<Integer>());

myList.get(0).add(1);
myList.get(0).add(2);
myList.get(0).add(3);
myList.get(1).add(4);
myList.get(1).add(5);
myList.get(1).add(6);
myList.get(2).add(7);
myList.get(2).add(8);
myList.get(2).add(9);

How to check if the lists that exist in myList have the same length? I know that I can check the length of each list by something like myList.get(0).size(), so what is an efficient and short way to check if all these lists have the same length (instead of checking one by one)?

Upvotes: 2

Views: 4473

Answers (6)

M2E67
M2E67

Reputation: 970

listOfList.stream().map(list -> list.size()).distinct().count() == 1

Upvotes: 1

renatodamas
renatodamas

Reputation: 19425

I propose the following:

myList.stream()
    .map(e -> e.size())
    .distinct()
    .limit(2)  // optimization
    .count() == 1;

Basically checking if there are any 2 lists with a different size.

Upvotes: 0

Fabian Brand&#227;o
Fabian Brand&#227;o

Reputation: 128

If you use Java 8, you can use lambda expressions to simplify your application. Look:

if (myList.stream().filter(integers -> integers.size() == myList.get(0).size()).count() == myList.size()) {
    System.out.println("The sub-lists have the same size");
}

Upvotes: 1

Raven221221221
Raven221221221

Reputation: 142

You cannot hope to say that n lists have the same length without visiting them. You have to go one-by-one. davidxxx's answer shows a really nice way with streams but visits all the lists anyway. The naive way may be

public static boolean sameLength(List<List> lists){
    if(lists.get(0) == null){ //EDIT: as davidxxx pointed out this if is superfluous
        return true;
    }
    int len = lists.get(0).size();
    for (List list : lists) {
        if(list.size() != len){
            return false;
        }
    }
    return true;
}

My IDE even suggests using functional operators with the above code like davidxxx said.

Upvotes: 2

Sudhir Ojha
Sudhir Ojha

Reputation: 3305

You can also check using a counter variable, iterate your myList and get the size of sublist. If size of myList== sublist then assign true else assign false like following:

public static boolean isSameLength(List<List<Integer>> myList) {
    boolean isSame = false;
    for (List<Integer> sublist : myList) {
        if (sublist.size() == myList.size()) {
            isSame = true;
        } else {
            isSame = false;
        }
    }
    return isSame;
}

Upvotes: 0

davidxxx
davidxxx

Reputation: 131346

You could use Stream.allMatch() by matching any contained list size (for example the first one) with all other contained lists :

boolean isSameLength = 
myList.stream()
      .allMatch(l -> l.size() == myList.get(0).size())

It makes the first comparison helpless as it compares the same contained list but it is more readable that :

boolean isSameLength = 
myList.stream()
      .skip(1)
      .allMatch(l -> l.size() == myList.get(0).size())

Upvotes: 9

Related Questions