Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does Vavr offer that the JDK does not, for example Try, Either and persistent collections? Show how Try replaces a try-catch.

Vavr adds functional types the JDK lacks: Try, which wraps a computation that may throw so we can chain map and getOrElse instead of writing try-catch; Either, which holds either an error or a result without using exceptions; an Option with richer operations than Optional; and persistent immutable collections that share structure on every update. Try.of(() -> parse(text)).map(Value::normalize).getOrElse(Value.DEFAULT) replaces a try-catch block with one expression. Before adopting it, check the project's release status, and note that Java 21 now covers much of its pattern matching.

Why this question is asked

Java has borrowed many ideas from functional languages, but some common building blocks are still missing from the JDK. Vavr, previously called Javaslang, fills those gaps, and interviewers ask about it to see whether we understand the ideas, not just the library: treating failure as a value, and collections that never change. In this answer, we will rewrite a try-catch with Try, see how Either models business errors, compare Vavr's Option with Optional, look at persistent collections and pattern matching, and finish with what to check before adopting the library.

1A Quick Recap of the Problem Vavr Solves

In plain Java, a computation that can fail usually throws an exception. Exceptions are fine for unexpected errors, but they are awkward when failure is a normal outcome, such as parsing user input. The failure path jumps out of the expression, so the code becomes a block of statements with a variable declared before the try and assigned in two places.

Functional languages handle this by making the outcome a value. Instead of returning a result or throwing, the computation returns an object that contains either the result or the failure. We can then transform it with map, recover from it, or extract a value with a default, all as one expression. Vavr brings that style to Java.

2Replacing a Try-Catch With Try

Let us parse a text into a value, normalise it, and fall back to a default if parsing fails. First the classic version, then the Vavr one.

Java
// Classic try-catch
Value value;
try {
value = parse(text).normalize();
} catch (ParseException e) {
value = Value.DEFAULT;
}
// With Vavr
Value value = Try.of(() -> parse(text)) // Success(Value) or Failure(ParseException)
.map(Value::normalize) // runs only on Success
.getOrElse(Value.DEFAULT); // the value, or DEFAULT on Failure
Try.of(() -> Integer.parseInt("42")).map(n -> n * 2).getOrElse(-1); // 84
Try.of(() -> Integer.parseInt("x")).map(n -> n * 2).getOrElse(-1); // -1
Try.of(() -> Integer.parseInt("x")).isFailure(); // true

Try.of runs the lambda immediately and catches any exception it throws, returning a Failure that holds it; otherwise it returns a Success with the result. map applies a function only to a Success, and if that function itself throws, the result becomes a Failure. getOrElse ends the chain. So the whole fallback logic is one expression with no mutable variable. Notice also that checked exceptions are allowed inside Try.of, because its functional interface declares throws Throwable, which removes the usual pain of checked exceptions in lambdas.

There are richer operations too: recover maps a specific exception type to a value, onFailure logs, and toEither turns the result into an Either.

3Either: A Result or an Error Without Exceptions

Try always holds a Throwable on failure. For business outcomes, we often want a typed error instead, such as InsufficientFunds. Either<L, R> holds exactly one of two values: a Left, by convention the error, or a Right, the result. It is right-biased, meaning map and flatMap work on the Right side and pass a Left through unchanged.

Java
Either<PaymentError, Receipt> pay(Account a, Money m) {
if (a.balance().compareTo(m) < 0) return Either.left(new PaymentError.InsufficientFunds(a.id()));
return Either.right(new Receipt(a.id(), m));
}
pay(account, tenEuros)
.map(Receipt::id) // only if Right
.fold(err -> "failed: " + err, // Left -> "failed: InsufficientFunds[id=7]"
id -> "paid: " + id); // Right -> "paid: r-123"

The signature now tells every caller that payment can fail and how, which an unchecked exception does not. The caller is pushed to handle both cases with fold, instead of forgetting a catch. Option is Vavr's equivalent of Optional, with more operations, such as peek, toTry or toEither, and it is a proper collection-like type; Optional was deliberately kept small by the JDK designers.

4Persistent Collections That Share Structure

JDK collections are mutable by default, and List.copyOf or Collections.unmodifiableList only prevent changes; an unmodifiable view still reflects changes to the list underneath, and copying a large list to add one element costs time proportional to its size. Vavr's collections, such as io.vavr.collection.List, Vector and HashMap, are persistent: every update returns a new collection and leaves the old one unchanged, and the two share most of their internal structure.

Java
io.vavr.collection.List<Integer> a = io.vavr.collection.List.of(2, 3);
io.vavr.collection.List<Integer> b = a.prepend(1); // new list, shares the nodes of a
a; // List(2, 3), unchanged
b; // List(1, 2, 3)
b.tail() == a; // true: the same nodes, nothing was copied
Vavr type and the closest JDK feature
VavrClosest JDK featureWhat Vavr adds
Trytry-catchfailure as a value, chainable
Eithersealed interface with two recordsready-made, right-biased operations
OptionOptionalmore operations, collection-like
List, Vector, HashMapList.of, unmodifiable viewspersistent, structure sharing
Match APIswitch with patterns (Java 21)little now; JDK covers most uses
The JDK has closed some gaps, but Try and persistent collections still have no direct equivalent.

Notice the last line: prepending did not copy anything, because b is a new node pointing at the existing list. Vector and HashMap use trees with a high branching factor, so an update copies only a short path of small nodes. That makes immutable data cheap to update, which is the core idea behind functional data structures.

5Pattern Matching and the Project's Status

Vavr also has a pattern matching API, Match(x).of(Case($(1), "one"), Case($(), "other")), written years before Java had anything similar. Since Java 21, switch with record and type patterns covers most of these uses with compiler support and exhaustiveness checks over sealed types, so new code should prefer the language feature.

Finally, the project's status matters. Vavr's release activity has been uneven over the years, with a long gap while a 1.0 rewrite was discussed, and a small group of maintainers. Before adopting it, we should check recent releases, open issues and maintainers, and limit its use to the domain layer so it could be removed later. A library that changes how all our methods look is hard to remove if the project stalls.

6Follow-up questions

When would you still use a plain try-catch over Try?

When the failure is unexpected and should propagate, such as a database outage, or when the code is a simple sequence of statements where a catch is clearer. Try is best when failure is a normal outcome and we want to transform or default it inline.

Can you write Either without Vavr in modern Java?

Yes. A sealed interface Result<T> with two records, Ok<T>(T value) and Err<T>(Error error), plus a switch with patterns, gives the same type safety and exhaustiveness checks. We lose the built-in map and flatMap, but those are a few lines to write once.

Are Vavr collections compatible with JDK collections?

Not directly; they implement Vavr's own interfaces. They offer conversion methods such as toJavaList and asJava, but every boundary with JDK APIs, Spring or Jackson needs a conversion or an extra Jackson module, which is a cost to weigh.

7Conclusion

In this answer, we saw that Vavr adds Try, which turns a computation that may throw into a value we can chain, as in Try.of(() -> parse(text)).map(Value::normalize).getOrElse(Value.DEFAULT); Either, which models a typed error or a result without exceptions; an Option richer than Optional; and persistent collections that share structure on update. Java 21 patterns now cover most of Vavr's pattern matching, and the project's uneven release activity is worth checking before adoption.

What to say in the interviewAbout 80 seconds aloud

Open with

Vavr gives Java a few functional building blocks the JDK still lacks. Try wraps a computation that may throw and turns the outcome into a value, Success or Failure.

Then, if they want more

So instead of declaring a variable, assigning it in a try block and again in the catch, I can write Try.of(() -> parse(text)).map(Value::normalize).getOrElse(Value.DEFAULT) as one expression; map only runs on success, and the lambda may even throw checked exceptions.

Either models a result or a typed error, Left for the error and Right for the result, and it is right-biased, so map and flatMap work on the success side and fold handles both. That makes expected business failures visible in the signature. Option is like Optional but with more operations.

Its persistent collections, List, Vector and HashMap, return a new collection on every update and share structure with the old one, so prepending to a list copies nothing, unlike JDK unmodifiable views which only block changes. It also had a pattern matching API long before Java, but since Java 21 I would use switch with record patterns instead.

Before adopting it, I would check the project's recent release activity, because it has been uneven, and I would keep it in the domain layer so it stays removable.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does Vavr offer that the JDK does not, for example Try, Either and persistent...": https://scrivio-interview-library.netlify.app/java/utility-libraries/f508333e00/