import io.vavr.collection.List;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
numbers.map(String::valueOf).forEach(System.out::println);
int sum = numbers.filter(n -> n % 2 == 0).sum().intValue();
System.out.println("Sum of even numbers: " + sum);
List<List<Integer>> nestedNumbers = List.of(
List.of(1, 2),
List.of(3, 4),
List.of(5, 6)
);
List<Integer> flattenedList = nestedNumbers.flatMap(list -> list);
System.out.println("Flattened list: " + flattenedList);
}
}
import io.vavr.control.Try;
public class ExceptionHandlingExample {
public static void main(String[] args) {
String input = "42";
Try<Integer> result = Try.of(() -> Integer.parseInt(input))
.map(i -> i * 2)
.recover(ex -> {
System.err.println("An error occurred: " + ex.getMessage());
return 0;
});
System.out.println("Result: " + result.get());
}
}