nie*_*mar 15 java java-stream streamex
我有一个像这样的对象流:
"0", "1", "2", "3", "4", "5",
Run Code Online (Sandbox Code Playgroud)
如何将其转换为对的流:
{ new Pair("0", "1"), new Pair("2", "3"), new Pair("4", "5")}.
Run Code Online (Sandbox Code Playgroud)
流大小未知.我正在从一个可能很大的文件中读取数据.我只有收集器的迭代器,我使用spliterator将此迭代器转换为流.我知道这是使用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());
Run Code Online (Sandbox Code Playgroud)
要在流中创建Pairs,您需要一个有状态的lambdas,通常应该避免但可以完成.注意:这仅在流是单线程时才有效.即不平行.
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.
Run Code Online (Sandbox Code Playgroud)
问题的标题说从流中收集对,所以我假设您想实际收集这些对,但您评论道:
您的解决方案有效,问题是它将数据从文件加载到 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]
}
Run Code Online (Sandbox Code Playgroud)
我将通过收集到列表中并使用AbstractList提供视图来做到这一点 of the elements as pairs.
首先是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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们就可以定义我们需要的收集器了。这本质上是以下的简写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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,对于我们知道支持列表是新生成的用例(如本例所示),定义一个不防御性复制列表的 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]]
}
Run Code Online (Sandbox Code Playgroud)
这是一个包含可运行示例的完整源文件(作为 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]]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2186 次 |
| 最近记录: |