本文整理匯總了Java中java.util.stream.Stream.map方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.map方法的具體用法?Java Stream.map怎麽用?Java Stream.map使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.map方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onTabCompletion
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
public List<String> onTabCompletion(CommandSender sender, Class<?> type, List<String> args) {
Stream<? extends Player> players = Bukkit.getOnlinePlayers().stream();
if(sender instanceof Player) {
Player player = (Player) sender;
players = players.filter(player::canSee);
}
Stream<String> strStream = players.map(Player::getName);
if(args.isEmpty())
return strStream.collect(Collectors.toList());
String partial = args.get(0);
return strStream
.filter(s -> StringUtil.startsWithIgnoreCase(s, partial))
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toList());
}
示例2: load
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
public Stream<PropertyBox> load(QueryConfigurationProvider configuration, int offset, int limit)
throws DataAccessException {
Query q = buildQuery(configuration, true);
// paging
if (limit > 0) {
q.limit(limit);
q.offset(offset);
}
// execute
Stream<PropertyBox> results = q.stream(propertySet);
if (getItemIdentifier().isPresent()) {
return results.map(pb -> new IdentifiablePropertyBox(pb, getItemIdentifier().get()));
}
return results;
}
示例3: testLazyFilterAndMapAndDistinct
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testLazyFilterAndMapAndDistinct() {
ICounter<String, Stream<String>> req = Countify.of(new FileRequest()::getContent);
WeatherWebApi api = new WeatherWebApi(req::apply);
Stream<WeatherInfoDto> infos = api.pastWeather(41.15, -8.6167, of(2017, 02, 01), of(2017, 02, 28));
assertEquals(1, req.getCount());
infos = infos.filter(info -> info.getDescription().toLowerCase().contains("sun"));
assertEquals(1, req.getCount());
Stream<Integer> temps = infos.map(info -> info.getTempC());
assertEquals(1, req.getCount());
temps = temps.distinct();
assertEquals(1, req.getCount());
assertEquals(5, temps.count());
assertEquals(1, req.getCount());
}
示例4: dynamicTestsFromStream
import java.util.stream.Stream; //導入方法依賴的package包/類
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
Stream<String> inputStream = Stream.of("A", "B", "C");
return inputStream.map(
input -> dynamicTest("Display name for input " + input, () -> {
System.out.println("Testing " + input);
}));
}
示例5: execute
import java.util.stream.Stream; //導入方法依賴的package包/類
public MemgraphCypherScope execute(
MemgraphCypherQueryContext ctx,
boolean distinct,
CypherReturnBody returnBody,
MemgraphCypherScope scope
) {
List<CypherReturnItem> returnItems = returnBody.getReturnItems()
.stream()
.flatMap(ri -> {
if (ri.getExpression() instanceof CypherAllLiteral) {
return getAllFieldNamesAsReturnItems(scope);
}
return Stream.of(ri);
})
.collect(Collectors.toList());
LinkedHashSet<String> columnNames = getColumnNames(returnItems);
Stream<MemgraphCypherScope.Item> rows = scope.stream();
long aggregationCount = aggregationCount(ctx, returnItems);
if (returnItems.size() > 0 && aggregationCount == returnItems.size()) {
rows = Stream.of(getReturnRow(ctx, returnItems, null, scope));
} else if (aggregationCount > 0 && isGroupable(returnItems.get(0))) {
Map<Optional<?>, MemgraphCypherScope> groups = groupBy(ctx, returnItems.get(0), rows);
rows = groups.entrySet().stream()
.map(group -> getReturnRow(ctx, returnItems, group.getKey(), group.getValue()));
} else {
rows = rows
.map(row -> getReturnRow(ctx, returnItems, null, row));
}
if (distinct) {
rows = rows.distinct();
}
MemgraphCypherScope results = MemgraphCypherScope.newFromItems(rows, columnNames, scope);
return applyReturnBody(ctx, returnBody, results);
}
示例6: createBatchFor
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Creates a new batch containing the specified import requests.
* @param requests the import requests to queue.
* @return the new batch.
* @throws NullPointerException if the stream or any of its elements is
* {@code null}.
* @throws IllegalArgumentException if the stream is empty.
*/
protected ImportBatch createBatchFor(Stream<ImportInput> requests) {
requireNonNull(requests, "requests");
Stream<ImportInput> rs = requests.map(r -> {
requireNonNull(r, "request");
return r;
});
ImportBatch batch = new ImportBatch(rs);
ImportBatchStatus unprocessed = new ImportBatchStatus(batch);
env.batchStore().put(batch.batchId(), unprocessed);
return unprocessed.batch();
}
示例7: testWeatherServiceLazy
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testWeatherServiceLazy(){
/**
* Arrange WeatherService --> WeatherWebApi --> Countify --> FileRequest
*/
ICounter<String, Stream<String>> req = Countify.of(new FileRequest()::getContent);
WeatherService api = new WeatherService(new WeatherWebApi(req::apply));
/**
* Act and Assert
*/
Stream<Location> locals = api.search("Porto");
assertEquals(1, req.getCount());
locals = locals.filter(l -> l.getLatitude() > 0 );
assertEquals(1, req.getCount());
Location loc = locals.findFirst().get();
assertEquals(1, req.getCount());
Stream<WeatherInfo> infos = loc.getPastWeather(of(2017,02,01), of(2017,02,28));
assertEquals(2, req.getCount());
infos = infos.filter(info -> info.getDescription().toLowerCase().contains("sun"));
Stream<Integer> temps = infos.map(WeatherInfo::getTempC);
temps = temps.distinct();
assertEquals(2, req.getCount());
/**
* When we iterate over the pastWeather then we make one more request
*/
assertEquals(5, temps.count()); // iterates all items
assertEquals(2, req.getCount());
}
示例8: cacheAll
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Cache a bunch of guilds
* Difference to the sync method is that this won't double check the saved DiscordGuilds against their presence in
* the bot. This method is fine to call to cache only a subset of all guilds
*
* @param dbWrapper The database to run the sync on
* @param guilds Stream over all guilds to be cached
* @param clazz Class of the actual DiscordGuild entity
* @return DatabaseExceptions caused by the execution of this method
*/
@Nonnull
public static <E extends DiscordGuild<E>> Collection<DatabaseException> cacheAll(@Nonnull final DatabaseWrapper dbWrapper,
@Nonnull final Stream<Guild> guilds,
@Nonnull final Class<E> clazz) {
long started = System.currentTimeMillis();
final List<DatabaseException> exceptions = new ArrayList<>();
final AtomicInteger joined = new AtomicInteger(0);
final AtomicInteger streamed = new AtomicInteger(0);
final Function<Guild, Function<E, E>> cacheAndJoin = (guild) -> (discordguild) -> {
E result = discordguild.set(guild);
if (!result.present) {
result = result.join();
joined.incrementAndGet();
}
return result;
};
final Stream<Transfiguration<Long, E>> transfigurations = guilds.map(
guild -> {
if (streamed.incrementAndGet() % 100 == 0) {
log.debug("{} guilds processed while caching", streamed.get());
}
return Transfiguration.of(EntityKey.of(guild.getIdLong(), clazz), cacheAndJoin.apply(guild));
}
);
exceptions.addAll(dbWrapper.findApplyAndMergeAll(transfigurations));
log.debug("Cached {} DiscordGuild entities of class {} in {}ms with {} exceptions, joined {}",
streamed.get(), clazz.getSimpleName(), System.currentTimeMillis() - started, exceptions.size(), joined.get());
return exceptions;
}
示例9: testWeatherServiceLazy
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testWeatherServiceLazy(){
/**
* Arrange WeatherService --> WeatherWebApi --> Countify --> FileRequest
*/
ICounter<String, CompletableFuture<Stream<String>>> req = Countify.of(new FileRequest()::getContent);
WeatherService api = new WeatherService(new WeatherWebApi(req::apply));
/**
* Act and Assert
*/
Stream<Location> locals = api.search("Porto").join();
assertEquals(1, req.getCount());
locals = locals.filter(l -> l.getLatitude() > 0 );
assertEquals(1, req.getCount());
/**
* When we get the first item from Stream we are instantiating a new
* Location object and thus requesting its last 30 days past weather.
*/
Location loc = locals.findFirst().get();
assertEquals(2, req.getCount());
Stream<WeatherInfo> infos = loc.getPastWeather(of(2017,02,01), of(2017,02,28));
assertEquals(3, req.getCount());
infos = infos.filter(info -> info.getDescription().toLowerCase().contains("sun"));
Stream<Integer> temps = infos.map(WeatherInfo::getTempC);
temps = temps.distinct();
assertEquals(3, req.getCount());
assertEquals(5, temps.count()); // iterates all items
assertEquals(3, req.getCount());
}
示例10: mapToStream
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* @see #mapToStream(Stream, Function) mapToStream
*/
default Stream<TARGET> mapToStream(final Stream<SOURCE> sources) {
return sources.map(this::map);
}
示例11: forAll
import java.util.stream.Stream; //導入方法依賴的package包/類
static <T> Stream<Runnable> forAll(Stream<? extends T> inputs, Consumer<? super T> consumer) {
requireNonNull(consumer);
return inputs.map(input -> () -> consumer.accept(input));
}
示例12: testWeatherServiceLazyAndCache
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testWeatherServiceLazyAndCache(){
/**
* Arrange WeatherService --> WeatherWebApi --> Countify --> FileRequest
*/
ICounter<String, CompletableFuture<Stream<String>>> req = Countify.of(new FileRequest()::getContent);
// Function<String, Iterable<String>> cache = Cache.memoize(req);
WeatherServiceCache api = new WeatherServiceCache(new WeatherWebApi(req::apply));
/**
* Act and Assert
*/
Stream<Location> locals = api.search("Porto").join();
assertEquals(1, req.getCount());
locals = locals.filter(l -> l.getLatitude() > 0 );
assertEquals(1, req.getCount());
/**
* Counts 2 request when iterates to get the first Location.
* Location requests last 30 days past weather asynchronously.
*/
Location loc = locals.iterator().next();
assertEquals(2, req.getCount());
Stream<WeatherInfo> infos = loc.getPastWeather(of(2017,02,01), of(2017,02,28));
assertEquals(3, req.getCount());
infos = infos.filter(info ->
info.getDescription().toLowerCase().contains("sun"));
Stream<Integer> temps = infos.map(WeatherInfo::getTempC);
temps = temps.distinct();
assertEquals(3, req.getCount());
assertEquals(5, temps.count()); // iterates all items
assertEquals(3, req.getCount());
/**
* NOT caching Locations. Just cache for Past Weather.
* So, searching for Porto makes 2 more requests: 1 for Location and
* 1 more for last 30 days weather.
*/
loc = api.search("Porto").join().findFirst().get();
assertEquals(5, req.getCount());
/**
* February past weather is already in cache for Porto.
* So, we will not make no more requests.
*/
temps = loc
.getPastWeather(of(2017,02,01), of(2017,02,28))
.filter(info -> info.getDescription().toLowerCase().contains("sun"))
.map(WeatherInfo::getTempC);
assertEquals((long) 20, (long) temps.skip(2).findFirst().get()); // another iterator
assertEquals(5, req.getCount());
/**
* getting a sub-interval of past weather should return from cache
*/
infos = loc.getPastWeather(of(2017,02,05), of(2017,02,18));
infos.iterator().next();
assertEquals(5, req.getCount());
/**
* getting a new interval gets from IO
*/
infos = loc.getPastWeather(of(2017,02,15), of(2017,03,15));
infos.forEach((item) -> {}); // Consume all to add all itens in cache
assertEquals(6, req.getCount());
/**
* getting a sub-interval of past weather should return from cache
*/
infos = loc.getPastWeather(of(2017,02,20), of(2017,03,10));
infos.iterator().next();
assertEquals(6, req.getCount());
}
示例13: of
import java.util.stream.Stream; //導入方法依賴的package包/類
static KitNode of(Stream<Kit> kits) {
return new KitNodeImpl(Stream.empty(), new All<>(kits.map(Unit::new)), StaticFilter.ALLOW, empty(), empty());
}
示例14: indent
import java.util.stream.Stream; //導入方法依賴的package包/類
private Stream<String> indent(Stream<String> lines) {
return lines.map(line -> " " + line);
}
示例15: parse
import java.util.stream.Stream; //導入方法依賴的package包/類
public Stream<LenderRecord> parse(Stream<String> stream) {
return stream.map(s -> parse(s));
}