Swift SequenceとJava Stream filterメソッド

filterメソッド

Example 1

Java

IntStream stream = IntStream.range(0, 10)
                            .filter(x -> x % 2 == 0);
stream.forEach(System.out::println);

Swift

var numbers = Range(0...9).filter({x in x % 2 == 0})
print(numbers)

Example 2

Java

List<String> words = List.of("ABCDE", "ABC", "ABCDEFGHI", "ABCDEF", "A");
words.stream()
     .filter(word -> word.length() > 5)
     .forEach(System.out::println);

Swift

var words = ["ABCDE", "ABC", "ABCDEFGHI", "ABCDEF", "A"]
words.filter({word in word.count > 5})
     .forEach({word in print(word)})

Java,Swift

Posted by shi-n