本文整理汇总了Java中java8.util.stream.Stream类的典型用法代码示例。如果您正苦于以下问题:Java Stream类的具体用法?Java Stream怎么用?Java Stream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Stream类属于java8.util.stream包,在下文中一共展示了Stream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: expandSpecialCharactersInSingletonWord
import java8.util.stream.Stream; //导入依赖的package包/类
private Stream<String> expandSpecialCharactersInSingletonWord(String word) {
if (word.length() == 1) {
return stream(word.replace("@", "ARROBA")
.replace("´", "APOSTROFE")
.replace("%", "PORCIENTO")
.replace("#", "NUMERO")
.replace("!", "ADMIRACION")
.replace(".", "PUNTO")
.replace("$", "PESOS")
.replace("\"", "COMILLAS")
.replace("-", "GUION")
.replace("/", "DIAGONAL")
.replace("+", "SUMA")
.replace("(", "ABRE PARENTESIS")
.replace(")", "CIERRA PARENTESIS")
.split(" "));
}
return stream(word);
}
示例2: testUnBox
import java8.util.stream.Stream; //导入依赖的package包/类
public void testUnBox() {
List<Double> list = java.util.Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
Spliterator<Double> spliter = Spliterators.spliteratorUnknownSize(list.iterator(), 0);
Stream<Double> stream = StreamSupport.stream(spliter, false);
// double sum = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0).stream().mapToDouble(i -> i).reduce(0.0, Double::sum);
double sum = stream.mapToDouble(new ToDoubleFunction<Double>() {
@Override
public double applyAsDouble(Double i) {
return i;
}
}).reduce(0.0, (DoubleBinaryOperator) new DoubleBinaryOperator() {
@Override
public double applyAsDouble(double v, double v2) {
return v + v2;
}
});
assertEquals(sum, 1.0 + 2.0 + 3.0 + 4.0 + 5.0);
}
示例3: testEntrySetSizeRemove
import java8.util.stream.Stream; //导入依赖的package包/类
private void testEntrySetSizeRemove(String name, final Set<Map.Entry<Integer, Integer>> c) {
Iterator<Map.Entry<Integer, Integer>> testIter = c.iterator();
Map.Entry<Integer, Integer> first = testIter.next();
assertTrue(c.remove(first));
Supplier<Spliterator<Map.Entry<Integer, Integer>>> late = new Supplier<Spliterator<Map.Entry<Integer, Integer>>>() {
@Override
public Spliterator<Map.Entry<Integer, Integer>> get() {
return Spliterators.spliterator(c, 0);
}
};
Stream<Map.Entry<Integer, Integer>> stream = StreamSupport.stream(late, 0, false);
Map.Entry<Integer, Integer> second = c.iterator().next();
assertTrue(c.remove(second));
Object[] result = stream.toArray();
assertEquals(result.length, c.size());
}
示例4: testParSeq
import java8.util.stream.Stream; //导入依赖的package包/类
public void testParSeq() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Spliterator<Integer> spliter = Spliterators.spliteratorUnknownSize(list.iterator(), 0);
Stream<Integer> stream = StreamSupport.stream(spliter, false);
// Stream<Integer> s = Arrays.asList(1, 2, 3, 4).stream().parallel();
Stream<Integer> s = stream.parallel();
assertTrue(s.isParallel());
s = s.sequential();
assertFalse(s.isParallel());
s = s.sequential();
assertFalse(s.isParallel());
s = s.parallel();
assertTrue(s.isParallel());
s = s.parallel();
assertTrue(s.isParallel());
}
示例5: addAddAll
import java8.util.stream.Stream; //导入依赖的package包/类
public void addAddAll() {
final StringJoiner sj = new StringJoiner(DASH, "{", "}");
sj.add(ONE);
ArrayList<String> nextOne = new ArrayList<String>();
nextOne.add(TWO);
nextOne.add(THREE);
Stream<String> stream = StreamSupport.stream(nextOne);
stream.forEachOrdered(new java8.util.function.Consumer<String>() {
@Override
public void accept(String t) {
sj.add(t);
}
});
String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
assertEquals(sj.toString(), expected);
}
示例6: addAlladd
import java8.util.stream.Stream; //导入依赖的package包/类
void addAlladd() {
final StringJoiner sj = new StringJoiner(DASH, "{", "}");
ArrayList<String> firstOne = new ArrayList<String>();
firstOne.add(ONE);
firstOne.add(TWO);
Stream<String> stream = StreamSupport.stream(firstOne);
stream.forEachOrdered(new java8.util.function.Consumer<String>() {
@Override
public void accept(String t) {
sj.add(t);
}
});
sj.add(THREE);
String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
assertEquals(sj.toString(), expected);
}
示例7: testDropWhileMulti
import java8.util.stream.Stream; //导入依赖的package包/类
private void testDropWhileMulti(Consumer<Stream<Integer>> mRef,
Consumer<IntStream> mInt,
Consumer<LongStream> mLong,
Consumer<DoubleStream> mDouble) {
Map<String, Supplier<Stream<Integer>>> sources = new HashMap<>();
sources.put("IntStream.range().boxed()",
() -> IntStreams.range(0, DROP_SOURCE_SIZE).boxed());
sources.put("IntStream.range().boxed().unordered()",
() -> IntStreams.range(0, DROP_SOURCE_SIZE).boxed().unordered());
sources.put("LinkedList.stream()",
() -> StreamSupport.stream(IntStreams.range(0, DROP_SOURCE_SIZE).boxed()
.collect(toCollection(LinkedList::new))
));
sources.put("LinkedList.stream().unordered()",
() -> StreamSupport.stream(IntStreams.range(0, DROP_SOURCE_SIZE).boxed()
.collect(toCollection(LinkedList::new))
)
.unordered());
testWhileMulti(sources, mRef, mInt, mLong, mDouble);
}
示例8: ConcatTest
import java8.util.stream.Stream; //导入依赖的package包/类
public ConcatTest(String scenario, Collection<Integer> c1, Collection<Integer> c2, Collection<Integer> expected) {
this.scenario = scenario;
this.c1 = c1;
this.c2 = c2;
this.expected = expected;
// verify prerequisite
Stream<Integer> s1s = StreamSupport.stream(c1, Characteristics.get(c1));
Stream<Integer> s2s = StreamSupport.stream(c2, Characteristics.get(c2));
Stream<Integer> s1p = StreamSupport.stream(c1, Characteristics.get(c1), true);
Stream<Integer> s2p = StreamSupport.stream(c2, Characteristics.get(c2), true);
assertTrue(s1p.isParallel());
assertTrue(s2p.isParallel());
assertFalse(s1s.isParallel());
assertFalse(s2s.isParallel());
assertTrue(s1s.spliterator().hasCharacteristics(Spliterator.ORDERED));
assertTrue(s1p.spliterator().hasCharacteristics(Spliterator.ORDERED));
assertTrue(s2s.spliterator().hasCharacteristics(Spliterator.ORDERED));
assertTrue(s2p.spliterator().hasCharacteristics(Spliterator.ORDERED));
}
示例9: testOps
import java8.util.stream.Stream; //导入依赖的package包/类
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
// @@@ More things to test here:
// - Every value in data is present in right bucket
// - Total number of values equals size of data
for (MapperData<Integer, ?> md : getMapperData(data)) {
Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
Map<Object, List<Integer>> result =
withData(data)
.terminal((Stream<Integer> s) -> s, s -> s.collect(tab))
.resultAsserter((act, exp, ord, par) -> {
if (par & !ord) {
GroupByOpTest.assertMultiMapEquals(act, exp);
}
else {
GroupByOpTest.assertObjectEquals(act, exp);
}
})
.exercise();
assertEquals(result.keySet().size(), md.expectedSize);
}
}
示例10: testConsumed
import java8.util.stream.Stream; //导入依赖的package包/类
public void testConsumed() {
Stream<Integer> s1 = StreamSupport.stream(countTo(100));
try {
s1.forEach(i -> {});
// Adding onClose handler when stream is consumed is illegal
// handler must not be registered
checkISE(() -> s1.onClose(() -> fail("s1")));
} finally {
if (s1 != null) {
s1.close();
}
}
// close() must be idempotent:
// second close() invoked at the end of try-with-resources must have no effect
Stream<Integer> s2 = StreamSupport.stream(countTo(100));
try {
s2.close();
// Adding onClose handler when stream is closed is also illegal
checkISE(() -> s2.onClose(() -> fail("s2")));
} finally {
if (s2 != null) {
s2.close();
}
}
}
示例11: assertValue
import java8.util.stream.Stream; //导入依赖的package包/类
@Override
void assertValue(M map, Supplier<Stream<T>> source, boolean ordered) throws Exception {
if (!clazz.isAssignableFrom(map.getClass()))
fail(String.format("Class mismatch in ToMapAssertion: %s, %s", clazz, map.getClass()));
Set<K> uniqueKeys = source.get().map(keyFn).collect(toSet());
assertEquals(uniqueKeys, map.keySet());
source.get().forEach(t -> {
K key = keyFn.apply(t);
V v = source.get()
.filter(e -> key.equals(keyFn.apply(e)))
.map(valueFn)
.reduce(mergeFn)
.get();
assertEquals(map.get(key), v);
});
}
示例12: exerciseMapCollection
import java8.util.stream.Stream; //导入依赖的package包/类
private<T, M extends Map>
void exerciseMapCollection(TestData<T, Stream<T>> data,
Collector<T, ?, ? extends M> collector,
CollectorAssertion<T, M> assertion)
throws Exception {
boolean ordered = !collector.characteristics().contains(Collector.Characteristics.UNORDERED);
M m = withData(data)
.terminal(s -> s.collect(collector))
.resultAsserter(mapTabulationAsserter(ordered))
.exercise();
assertion.assertValue(m, () -> data.stream(), ordered);
m = withData(data)
.terminal(s -> s.unordered().collect(collector))
.resultAsserter(mapTabulationAsserter(ordered))
.exercise();
assertion.assertValue(m, () -> data.stream(), false);
}
示例13: testGroupingByWithFlatMapping
import java8.util.stream.Stream; //导入依赖的package包/类
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testGroupingByWithFlatMapping(String name, TestData.OfRef<Integer> data) throws Exception {
Function<Integer, Integer> classifier = i -> i % 3;
Function<Integer, Stream<Integer>> flatMapperByNull = i -> null;
Function<Integer, Stream<Integer>> flatMapperBy0 = i -> RefStreams.empty();
Function<Integer, Stream<Integer>> flatMapperBy2 = i -> RefStreams.of(i, i);
exerciseMapCollection(data,
groupingBy(classifier, flatMapping(flatMapperByNull, toList())),
new GroupingByAssertion<>(classifier, HashMap.class,
new FlatMappingAssertion<>(flatMapperBy0,
new ToListAssertion<>())));
exerciseMapCollection(data,
groupingBy(classifier, flatMapping(flatMapperBy0, toList())),
new GroupingByAssertion<>(classifier, HashMap.class,
new FlatMappingAssertion<>(flatMapperBy0,
new ToListAssertion<>())));
exerciseMapCollection(data,
groupingBy(classifier, flatMapping(flatMapperBy2, toList())),
new GroupingByAssertion<>(classifier, HashMap.class,
new FlatMappingAssertion<>(flatMapperBy2,
new ToListAssertion<>())));
}
示例14: testWhileMulti
import java8.util.stream.Stream; //导入依赖的package包/类
private void testWhileMulti(TestData.OfRef<Integer> data,
ResultAsserter<Iterable<Integer>> ra,
Function<Stream<Integer>, Stream<Integer>> mRef,
Function<IntStream, IntStream> mInt,
Function<LongStream, LongStream> mLong,
Function<DoubleStream, DoubleStream> mDouble) {
Map<String, Function<Stream<Integer>, Stream<Integer>>> ms = new HashMap<>();
ms.put("Ref", mRef);
ms.put("Int", s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e));
ms.put("Long", s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e));
ms.put("Double", s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e));
ms.put("Ref using defaults", s -> mRef.apply(DefaultMethodStreams.delegateTo(s)));
ms.put("Int using defaults", s -> mInt.apply(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))).mapToObj(e -> e));
ms.put("Long using defaults", s -> mLong.apply(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))).mapToObj(e -> (int) e));
ms.put("Double using defaults", s -> mDouble.apply(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))).mapToObj(e -> (int) e));
testWhileMulti(data, ra, ms);
}
示例15: testParSeq
import java8.util.stream.Stream; //导入依赖的package包/类
public void testParSeq() {
Stream<Integer> s = StreamSupport.stream(Arrays.asList(1, 2, 3, 4)).parallel();
assertTrue(s.isParallel());
s = s.sequential();
assertFalse(s.isParallel());
s = s.sequential();
assertFalse(s.isParallel());
s = s.parallel();
assertTrue(s.isParallel());
s = s.parallel();
assertTrue(s.isParallel());
}