本文整理匯總了Java中java.util.stream.Stream.distinct方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.distinct方法的具體用法?Java Stream.distinct怎麽用?Java Stream.distinct使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.distinct方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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);
}
示例2: 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());
}
示例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: 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());
}
示例5: 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());
}
示例6: testLazyFilterAndMapAndDistinct
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testLazyFilterAndMapAndDistinct() {
ICounter<String, CompletableFuture<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)).join();
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());
}
示例7: list
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
public Stream<String> list() throws IOException {
Stream<String> s = delegate().list();
for (ResourceFinder finder : finders) {
s = Stream.concat(s, finder.list());
}
return s.distinct();
}
示例8: testWeatherServiceLazyAndCache
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void testWeatherServiceLazyAndCache(){
/**
* Arrange WeatherService --> WeatherWebApi --> Countify --> FileRequest
*/
ICounter<String, Stream<String>> req = Countify.of(new FileRequest()::getContent);
// Function<String, Iterable<String>> cache = Cache.memoize(req);
WeatherService api = new WeatherServiceCache(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());
/**
* Counts 1 request when iterates to get the first Location
*/
Location loc = locals.iterator().next();
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());
temps = api.search("Porto")
.findFirst().get()
.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(3, 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(3, 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(4, 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(4, req.getCount());
}
示例9: 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());
}