本文整理匯總了Java中java.util.stream.Stream.flatMap方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.flatMap方法的具體用法?Java Stream.flatMap怎麽用?Java Stream.flatMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.flatMap方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSemiBlocksInArea
import java.util.stream.Stream; //導入方法依賴的package包/類
public Stream<ISemiBlock> getSemiBlocksInArea(World world, AxisAlignedBB aabb){
List<Chunk> applicableChunks = new ArrayList<Chunk>();
int minX = (int)aabb.minX;
int minY = (int)aabb.minY;
int minZ = (int)aabb.minZ;
int maxX = (int)aabb.maxX;
int maxY = (int)aabb.maxY;
int maxZ = (int)aabb.maxZ;
//Get the relevant chunks.
for (int x = minX; x < maxX + 16; x += 16) {
for (int z = minZ; z < maxZ + 16; z += 16) {
Chunk chunk = world.getChunkFromBlockCoords(new BlockPos(x, 0, z));
applicableChunks.add(chunk);
}
}
//Retrieve all semi block storages from the relevant chunks
Stream<Map<BlockPos, List<ISemiBlock>>> chunkMaps = applicableChunks.stream()
.map(chunk -> getSemiBlocks().get(chunk))
.filter(map -> map != null);
Stream<List<ISemiBlock>> semiBlocksPerPos = chunkMaps.flatMap(map -> map.values().stream());
Stream<ISemiBlock> existingSemiBlocksInArea = semiBlocksPerPos.flatMap(semiBlockLists -> semiBlockLists.stream());
Stream<ISemiBlock> allSemiBlocksInArea = Streams.concat(existingSemiBlocksInArea, addingBlocks.stream());
return allSemiBlocksInArea.filter(s -> !s.isInvalid() &&
minX <= s.getPos().getX() && s.getPos().getX() <= maxX &&
minY <= s.getPos().getY() && s.getPos().getY() <= maxY &&
minZ <= s.getPos().getZ() && s.getPos().getZ() <= maxZ);
}
示例2: pluckDescendants
import java.util.stream.Stream; //導入方法依賴的package包/類
private Stream<Node> pluckDescendants(Node parent, TemplateId... path) {
Stream<Node> tier = Stream.of(parent);
for (TemplateId nextTier : Arrays.asList(path)) {
tier = tier.flatMap(node -> node.getChildNodes(nextTier));
}
return tier;
}
示例3: lookupProvidedChildrenByTags
import java.util.stream.Stream; //導入方法依賴的package包/類
public static <T, X> Stream<T> lookupProvidedChildrenByTags(Stream<T> children, X tags, BiFunction<T, X, X> adjuster, BiFunction<T, X, Stream<T>> nodeToChildren) {
Stream<T> result = children.flatMap(child -> {
X adjustedTags = adjuster.apply(child, tags);
Stream<T> subStream = lookupChildrenByTags(child, adjustedTags, adjuster, nodeToChildren);
return subStream = Stream.concat(Stream.of(child), subStream);
});
return result;
}
開發者ID:SmartDataAnalytics,項目名稱:SubgraphIsomorphismIndex,代碼行數:9,代碼來源:SubgraphIsomorphismIndexImpl.java
示例4: middleStream
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* 中間操作(Intermediate) 轉換Stream
*/
public static void middleStream(){
System.err.println("----------------------------中間操作 轉換Stream-----------------------------");
//去除重複 distinct
lists.stream().distinct().forEach(p -> System.out.print(p + "\t")); //1 2 3 follow wind followwwind
System.out.println();
//過濾元素 filter
lists.stream().filter(p -> p.length() > 1).forEach(p -> System.out.print(p + "\t")); //follow wind followwwind
System.out.println();
// sorted 流排序,中間操作返回流本身
lists.stream().filter(str -> str.contains("w"))
.sorted((str1, str2) -> {
if (str1.length() == str2.length()) {
return 0;
} else if (str1.length() > str2.length()) {
return 1;
} else {
return -1;
}
}).forEach(System.out::println); //wind follow followwwind
//limit 對一個Stream進行截斷操作,獲取其前N個元素,如果原Stream中包含的元素個數小於N,那就獲取其所有的元素;
lists.stream().limit(5).forEach(p -> System.out.print(p + "\t")); //1 2 3 3 follow
System.out.println();
//skip 返回一個丟棄原Stream的前N個元素後剩下元素組成的新Stream,如果原Stream中包含的元素個數小於N,那麽返回空Stream;
lists.stream().skip(5).forEach(p -> System.out.print(p + "\t")); //wind followwwind
System.out.println();
//peek 生成一個包含原Stream的所有元素的新Stream,同時會提供一個消費函數(Consumer實例),新Stream每個元素被消費的時候都會執行給定的消費函數;
lists.stream().peek(p -> {p = p.toUpperCase(); System.out.println(p);}).forEach(System.out::println);
System.out.println();
//轉換元素 map
lists.stream().map(p -> p + "-->").forEach(System.out::print); // 1-->2-->3-->3-->follow-->wind-->followwwind-->
System.out.println("end");
lists.stream().map(p -> p.split(" ")).map(p -> p[0] + "\t").forEach(System.out::print);//1 2 3 3 follow wind followwwind
System.out.println();
lists.stream().map(p -> p.split("")).map(p -> {
String tmp = "";
if(p.length > 1){
tmp = p[1];
}else{
tmp = p[0];
}
return tmp + "\t";
}).forEach(System.out::print); //1 2 3 3 o i o
System.out.println();
lists.stream().filter(p -> p.matches("\\d+")).mapToInt(p -> Integer.valueOf(p)).forEach(p -> System.out.print(p + "\t"));//1 2 3 3
System.out.println();
// flatMap 和map類似,不同的是其每個元素轉換得到的是Stream對象,會把子Stream中的元素壓縮到父集合中
lists.stream().flatMap(p -> Stream.of(p.split("www"))).forEach(p -> System.out.print(p + "\t"));
//1 2 3 3 follow wind follo ind
Stream<List<Integer>> inputStream = Stream.of(
Arrays.asList(1),
Arrays.asList(2, 3),
Arrays.asList(4, 5, 6)
);
System.out.println();
Stream<Integer> outputStream = inputStream.
flatMap((childList) -> childList.stream());
outputStream.forEach(p -> System.out.print(p + "\t")); //1 2 3 4 5 6
System.out.println();
System.err.println("----------------------------中間操作 轉換Stream-----------------------------");
}
示例5: contexts
import java.util.stream.Stream; //導入方法依賴的package包/類
protected Stream<? extends BukkitFacetContext<?>> contexts(Object event) {
// Try to get some online players from the event, either directly
// through MatchPlayerEvent, or indirectly through entities.
final Set<MatchPlayer> players;
if(event instanceof MatchPlayerEvent) {
players = ((MatchPlayerEvent) event).players().collect(Collectors.toImmutableSet());
} else {
final Set<Entity> entities = new HashSet<>();
if(event instanceof EntityAction) entities.add(((EntityAction) event).getActor());
if(event instanceof EntityEvent) entities.add(((EntityEvent) event).getEntity());
if(event instanceof PlayerAction) entities.add(((PlayerAction) event).getActor());
if(event instanceof PlayerEvent) entities.add(((PlayerEvent) event).getPlayer());
players = entities.stream()
.flatMap(entity -> Streams.ofNullable(finder.getPlayer(entity)))
.collect(Collectors.toImmutableSet());
}
// If we have one or more MatchPlayers, return them along with their user contexts
if(!players.isEmpty()) {
return Stream.concat(
players.stream(),
players.stream().map(player -> player.userContext)
);
}
// If we couldn't derive any online players from the event, try for offline player UUIDs
final Set<UUID> uuids;
if(event instanceof MatchUserEvent) {
uuids = ((MatchUserEvent) event).users().collect(Collectors.toImmutableSet());
} else if(event instanceof UserEvent) {
uuids = ImmutableSet.of(((UserEvent) event).getUser().uuid());
} else {
return Stream.empty();
}
// Restrict to a specific match, if possible
final Stream<Match> matches = finder.match((Event) event)
.map(Stream::of)
.orElseGet(() -> finder.currentMatches().stream());
// Search the selected matches for both users and players
// with the selected UUIDs.
return matches.flatMap(
match -> uuids.stream().flatMap(
uuid -> Stream.concat(
Optionals.stream(match.player(uuid)),
Optionals.stream(match.userContext(uuid))
)
)
);
}
示例6: upcast
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
public Stream<Data<T>> upcast(Stream<Data<T>> input) {
return input.flatMap(i -> Optional.ofNullable(upcasters.get(new DataRevision(i)))
.map(upcaster -> upcast(upcaster.upcast(i)))
.orElse(Stream.of(i)));
}
示例7: diffResults
import java.util.stream.Stream; //導入方法依賴的package包/類
static Stream<String> diffResults(
Case c,
Stream<Map.Entry<int[], Map<String, String>>> expectedResults
) {
return expectedResults
.flatMap(exp -> {
int[] comb = exp.getKey();
Map<String, String> expected = exp.getValue();
String src = expandTemplate(c, comb);
Map<String, String> actual;
try {
try (TestClassLoader cl = compile(src)) {
actual = generateResult(c, cl);
} catch (CompileException ce) {
return Stream.of(src + "\n" +
"got compilation error: " + ce);
}
} catch (IOException ioe) {
// from TestClassLoader.close()
return Stream.of(src + "\n" +
"got IOException: " + ioe);
}
if (actual.equals(expected)) {
return Stream.empty();
} else {
Map<String, String> diff = new HashMap<>(expected);
diff.entrySet().removeAll(actual.entrySet());
return Stream.of(
diff.entrySet()
.stream()
.map(e -> "expected: " + e.getKey() + ": " +
e.getValue() + "\n" +
" actual: " + e.getKey() + ": " +
actual.get(e.getKey()) + "\n")
.collect(joining("\n", src + "\n\n", "\n"))
);
}
});
}