java.util.stream.IntStream.summaryStatistics()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(132)

本文整理了Java中java.util.stream.IntStream.summaryStatistics()方法的一些代码示例,展示了IntStream.summaryStatistics()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IntStream.summaryStatistics()方法的具体详情如下:
包路径:java.util.stream.IntStream
类名称:IntStream
方法名:summaryStatistics

IntStream.summaryStatistics介绍

[英]Returns an IntSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.

This is a terminal operation.
[中]返回一个IntSummaryStatistics,描述有关此流元素的各种摘要数据。这是reduction的一个特例。
这是一个terminal operation

代码示例

代码示例来源:origin: speedment/speedment

@Override
  public IntSummaryStatistics execute() {
    try (final IntStream stream = buildPrevious()) {
      return stream.summaryStatistics();
    }
  }
}

代码示例来源:origin: speedment/speedment

default IntSummaryStatistics summaryStatistics(IntPipeline pipeline) {
  requireNonNull(pipeline);
  return optimize(pipeline).getAsIntStream().summaryStatistics();
}

代码示例来源:origin: RichardWarburton/java-8-lambdas-exercises

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());
}
// END printTrackLengthStatistics

代码示例来源:origin: stackoverflow.com

import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class SOTest {
  public static void main(String[] args){
    int[] tab = {12, 1, 21, 8};
    IntSummaryStatistics stat = Arrays.stream(tab).summaryStatistics();
    int min = stat.getMin();
    int max = stat.getMax();
    System.out.println("Min = " + min);
    System.out.println("Max = " + max);
  }
}

代码示例来源:origin: net.dongliu/commons-lang

@Override
public IntSummaryStatistics summaryStatistics() {
  return stream.summaryStatistics();
}

代码示例来源:origin: osmlab/atlas

/**
 * @param collection
 *            The collection of items
 * @return A state object for collecting statistics such as count, min, max, sum, and average.
 */
public static IntSummaryStatistics summarizingInt(final Collection<Integer> collection)
{
  return collection.stream().mapToInt(value -> value).summaryStatistics();
}

代码示例来源:origin: osmlab/atlas

/**
 * Get summary statistic from an object collection. An example to get summary of age from a
 * Person list would be
 * <p>
 * <code> {@literal IntSummaryStatistics stat = StatisticUtils.summarizingInt(list, x->x.getAge());} </code>
 * </p>
 *
 * @param collection
 *            The collection of items
 * @param function
 *            You need to specify the function to get int value for each object T
 * @param <T>
 *            The type of the statistic
 * @return A state object for collecting statistics such as count, min, max, sum, and average.
 */
public static <T> IntSummaryStatistics summarizingInt(final Collection<T> collection,
    final ToIntFunction<? super T> function)
{
  return collection.stream().mapToInt(function).summaryStatistics();
}

代码示例来源:origin: se.ugli.ugli-commons/ugli-commons

@Override
public IntSummaryStatistics summaryStatistics() {
  // This is a terminal operation
  return evalAndclose(() -> stream.summaryStatistics());
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
  int myarr[]={1,2,3,4,4,5,6,5,7,8,4};
  IntSummaryStatistics statisticalData=Arrays.stream(myarr).summaryStatistics();
  System.out.println("Average is " + statisticalData.getAverage());
  System.out.println("Sum is " + statisticalData.getSum());
}

代码示例来源:origin: com.speedment.runtime/runtime-core

default IntSummaryStatistics summaryStatistics(IntPipeline pipeline) {
  requireNonNull(pipeline);
  return optimize(pipeline).getAsIntStream().summaryStatistics();
}

代码示例来源:origin: BruceEckel/OnJava8-Examples

public static void main(String[] args) {
  System.out.println(rands().average().getAsDouble());
  System.out.println(rands().max().getAsInt());
  System.out.println(rands().min().getAsInt());
  System.out.println(rands().sum());
  System.out.println(rands().summaryStatistics());
 }
}

代码示例来源:origin: stackoverflow.com

IntStream s = arr.stream().mapToInt(i -> Integer::intValue).filter(i < 10);
IntSummaryStatistics stats = s.summaryStatistics();

double average = stats.getAverage();
int max = stats.getMax();

代码示例来源:origin: stackoverflow.com

Random r = new Random();
IntStream randomStream = r.ints(10000,0, 2);
IntSummaryStatistics stats =  randomStream.summaryStatistics();
System.out.println("Heads: "+ stats.getSum());
System.out.println("Tails: "+(stats.getCount()-stats.getSum()));

代码示例来源:origin: com.aol.cyclops/cyclops-streams

/**
   * Perform an asynchronous summaryStatistics operation
   * @see java.util.stream.Stream#mapToInt(ToIntFunction)
   *      @see java.util.stream.IntStream#summaryStatistics()
   * */
  default  CompletableFuture<IntSummaryStatistics> summaryStatisticsInt(ToIntFunction<? super T> fn){
    
    return CompletableFuture.supplyAsync(()->getStream()
                .flatMapToInt(t-> IntStream.of(fn.applyAsInt(t)))
                .summaryStatistics(),getExec());
      
  }
}

代码示例来源:origin: xiangwbs/springboot

System.out.println("join:" + String.join(",", abc));
IntSummaryStatistics statistics = lists.stream().mapToInt(x -> x).summaryStatistics();
System.out.println("List中最大的数字 : " + statistics.getMax());
System.out.println("List中最小的数字 : " + statistics.getMin());

代码示例来源:origin: FlareBot/FlareBot

@Override
public JSONObject processData() {
  JSONObject object = new JSONObject();
  IntSummaryStatistics statistics;
  try {
    statistics = FlareBot.instance().getMusicManager().getPlayers().stream()
        .mapToInt(player -> player.getPlaylist().size())
        .summaryStatistics();
  } catch (IllegalArgumentException ignored) {
    return null;
  }
  object.put("data", new JSONObject()
      .put("guilds", Getters.getGuildCache().size())
      .put("loaded_guilds", FlareBotManager.instance().getGuilds().size())
      .put("guilds_with_songs", statistics.getCount())
      .put("songs", statistics.getSum()));
  return object;
}

代码示例来源:origin: ucarGroup/DataLink

.stream()
.mapToInt(Long::intValue)
.summaryStatistics()
.getMin();

代码示例来源:origin: ucarGroup/DataLink

writerStatistic.setRecordsCountAfterGroup((int) result.stream().mapToInt(i -> i.getRecords().size()).summaryStatistics().getSum());

代码示例来源:origin: MegaMek/megamek

return retVal.stream().mapToInt(Integer::intValue).summaryStatistics();

代码示例来源:origin: eclipse/eclipse-collections-kata

@Test
public void getAgeStatisticsOfPets()
{
  // Try to use a MutableIntList here instead
  // Hints: flatMap = flatCollect, map = collect, mapToInt = collectInt
  MutableList<Integer> petAges = this.people
      .stream()
      .flatMap(person -> person.getPets().stream())
      .map(pet -> pet.getAge())
      .collect(Collectors.toCollection(FastList::new));
  // Try to use an IntSet here instead
  Set<Integer> uniqueAges = petAges.toSet();
  // IntSummaryStatistics is a class in JDK 8 - Try and use it with MutableIntList.forEach()
  IntSummaryStatistics stats = petAges.stream().mapToInt(i -> i).summaryStatistics();
  // Is a Set<Integer> equal to an IntSet?
  // Hint: Try IntSets instead of Sets as the factory
  Assert.assertEquals(Sets.mutable.with(1, 2, 3, 4), uniqueAges);
  // Try to leverage min, max, sum, average from the Eclipse Collections primitive api
  Assert.assertEquals(stats.getMin(), petAges.stream().mapToInt(i -> i).min().getAsInt());
  Assert.assertEquals(stats.getMax(), petAges.stream().mapToInt(i -> i).max().getAsInt());
  Assert.assertEquals(stats.getSum(), petAges.stream().mapToInt(i -> i).sum());
  Assert.assertEquals(stats.getAverage(), petAges.stream().mapToInt(i -> i).average().getAsDouble(), 0.0);
  Assert.assertEquals(stats.getCount(), petAges.size());
  // Hint: Match = Satisfy
  Assert.assertTrue(petAges.stream().allMatch(i -> i > 0));
  Assert.assertFalse(petAges.stream().anyMatch(i -> i == 0));
  Assert.assertTrue(petAges.stream().noneMatch(i -> i < 0));
  // Don't forget to comment this out or delete it when you are done
  Assert.fail("Refactor to Eclipse Collections");
}

相关文章

微信公众号

最新文章

更多