v0.11.0 — last minor before 1.0

vavr

Turns Java upside down.

Object-functional library providing immutable collections, pattern matching, and functional control structures for Java 8+.

Example.java
// Immutable, functional collections
List<String> names = persons
    .filter(p -> p.age > 12)
    .map(Person::getName);

// Seamless Java interop
java.util.List<String> javaNames =
    names.asJava();
<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.11.0</version>
</dependency>
implementation 'io.vavr:vavr:0.11.0'

Why Vavr?

Write expressive, type-safe functional code in Java.

Collections

Immutable Collections

No more stream().collect() pipelines. Operate on values directly.

List<String> names = persons
    .filter(p -> p.age > 12)
    .map(Person::getName);
Control Flow

Pattern Matching

Decompose values structurally.

Number num = Match(opt).of(
  Case($Some($()), v -> 1.0d),
  Case($None(), 0)
);
Error Handling

Try Monad

Functional success and failure.

R result = Try(() -> mightFail())
  .recover(x -> ...)
  .getOrElse(defaultValue);
Expressions

For Comprehension

Lazy evaluated collection expressions that yield values.

Iterator<String> result =
    For(persons.filter(Person::hasAddress), p ->
        For(p.addresses).yield(a ->
            p.name + "," + a.street
        )
    );
Types

Option

Serializable alternative to Optional.

class Person implements Serializable {
  final Option<String> title;
  final String name;
}
Interop

Java Compatible

Seamlessly convert back and forth.

java.util.List<Integer> result =
  iterator.map(String::length)
    .asJava();