Java: how to check boolean conditions in two-dimensional lists / arrays?

Given a tabular data structure, for example. arraylist arraylists (2D array or some other repeating data structure), what would be the cleanest way to validate elements against specific rules?

For example, we give the following data:

[true, false, false, true]
[false, false, false, false]
[true, false, false, false]

How can I ensure that any of the three conditions is met:

  • all elements in all rows true
  • all elements in all rows false
  • If the string contains mixed values, then only the first element can be true, otherwise the check should fail.

For example, the above data should not be verified due to the value truein position [0, 3].

UPDATE: -, Java 8 myBoolArrayList.stream().allMatch(Boolean::booleanValue); myBoolArrayList.stream().noneMatch(Boolean::booleanValue); - , , .

+4
3

, , , :

all-true all-false, , " () ", , !booleanList.contains(!booleanList.get(0)). theres :

, , .

: false, , , . :

  • false, true
  • false, , false
  • , false,

, , , , . 1 :

  • true,
  • false, , , false
  • , , ,

,

list.size()<2 ||
     list.get(1)? // has a TRUE at a position other than the first
     !list.contains(false): // all are TRUE
     !list.subList(1, list.size()).contains(true); // all except the first are FALSE

API ,

  • !list.contains(x) list.stream().noneMatch(Predicate.isEqual(x))
  • !list.subList(1, list.size()).contains(x) list.stream().skip(1).noneMatch(Predicate.isEqual(x)),

Stream API.

, a List List s, Stream API, , :

static boolean validate(List<List<Boolean>> list2D) {
    return list2D.stream().allMatch(list-> list.size()<2 ||
        list.get(1)? !list.contains(false): !list.subList(1, list.size()).contains(true)
    );
}
+2

Guava , :

[: ... , , .

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;

public class ValidationTest {

    @Test
    public void test() {
        // Here is the initial table, you can play with it.
        List<List<Boolean>> list = new ArrayList<List<Boolean>>() {{
            add(new ArrayList<Boolean>() {{
                add(false);
                add(false);
                add(false);
                add(false);
            }});
            add(new ArrayList<Boolean>() {{
                add(true);
                add(false);
                add(false);
                add(false);
            }});
            add(new ArrayList<Boolean>() {{
                add(true);
                add(true);
                add(true);
                add(true);
            }});
        }};
        boolean checkBooleanRule = this.checkBooleanRule(list);
        Assert.assertTrue("The table is invalid", checkBooleanRule);
    }

    private boolean checkRow(List<Boolean> row, final boolean value) {
        // If checking everything is true/false, let just find if there is a False = !value
        return row.contains(!value);
    }

    private boolean checkBooleanRule(final List<List<Boolean>> inputList) {
        // Gets the row that brake the rule!
        Optional<List<Boolean>> invalidRow = Iterables.tryFind(inputList, new Predicate<List<Boolean>>() {
            @Override
            public boolean apply(List<Boolean> inputRow) {
                if (!checkRow(inputRow, true))
                    return false;
                if (!checkRow(inputRow, false))
                    return false;
                return (inputRow.get(0) ? checkRow(inputRow.subList(1, inputRow.size()), false) : true);
            }
        });
        // If Present, then, the Table is invalid! == There is a row with a false!
        return !invalidRow.isPresent();
    }
}
+1

This is one way to do this using Java Lambda expressions. Link What is the best way to filter your Java collection?

Update thanks to comments

import java.util.ArrayList;

import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {

        ArrayList<Boolean> array = new ArrayList<Boolean>();
        array.add(true);
        array.add(true);
        array.add(true);
        array.add(false);

        //the simplest to read way 
        Stream<Boolean> arrayChecker = array.stream().filter(a -> a == true);

       //Checks if the stream filter is equal to the array size. If so then all true 
        System.out.println(arrayChecker.count() == array.size() );

        //the better way thanks to @ccpizza
        Stream<Boolean> arrayChecker = array.stream().filter(Boolean::booleanValue);

        //Even better way
        array.stream().allMatch(Boolean::booleanValue);

       //so basic I missed it. Thanks to @Holger
       System.out.println(! array.contains(false));
    }
}
0
source

All Articles