0

I have thousands of markdown templates files mixed in with markdown files that have actual content. The following python snippet checks each of the markdown files files that match all of the following conditions. If true, I know that it's a template, which gets moved into a templates folder in the same directory.

I have been asked to convert this code over to fit into some existing java code, is there a java equivalent for python's all() or do I need to take a different direction. I am not that familiar with java and my searches have not turned up anything but the startswith() function in the apache.commons.

for i, fpath in enumerate(md_list):
        with open(fpath) as f:
            result = all(line.startswith('#') or line.startswith('[') or
                         line.startswith('|') or line.startswith('(') or
                         line.isspace() for line in f);
            if result is True: do something
L0ngSh0t
  • 361
  • 2
  • 9

2 Answers2

1

You can define your method, like this:

public static boolean areAllTrue(boolean[] array)
{
    for(boolean b : array) if(!b) return false;
    return true;
}

Something using set:

Set<Boolean> flags = new HashSet<Boolean>(myArray);
flags.contains(false);

Or

 Arrays.asList(myArray).contains(false)

Clearly you need first of all create your boolean array doing your checks.

Nikaido
  • 4,443
  • 5
  • 30
  • 47
1

If you use Java 8 there is a allMatch function

lines.stream().allMatch(line -> condition1(line) && condition2(line)...)
Querenker
  • 2,242
  • 1
  • 18
  • 29
  • I like this but most of the systems where we will be sending the code are still on Java 7. Thanks for the reply. – L0ngSh0t Mar 18 '16 at 14:35