求含Java不可变列表实现及createAndAdd方法的开源库
Perfect scenario—you want immutable lists where adding an element returns a new immutable list instead of modifying the original, which is a core functional programming pattern. Here are the most popular Java libraries that have exactly this functionality:
1. Vavr (formerly Javaslang)
Vavr is built for functional programming in Java, and its List type is immutable by default. It directly supports the exact behavior you're looking for with methods like append() (or add(), which is an alias for append()), which return a new list without touching the original.
Example code:
import io.vavr.collection.List; public class ImmutableListDemo { public static void main(String[] args) { final List<Integer> unmodifiableList1 = List.of(1, 2, 3, 4); final List<Integer> unmodifiableList2 = unmodifiableList1.append(5); // Or use unmodifiableList1.add(5) — same result System.out.println(unmodifiableList1); // Output: List(1, 2, 3, 4) System.out.println(unmodifiableList2); // Output: List(1, 2, 3, 4, 5) } }
This is the closest match to your createAndAdd() method—clean, concise, and exactly what you described.
2. Google Guava
Guava is a staple Java utility library, and its ImmutableList is widely used for immutable collections. While it doesn't have a direct one-method "add and return new list" call, you can use its builder pattern to achieve the same result cleanly:
Example code:
import com.google.common.collect.ImmutableList; public class GuavaImmutableDemo { public static void main(String[] args) { final ImmutableList<Integer> unmodifiableList1 = ImmutableList.of(1, 2, 3, 4); final ImmutableList<Integer> unmodifiableList2 = ImmutableList.<Integer>builder() .addAll(unmodifiableList1) .add(5) .build(); System.out.println(unmodifiableList1); // Output: [1, 2, 3, 4] System.out.println(unmodifiableList2); // Output: [1, 2, 3, 4, 5] } }
Guava is a safe choice if you're already using it in your project—it's well-maintained and widely adopted.
3. Apache Commons Collections
Apache Commons Collections also has an ImmutableList implementation, though it's a bit more verbose than the options above. You'll need to create a mutable copy, add the element, then convert back to immutable:
Example code:
import org.apache.commons.collections4.ImmutableList; import java.util.ArrayList; public class CommonsImmutableDemo { public static void main(String[] args) { final ImmutableList<Integer> unmodifiableList1 = ImmutableList.of(1, 2, 3, 4); final ImmutableList<Integer> unmodifiableList2 = ImmutableList.copyOf( new ArrayList<>(unmodifiableList1) {{ add(5); }} ); System.out.println(unmodifiableList1); // Output: [1, 2, 3, 4] System.out.println(unmodifiableList2); // Output: [1, 2, 3, 4, 5] } }
This works, but it's not as clean as the other two options.
Final Recommendation
If you're starting fresh or want the most idiomatic match for your desired behavior, go with Vavr. If you're already using Guava in your project, stick with its builder pattern—it's straightforward and integrates seamlessly.
内容的提问来源于stack exchange,提问作者henry86

