public double averageNumberOfTracks(List<Album> albums) { return albums.stream() .collect(averagingInt(album -> album.getTrackList().size())); } // END averagingTracks
double average = numbers.stream().collect(Collectors.averagingInt((p) -> p - 1)); System.out.println("average = " + average); // print average = 1.5
public static void main(String[] args) { List<Project> projects = Project.buildData(); Double collect = projects.stream() .collect(averagingInt(Project::getStars)); System.out.println(collect); System.out.println(Stream.of("Hello", "Java8") .collect(joining(","))); Integer collect1 = projects.stream() .collect(reducing(0, Project::getStars, (x, y) -> x + y)); System.out.println(collect1); Optional<Integer> collect2 = projects.stream() .map(Project::getStars) .collect(reducing((x, y) -> x + y)); System.out.println(collect2); } }
private static void test3(List<Person> persons) { Double averageAge = persons .stream() .collect(Collectors.averagingInt(p -> p.age)); System.out.println(averageAge); // 19.0 }
private static double maxDeviationIn(Collection<Integer> values) { double average = values.stream().collect(Collectors.averagingInt(it -> it)); return values.stream().map(it -> abs(it - average)).reduce(0.0D, Math::max); } }
@Override public double avgInt(ToIntFunction<? super T> function) { return collect(Collectors.averagingInt(function)); }
@Override public double avgInt(ToIntFunction<? super T> function) { return collect(Collectors.averagingInt(function)); }
import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.stream.Collectors; public class FindAverage { public static void main(String[] args) throws IOException, URISyntaxException { Double average = Arrays .stream(Files .lines(Paths.get(ClassLoader.getSystemResource( "input.txt").toURI())) .reduce((a, b) -> a + " " + b).map(e -> e.split(" ")) .get()).filter(e -> e.matches("\\d+")) .map(Integer::new) .collect(Collectors.averagingInt(Integer::intValue)); System.out.println("Average = " + average); } }
System.out.println(personByAge); Double averageAge = persons.stream().collect(Collectors.averagingInt(Person::getAge)); System.out.println(averageAge);
works.queued.drainTo(works.working, worker.getMaxBatchSize()); if (!works.working.isEmpty()) { double priority = works.working.stream().collect(Collectors.averagingInt(Prioritized::getPriority)); workExecutor.execute(new PrioritizedRunnable((int)priority) {
collect.forEach((k, v) -> System.out.println(k + " " + v.stream().collect(Collectors.averagingInt(Information::getAge))));
System.out.println(students.stream().collect(averagingInt(Student::getScore))); System.out.println(students.stream().collect(summingInt(Student::getScore))); IntSummaryStatistics intSummaryStatistics = students.stream().collect(summarizingInt(Student::getScore));
public void testObjCollectorIntAverager() { Cache<Integer, String> cache = getCache(0); int range = 10; // First populate the cache with a bunch of values IntStream.range(0, range).boxed().forEach(i -> cache.put(i, i + "-value")); assertEquals(range, cache.size()); CacheSet<Map.Entry<Integer, String>> entrySet = cache.entrySet(); assertEquals(4.5, createStream(entrySet).collect( () -> Collectors.averagingInt(Map.Entry::getKey))); }
private static double calculateMeanOfVertex(IntegerVertex vertex) { KeanuProbabilisticModel model = new KeanuProbabilisticModel(vertex.getConnectedGraph()); return MetropolisHastings.withDefaultConfigFor(model, KeanuRandom.getDefaultRandom()) .generatePosteriorSamples(model, Collections.singletonList(vertex)).stream() .limit(2000) .collect(Collectors.averagingInt((NetworkSample state) -> state.get(vertex).scalar())); } }