how to condense several foreach into a single for-each?

alexx :

I want to concatenate each of these arrays, and then just iterate over the resulting collection.

        String[] type = {"school", "home"};
        String[] place = {"tokyo ", " New York"};
        String[] date = {"Sep", "Feb"};


        for(String name: type) {
            Assert.assertFalse(isValid(name));
        }
        for(String name: place) {
            Assert.assertFalse(isValid(name));
        }
        for(String name: date) {
            Assert.assertFalse(isValid(name));
        }

Pshemo :

Not necessarily for-each you showed but maybe you would be interested in something like:

Stream.of(type, place, date) //Stream<String[]> example [ [a,b], [c,d] ]
      .flatMap(Stream::of)   //Stream<String>   example [ a, b, c, d ]
      .forEach(name -> Assert.assertFalse(isValid(name));

which is similar to

for (String[] arr : Arrays.asList(type,place,date)){
    for(String name : arr){
        Assert.assertFalse(isValid(name)
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309064&siteId=1