Guava Iterables concat实现不适用于Java 8中添加的新Iterable API方法?

时间:2022-11-10 20:46:09

I wanted a simple way to do Iterables concat so I tried Guava Iterables (19 RC1) in my Spring MVC code (Spring 4.2.1).

我想要一个简单的方法来做Iterables concat所以我在我的Spring MVC代码(Spring 4.2.1)中尝试了Guava Iterables(19 RC1)。

I have two iterables from Spring Data JPA. e.g.

我有两个来自Spring Data JPA的迭代。例如

Iterable<Portfolio> result = portfRepository.findBySomeCriteria();
Iterable<Portfolio> result2 = portfRepository.findByOtherCriteria();

I then do a concat:

然后我做了一个concat:

Iterable<Portfolio> combined = Iterables.concat(result, result2);

However when the combined Iterable is processed by Jackson, it only displayed { empty: false } instead of the array of portfolios.

但是,当Jackson处理组合的Iterable时,它只显示{empty:false}而不是投资组合数组。

I looked at Guava's concat implementation. It basically return an ImmutableList of two Iterables, and provided an overridden Iterator that knows how to iterate through first Iterable, then 2nd Iterable.

我看了一下Guava的concat实现。它基本上返回两个Iterables的ImmutableList,并提供了一个重写的Iterator,它知道如何遍历第一个Iterable,然后是第二个Iterable。

/**
 * Combines two iterables into a single iterable. The returned iterable has an
 * iterator that traverses the elements in {@code a}, followed by the elements
 * in {@code b}. The source iterators are not polled until necessary.
 *
 * <p>The returned iterable's iterator supports {@code remove()} when the
 * corresponding input iterator supports it.
 */
public static <T> Iterable<T> concat(
  Iterable<? extends T> a, Iterable<? extends T> b) {
  return concat(ImmutableList.of(a, b));
}

/**
 * Combines multiple iterables into a single iterable. The returned iterable
 * has an iterator that traverses the elements of each iterable in
 * {@code inputs}. The input iterators are not polled until necessary.
 *
 * <p>The returned iterable's iterator supports {@code remove()} when the
 * corresponding input iterator supports it. The methods of the returned
 * iterable may throw {@code NullPointerException} if any of the input
 * iterators is null.
 */
public static <T> Iterable<T> concat(
  final Iterable<? extends Iterable<? extends T>> inputs) {
  checkNotNull(inputs);
  return new FluentIterable<T>() {
    @Override
    public Iterator<T> iterator() {
      return Iterators.concat(iterators(inputs));
    }
  };
}

The problem is, the Java 8 Iterable API provided new methods:

问题是,Java 8 Iterable API提供了新方法:

default void forEach(Consumer<? super T> action)
default Spliterator<T> spliterator()

I guess if some code (in this case Spring MVC or Jackson) make use of these methods to loop through the Iterable instead of the iterator method, then the concatenated Guava Iterable which is a List of Iterables will not work probably. Can someone confirm this? Thanks.

我想如果一些代码(在这种情况下是Spring MVC或Jackson)使用这些方法来遍历Iterable而不是迭代器方法,那么连接的Guava Iterable(Iterables列表)可能无法正常工作。有人可以证实吗?谢谢。

1 个解决方案

#1


3  

You need the following dependency:

您需要以下依赖项:

This adds support for Guava specific datatypes.

这增加了对Guava特定数据类型的支持。

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-guava</artifactId>
    <version>2.6.2</version>
</dependency>

You probably will need the JDK8 datatypes as well at some point.

您可能在某些时候也需要JDK8数据类型。

#1


3  

You need the following dependency:

您需要以下依赖项:

This adds support for Guava specific datatypes.

这增加了对Guava特定数据类型的支持。

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-guava</artifactId>
    <version>2.6.2</version>
</dependency>

You probably will need the JDK8 datatypes as well at some point.

您可能在某些时候也需要JDK8数据类型。