本文整理汇总了Java中java.util.IntSummaryStatistics类的典型用法代码示例。如果您正苦于以下问题:Java IntSummaryStatistics类的具体用法?Java IntSummaryStatistics怎么用?Java IntSummaryStatistics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IntSummaryStatistics类属于java.util包,在下文中一共展示了IntSummaryStatistics类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSummingItemsInAList
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Test
public void testSummingItemsInAList() {
IntSummaryStatistics stats = names.stream().filter(name -> !name.isEmpty())
.collect(Collectors.summarizingInt(name -> name.length()));
assertEquals(17, stats.getSum());
assertEquals(2, stats.getMin());
assertEquals(5, stats.getMax());
assertEquals(3.4, stats.getAverage(), 0.01);
// Reference method name to call
stats = names.stream().filter(name -> !name.isEmpty()).collect(Collectors.summarizingInt(String::length));
assertEquals(17, stats.getSum());
assertEquals(2, stats.getMin());
assertEquals(5, stats.getMax());
assertEquals(3.4, stats.getAverage(), 0.01);
}
示例2: testIntStatistics
import java.util.IntSummaryStatistics; //导入依赖的package包/类
public void testIntStatistics() {
List<IntSummaryStatistics> instances = new ArrayList<>();
instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
IntSummaryStatistics::accept,
IntSummaryStatistics::combine));
instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
IntSummaryStatistics::accept,
IntSummaryStatistics::combine));
for (IntSummaryStatistics stats : instances) {
assertEquals(stats.getCount(), 1000);
assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
assertEquals(stats.getMax(), 1000);
assertEquals(stats.getMin(), 1);
}
}
示例3: getAsInt
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Override
public int getAsInt() {
BinaryOperator<Dimension> accumulator = new BinaryOperator<Dimension>() {
@Override
public Dimension apply(Dimension t, Dimension u) {
int w = Math.max(t.width, u.width);
int h = Math.max(t.height, u.height);
return new Dimension(w, h);
}
};
Dimension extent = plantingBlockModel.getPlantingBlocks().stream()
.map(pb -> new Dimension( pb.getX() + pb.getColumnCount() - 1, pb.getY() + pb.getRowCount() - 1 ))
.reduce(new Dimension(0,0), accumulator);
IntSummaryStatistics stats = plantingBlockModel.getPlantingBlocks().stream()
.collect(Collectors.summarizingInt(pb -> pb.getColumnCount() * pb.getRowCount()));
int maxSize = stats.getMax();
int maxExtent = extent.width * extent.height;
return Math.max(maxSize, maxExtent);
}
示例4: getStats
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Test
/** Get people statistics: average age, count, maximum age, minimum age and sum og all ages. */
public void getStats() {
List<Person> collection = asList(sara4, eva42, viktor40);
// Stats stats = Java7Impl.getStats(collection);
IntSummaryStatistics stats = collection.stream() //
.mapToInt(Person::getAge) //
.summaryStatistics();
assertEquals((double) (4 + 40 + 42) / 3, stats.getAverage(),.0001);
assertEquals(3, stats.getCount());
assertEquals(42, stats.getMax());
assertEquals(4, stats.getMin());
assertEquals(40 + 42 + 4, stats.getSum());
// collection.stream().mapToInt(Person::getAge).summaryStatistics()
}
示例5: segmentStatistics
import java.util.IntSummaryStatistics; //导入依赖的package包/类
/**
* Given a collection of items and a <code>Predicate</code> to be fulfilled returns an
* <code>IntSummaryStatistics</code> over a list of the lengths of segments of successive (as determined by the
* order they are returned by the iterator of the collection) items that fulfill the predicate.
*
* @param coll
* the collection of items
* @param test
* the predicate to fulfill
* @param <T>
* the type of the items
* @return an <code>IntSummaryStatistics</code> over the list of segment lengths
*/
static <T> IntSummaryStatistics segmentStatistics(Collection<T> coll, Predicate<T> test) {
List<Integer> chunkSizes = new ArrayList<>();
int currentSize = 0;
for (T item : coll) {
boolean p = test.test(item);
if (p) {
currentSize++;
} else {
if (currentSize != 0) {
chunkSizes.add(currentSize);
currentSize = 0;
}
}
}
if (currentSize != 0) {
chunkSizes.add(currentSize);
}
return chunkSizes.stream().collect(Collectors.summarizingInt(i -> i));
}
示例6: testSummarizing
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Test
public void testSummarizing() {
withRandom(r -> {
int[] data = IntStreamEx.of(r, 1000, 1, Integer.MAX_VALUE).toArray();
IntSummaryStatistics expected = IntStream.of(data).summaryStatistics();
IntSummaryStatistics statistics = IntStreamEx.of(data).collect(IntCollector.summarizing());
assertEquals(expected.getCount(), statistics.getCount());
assertEquals(expected.getSum(), statistics.getSum());
assertEquals(expected.getMax(), statistics.getMax());
assertEquals(expected.getMin(), statistics.getMin());
statistics = IntStreamEx.of(data).parallel().collect(IntCollector.summarizing());
assertEquals(expected.getCount(), statistics.getCount());
assertEquals(expected.getSum(), statistics.getSum());
assertEquals(expected.getMax(), statistics.getMax());
assertEquals(expected.getMin(), statistics.getMin());
});
}
示例7: getGeneratedInputCountFromOutputLines
import java.util.IntSummaryStatistics; //导入依赖的package包/类
/**
* @return a number >= 0 if it is present in the output file, otherwise -1
*/
private static int getGeneratedInputCountFromOutputLines(List<String> outputLines) {
IntSummaryStatistics ints = outputLines.stream()
.map(line -> TEST_COUNT_LINE_PATTERN.matcher(line.trim()))
.filter(m -> m.matches())
.mapToInt(m -> Integer.parseInt(m.group(1)))
.summaryStatistics();
if (ints.getMin() < 0) {
throw new RuntimeException("RANDOOP: Number of tests is negative:" + outputLines);
} else if (ints.getCount() > 0) {
return (int) ints.getSum();
} else {
return -1;
}
}
示例8: ex22_mapLengthToWordCount
import java.util.IntSummaryStatistics; //导入依赖的package包/类
/**
* Categorize the words from the text file into a map, where the map's key
* is the length of each word, and the value corresponding to a key is a
* count of words of that length. Don't bother with uniqueness or lower-
* casing the words. This is the same as the previous exercise except
* the map values are the count of words instead of a list of words.
*
* @throws IOException
*/
@Test
public void ex22_mapLengthToWordCount() throws IOException {
Map<Integer, Long> result = reader.lines().flatMap(line -> Arrays.stream(line.split(REGEXP)))
.collect(Collectors.groupingBy(s -> s.length(), Collectors.counting()));
assertEquals( 1L, (long)result.get(1));
assertEquals(11L, (long)result.get(2));
assertEquals(28L, (long)result.get(3));
assertEquals(21L, (long)result.get(4));
assertEquals(16L, (long)result.get(5));
assertEquals(12L, (long)result.get(6));
assertEquals(10L, (long)result.get(7));
assertEquals( 3L, (long)result.get(8));
assertEquals( 2L, (long)result.get(9));
assertEquals( 2L, (long)result.get(10));
assertEquals( 1L, (long)result.get(11));
IntSummaryStatistics stats = result.keySet().stream().mapToInt(i -> i).summaryStatistics();
assertEquals("min key", 1, stats.getMin());
assertEquals("max key", 11, stats.getMax());
}
示例9: ex22_mapLengthToWordCount
import java.util.IntSummaryStatistics; //导入依赖的package包/类
/**
* Categorize the words from the text file into a map, where the map's key
* is the length of each word, and the value corresponding to a key is a
* count of words of that length. Don't bother with uniqueness or lower-
* casing the words. This is the same as the previous exercise except
* the map values are the count of words instead of a list of words.
*
* @throws IOException
*/
@Test
@Ignore
public void ex22_mapLengthToWordCount() throws IOException {
Map<Integer, Long> result = null; // TODO
assertEquals(1L, (long) result.get(1));
assertEquals(11L, (long) result.get(2));
assertEquals(28L, (long) result.get(3));
assertEquals(21L, (long) result.get(4));
assertEquals(16L, (long) result.get(5));
assertEquals(12L, (long) result.get(6));
assertEquals(10L, (long) result.get(7));
assertEquals(3L, (long) result.get(8));
assertEquals(2L, (long) result.get(9));
assertEquals(2L, (long) result.get(10));
assertEquals(1L, (long) result.get(11));
IntSummaryStatistics stats = result.keySet().stream().mapToInt(i -> i).summaryStatistics();
assertEquals("min key", 1, stats.getMin());
assertEquals("max key", 11, stats.getMax());
}
示例10: ex22_mapLengthToWordCount
import java.util.IntSummaryStatistics; //导入依赖的package包/类
/**
* Categorize the words from the text file into a map, where the map's key
* is the length of each word, and the value corresponding to a key is a
* count of words of that length. Don't bother with uniqueness or lower-
* casing the words. This is the same as the previous exercise except
* the map values are the count of words instead of a list of words.
*
* @throws IOException
*/
@Test @Ignore
public void ex22_mapLengthToWordCount() throws IOException {
Map<Integer, Long> result = null; // TODO
assertEquals( 1L, (long)result.get(1));
assertEquals(11L, (long)result.get(2));
assertEquals(28L, (long)result.get(3));
assertEquals(21L, (long)result.get(4));
assertEquals(16L, (long)result.get(5));
assertEquals(12L, (long)result.get(6));
assertEquals(10L, (long)result.get(7));
assertEquals( 3L, (long)result.get(8));
assertEquals( 2L, (long)result.get(9));
assertEquals( 2L, (long)result.get(10));
assertEquals( 1L, (long)result.get(11));
IntSummaryStatistics stats = result.keySet().stream().mapToInt(i -> i).summaryStatistics();
assertEquals("min key", 1, stats.getMin());
assertEquals("max key", 11, stats.getMax());
}
示例11: int_summary_stats_with_stream
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Test
public void int_summary_stats_with_stream() {
IntSummaryStatistics stats = orderEntries.stream()
.mapToInt((x) -> x.getAmount()).summaryStatistics();
// average
assertEquals(13.5, stats.getAverage(), 0);
// count
assertEquals(4, stats.getCount(), 0);
// max
assertEquals(18, stats.getMax(), 0);
// min
assertEquals(10, stats.getMin(), 0);
// sum
assertEquals(54, stats.getSum(), 0);
}
示例12: int_summary_stats_stream_reduction_target
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Test
public void int_summary_stats_stream_reduction_target() {
IntSummaryStatistics stats = orderEntries.stream().collect(
Collectors.summarizingInt(OrderEntry::getAmount));
// average
assertEquals(13.5, stats.getAverage(), 0);
// count
assertEquals(4, stats.getCount(), 0);
// max
assertEquals(18, stats.getMax(), 0);
// min
assertEquals(10, stats.getMin(), 0);
// sum
assertEquals(54, stats.getSum(), 0);
}
示例13: printTrackLengthStatistics
import java.util.IntSummaryStatistics; //导入依赖的package包/类
public static void printTrackLengthStatistics(Album album) {
IntSummaryStatistics trackLengthStats
= album.getTracks()
.mapToInt(track -> track.getLength())
.summaryStatistics();
System.out.printf("Max: %d, Min: %d, Ave: %f, Sum: %d",
trackLengthStats.getMax(),
trackLengthStats.getMin(),
trackLengthStats.getAverage(),
trackLengthStats.getSum());
}
开发者ID:jinyi233,项目名称:https-github.com-RichardWarburton-java-8-Lambdas-exercises,代码行数:13,代码来源:Primitives.java
示例14: combinedStatisticsJDK_parallel
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Benchmark
public Object[] combinedStatisticsJDK_parallel()
{
DoubleSummaryStatistics stats1 =
Person.getJDKPeople().parallelStream().mapToDouble(Person::getHeightInInches).summaryStatistics();
DoubleSummaryStatistics stats2 =
Person.getJDKPeople().parallelStream().mapToDouble(Person::getWeightInPounds).summaryStatistics();
IntSummaryStatistics stats3 =
Person.getJDKPeople().parallelStream().mapToInt(Person::getAge).summaryStatistics();
return new Object[]{stats1, stats2, stats3};
}
示例15: combinedStatisticsECStream_parallel
import java.util.IntSummaryStatistics; //导入依赖的package包/类
@Benchmark
public Object[] combinedStatisticsECStream_parallel()
{
DoubleSummaryStatistics stats1 =
Person.getECPeople().parallelStream().mapToDouble(Person::getHeightInInches).summaryStatistics();
DoubleSummaryStatistics stats2 =
Person.getECPeople().parallelStream().mapToDouble(Person::getWeightInPounds).summaryStatistics();
IntSummaryStatistics stats3 =
Person.getECPeople().parallelStream().mapToInt(Person::getAge).summaryStatistics();
return new Object[]{stats1, stats2, stats3};
}