我有这样的对象流:
"0", "1", "2", "3", "4", "5",
如何将其转换为对流:
{ new Pair("0", "1"), new Pair("2", "3"), new Pair("4", "5")}.
流大小未知。我正在从一个可能很大的文件中读取数据。我只有迭代器来收集,我使用拆分器将此迭代器转换为流。我知道这是使用 StreamEx 处理相邻对的答案: 从流中收集连续对 这可以在 java 或 StreamEx 中完成吗?谢谢
我有这样的对象流:
"0", "1", "2", "3", "4", "5",
如何将其转换为对流:
{ new Pair("0", "1"), new Pair("2", "3"), new Pair("4", "5")}.
流大小未知。我正在从一个可能很大的文件中读取数据。我只有迭代器来收集,我使用拆分器将此迭代器转换为流。我知道这是使用 StreamEx 处理相邻对的答案: 从流中收集连续对 这可以在 java 或 StreamEx 中完成吗?谢谢
这不是天生的,但你可以做到
List input = ...
List<Pair> pairs = IntStream.range(0, input.size() / 2)
.map(i -> i * 2)
.mapToObj(i -> new Pair(input.get(i), input.get(i + 1)))
.collect(Collectors.toList());
要在流中创建 Pairs,您需要一个有状态的 lambda,通常应该避免但可以这样做。注意:这仅适用于流是单线程的。即不平行。
Stream<?> stream =
assert !stream.isParallel();
Object[] last = { null };
List<Pair> pairs = stream.map(a -> {
if (last[0] == null) {
last[0] = a;
return null;
} else {
Object t = last[0];
last[0] = null;
return new Pair(t, a);
}
}).filter(p -> p != null)
.collect(Collectors.toList());
assert last[0] == null; // to check for an even number input.
假设有一个Pair
withleft
和right
getter 和一个构造函数:
static class Paired<T> extends AbstractSpliterator<Pair<T>> {
private List<T> list = new ArrayList<>(2);
private final Iterator<T> iter;
public Paired(Iterator<T> iter) {
super(Long.MAX_VALUE, 0);
this.iter = iter;
}
@Override
public boolean tryAdvance(Consumer<? super Pair<T>> consumer) {
getBothIfPossible(iter);
if (list.size() == 2) {
consumer.accept(new Pair<>(list.remove(0), list.remove(0)));
return true;
}
return false;
}
private void getBothIfPossible(Iterator<T> iter) {
while (iter.hasNext() && list.size() < 2) {
list.add(iter.next());
}
}
}
用法是:
Iterator<Integer> iterator = List.of(1, 2, 3, 4, 5).iterator();
Paired<Integer> p = new Paired<>(iterator);
StreamSupport.stream(p, false)
.forEach(pair -> System.out.println(pair.getLeft() + " " + pair.getRight()));
问题的标题说从流中收集对,所以我假设你想实际收集这些,但你评论说:
您的解决方案有效,问题在于它将数据从文件加载到 PairList,然后我可以使用该集合中的流来处理对。我不能这样做,因为数据可能太大而无法存储在内存中。
所以这是一种不收集元素的方法。
将Iterator<T>转换为Iterator<List<T>>相对简单,然后将流转换为对流。
/**
* Returns an iterator over pairs of elements returned by the iterator.
*
* @param iterator the base iterator
* @return the paired iterator
*/
public static <T> Iterator<List<T>> paired(Iterator<T> iterator) {
return new Iterator<List<T>>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public List<T> next() {
T first = iterator.next();
if (iterator.hasNext()) {
return Arrays.asList(first, iterator.next());
} else {
return Arrays.asList(first);
}
}
};
}
/**
* Returns an stream of pairs of elements from a stream.
*
* @param stream the base stream
* @return the pair stream
*/
public static <T> Stream<List<T>> paired(Stream<T> stream) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(paired(stream.iterator()), Spliterator.ORDERED),
false);
}
@Test
public void iteratorAndStreamsExample() {
List<String> strings = Arrays.asList("a", "b", "c", "d", "e", "f");
Iterator<List<String>> pairs = paired(strings.iterator());
while (pairs.hasNext()) {
System.out.println(pairs.next());
// [a, b]
// [c, d]
// [e, f]
}
paired(Stream.of(1, 2, 3, 4, 5, 6, 7, 8)).forEach(System.out::println);
// [1, 2]
// [3, 4]
// [5, 6]
// [7, 8]
}
我会通过收集到一个列表中来做到这一点,并使用AbstractList来提供元素对的视图。
首先,PairList。这是一个简单的 AbstractList 包装器,包含任何具有偶数个元素的列表。(一旦指定了所需的行为,这可以很容易地适应处理奇数长度的列表。)
/**
* A view on a list of its elements as pairs.
*
* @param <T> the element type
*/
static class PairList<T> extends AbstractList<List<T>> {
private final List<T> elements;
/**
* Creates a new pair list.
*
* @param elements the elements
*
* @throws NullPointerException if elements is null
* @throws IllegalArgumentException if the length of elements is not even
*/
public PairList(List<T> elements) {
Objects.requireNonNull(elements, "elements must not be null");
this.elements = new ArrayList<>(elements);
if (this.elements.size() % 2 != 0) {
throw new IllegalArgumentException("number of elements must have even size");
}
}
@Override
public List<T> get(int index) {
return Arrays.asList(elements.get(index), elements.get(index + 1));
}
@Override
public int size() {
return elements.size() / 2;
}
}
然后我们可以定义我们需要的收集器。这本质上是 的简写collectingAndThen(toList(), PairList::new)
:
/**
* Returns a collector that collects to a pair list.
*
* @return the collector
*/
public static <E> Collector<E, ?, PairList<E>> toPairList() {
return Collectors.collectingAndThen(Collectors.toList(), PairList::new);
}
请注意,对于我们知道支持列表是新生成的用例(如本例中),定义一个不会防御性地复制列表的 PairList 构造函数可能是值得的。不过,这现在并不是很重要。但是一旦我们这样做了,这个方法就是collectingAndThen(toCollection(ArrayList::new), PairList::newNonDefensivelyCopiedPairList)
.
现在我们可以使用它了:
/**
* Creates a pair list with collectingAndThen, toList(), and PairList::new
*/
@Test
public void example() {
List<List<Integer>> intPairs = Stream.of(1, 2, 3, 4, 5, 6)
.collect(toPairList());
System.out.println(intPairs); // [[1, 2], [2, 3], [3, 4]]
List<List<String>> stringPairs = Stream.of("a", "b", "c", "d")
.collect(toPairList());
System.out.println(stringPairs); // [[a, b], [b, c]]
}
这是带有可运行示例的完整源文件(作为 JUnit 测试):
package ex;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
public class PairCollectors {
/**
* A view on a list of its elements as pairs.
*
* @param <T> the element type
*/
static class PairList<T> extends AbstractList<List<T>> {
private final List<T> elements;
/**
* Creates a new pair list.
*
* @param elements the elements
*
* @throws NullPointerException if elements is null
* @throws IllegalArgumentException if the length of elements is not even
*/
public PairList(List<T> elements) {
Objects.requireNonNull(elements, "elements must not be null");
this.elements = new ArrayList<>(elements);
if (this.elements.size() % 2 != 0) {
throw new IllegalArgumentException("number of elements must have even size");
}
}
@Override
public List<T> get(int index) {
return Arrays.asList(elements.get(index), elements.get(index + 1));
}
@Override
public int size() {
return elements.size() / 2;
}
}
/**
* Returns a collector that collects to a pair list.
*
* @return the collector
*/
public static <E> Collector<E, ?, PairList<E>> toPairList() {
return Collectors.collectingAndThen(Collectors.toList(), PairList::new);
}
/**
* Creates a pair list with collectingAndThen, toList(), and PairList::new
*/
@Test
public void example() {
List<List<Integer>> intPairs = Stream.of(1, 2, 3, 4, 5, 6)
.collect(toPairList());
System.out.println(intPairs); // [[1, 2], [2, 3], [3, 4]]
List<List<String>> stringPairs = Stream.of("a", "b", "c", "d")
.collect(toPairList());
System.out.println(stringPairs); // [[a, b], [b, c]]
}
}
我知道我迟到了,但是所有的答案似乎都非常复杂或者有很多 GC 开销/短期对象(这对现代 JVM 来说没什么大不了的),但是为什么不简单地做呢像这样?
public class PairCollaterTest extends TestCase {
static class PairCollater<T> implements Function<T, Stream<Pair<T, T>>> {
T prev;
@Override
public Stream<Pair<T, T>> apply(T curr) {
if (prev == null) {
prev = curr;
return Stream.empty();
}
try {
return Stream.of(Pair.of(prev, curr));
} finally {
prev = null;
}
}
}
public void testPairCollater() {
Stream.of("0", "1", "2", "3", "4", "5").sequential().flatMap(new PairCollater<>()).forEach(System.out::println);
}
}
印刷:
(0,1)
(2,3)
(4,5)
只需IntStream.range(1, 101)
用您的流替换(您不需要知道流的大小) -
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class TestClass {
public static void main(String[] args) {
final Pair pair = new Pair();
final List<Pair> pairList = new ArrayList<>();
IntStream.range(1, 101)
.map(i -> {
if (pair.a == null) {
pair.a = i;
return 0;
} else {
pair.b = i;
return 1;
}
})
.filter(i -> i == 1)
.forEach(i -> {
pairList.add(new Pair(pair));
pair.reset();
});
pairList.stream().forEach(p -> System.out.print(p + " "));
}
static class Pair {
public Object a;
public Object b;
public Pair() {
}
public Pair(Pair orig) {
this.a = orig.a;
this.b = orig.b;
}
void reset() {
a = null;
b = null;
}
@Override
public String toString() {
return "{" + a + "," + b + '}';
}
}
}