项目作者: mtumilowicz

项目描述 :
We show how to convert vavr collections to java collections.
高级语言: Java
项目地址: git://github.com/mtumilowicz/java11-vavr-collections-conversion-toJava-asJava.git


Build Status

java11-vavr-collections-conversion-toJava-asJava

We show how to convert vavr collections to java collections.

Reference: https://static.javadoc.io/io.vavr/vavr/0.9.0/io/vavr/collection/List.html

preface

In vavr, we have two approaches to converting vavr collections
to java collections:

  • toJava*(), where * could be: List, Array, Set
  • asJava()

The main difference between asJava() and toJava*() is that
a view from asJava() is created in O(1) (constant time)
whereas conversion (toJava*()) takes O(n) (linear time),
with n = collection size.

The operations on a view have the same performance
characteristics than the underlying persistent Vavr
collection whereas the performance characteristics of a
converted collection are those of the Java standard collections.

Motivation: we often have to use java collections (in
frameworks…).

toJava*

Reference: https://static.javadoc.io/io.vavr/vavr/0.9.0/io/vavr/Value.html

Converts vavr collections to java collections by copying
the content, for example:

  1. java.util.List<T> toJavaList() {
  2. return ValueModule.toJavaCollection(this, ArrayList::new, 10);
  3. }

where:

  1. static <T, R extends java.util.Collection<T>> R toJavaCollection(
  2. Value<T> value, Function<Integer, R> containerSupplier, int defaultInitialCapacity) {
  3. final int size;
  4. if (value instanceof Traversable && ((Traversable) value).isTraversableAgain() && !value.isLazy()) {
  5. size = ((Traversable) value).size();
  6. } else {
  7. size = defaultInitialCapacity;
  8. }
  9. final R container = containerSupplier.apply(size);
  10. value.forEach(container::add);
  11. return container;
  12. }

methods summary:

  • Object[] toJavaArray(),
  • T[] toJavaArray(Class<T> componentType),
  • <C extends Collection<T>> C toJavaCollection(Function<Integer,C> factory),
  • List<T> toJavaList(),
  • <LIST extends List<T>> LIST toJavaList(Function<Integer,LIST> factory),
  • Map<K,V> toJavaMap(Function<? super T,? extends Tuple2<? extends K,? extends V>> f),
  • `> MAP toJavaMap(Supplier factory,
    1. Function<? super T,? extends K> keyMapper,
    2. Function<? super T,? extends V> valueMapper)`,
  • `> MAP toJavaMap(Supplier factory,
    1. Function<? super T,? extends Tuple2<? extends K,? extends V>> f)`,
  • Optional<T> toJavaOptional(),
  • Stream<T> toJavaStream(),
  • Stream<T> toJavaParallelStream(),
  • Set<T> toJavaSet(),
  • <SET extends Set<T>> SET toJavaSet(Function<Integer,SET> factory)

asJava()

Reference: https://static.javadoc.io/io.vavr/vavr/0.9.2/io/vavr/collection/Seq.html

Creates an immutable List view on top of this Seq,
i.e. calling mutators will result in
UnsupportedOperationException at runtime.

Please note that our immutable java.util.List view
throws UnsupportedOperationException before checking
method arguments. Java does handle this case inconsistently.

  • List<T> asJava()
  • List<T> asJava(Consumer<? super List<T>> action)
    Note that we have also mutable equivalents:
  • List<T> asJavaMutable()
  • List<T> asJavaMutable(Consumer<? super List<T>> action)