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

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

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

IntStream.sum介绍

[英]Returns the sum of elements in this stream. This is a special case of a reduction and is equivalent to:

return reduce(0, Integer::sum);

This is a terminal operation.
[中]返回此流中元素的总和。这是reduction的一个特例,相当于:

return reduce(0, Integer::sum);

这是一个terminal operation

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public DefaultDataBuffer write(ByteBuffer... buffers) {
  if (!ObjectUtils.isEmpty(buffers)) {
    int capacity = Arrays.stream(buffers).mapToInt(ByteBuffer::remaining).sum();
    ensureCapacity(capacity);
    Arrays.stream(buffers).forEach(this::write);
  }
  return this;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * {@inheritDoc}
 * <p>This implementation creates a single {@link DefaultDataBuffer}
 * to contain the data in {@code dataBuffers}.
 */
@Override
public DefaultDataBuffer join(List<? extends DataBuffer> dataBuffers) {
  Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
  int capacity = dataBuffers.stream().mapToInt(DataBuffer::readableByteCount).sum();
  DefaultDataBuffer result = allocateBuffer(capacity);
  dataBuffers.forEach(result::write);
  dataBuffers.forEach(DataBufferUtils::release);
  return result;
}

代码示例来源:origin: shekhargulati/99-problems

public static <T> long lengthStream1(List<T> list) {
  return list.stream().mapToInt(x -> 1).sum();
}

代码示例来源:origin: apache/incubator-druid

public Collection<ServerHolder> getAllServers()
{
 final int historicalSize = historicals.values().stream().mapToInt(Collection::size).sum();
 final int realtimeSize = realtimes.size();
 final List<ServerHolder> allServers = new ArrayList<>(historicalSize + realtimeSize);
 historicals.values().forEach(allServers::addAll);
 allServers.addAll(realtimes);
 return allServers;
}

代码示例来源:origin: apache/incubator-druid

@Override
public int getQueryCount()
{
 return rels.stream().mapToInt(rel -> ((DruidRel) rel).getQueryCount()).sum();
}

代码示例来源:origin: apache/pulsar

@Override
public int getAvailablePermits() {
  return consumers.values().stream().mapToInt(ConsumerImpl::getAvailablePermits).sum();
}

代码示例来源:origin: apache/incubator-druid

public int size(T x)
{
 return fieldWriters.stream().mapToInt(w -> w.size(x)).sum();
}

代码示例来源:origin: twosigma/beakerx

public static int totalPoints(Number[][] data) {
 return Arrays.stream(data).
     mapToInt(x -> x.length).sum();
}

代码示例来源:origin: apache/storm

@Override
  protected Integer transform(ClusterSummary clusterSummary) {
    return clusterSummary.get_supervisors().stream()
        .mapToInt(SupervisorSummary::get_num_workers)
        .sum();
  }
});

代码示例来源:origin: apache/incubator-druid

static long requiredBufferCapacity(
  int cardinality,
  AggregatorFactory[] aggregatorFactories
)
{
 final int cardinalityWithMissingValue = cardinality + 1;
 final int recordSize = Arrays.stream(aggregatorFactories)
                .mapToInt(AggregatorFactory::getMaxIntermediateSizeWithNulls)
                .sum();
 return getUsedFlagBufferCapacity(cardinalityWithMissingValue) +  // total used flags size
   (long) cardinalityWithMissingValue * recordSize;                 // total values size
}

代码示例来源:origin: apache/storm

@Override
  protected Integer transform(ClusterSummary clusterSummary) {
    return clusterSummary.get_supervisors().stream()
        .mapToInt(SupervisorSummary::get_num_used_workers)
        .sum();
  }
});

代码示例来源:origin: Netflix/eureka

/**
 * @return a weakly consistent size of the number of instances in all the
 *         applications
 */
public int size() {
  return applications.stream().mapToInt(Application::size).sum();
}

代码示例来源:origin: Vedenin/useful-java-links

private static void testFlatMapToInt() {
  System.out.println();
  System.out.println("Test flat map start");
  Collection<String> collection = Arrays.asList("1,2,0", "4,5");
  // Get sum all digit from strings
  int sum = collection.stream().flatMapToInt((p) -> Arrays.asList(p.split(",")).stream().mapToInt(Integer::parseInt)).sum();
  System.out.println("sum = " + sum); // print  sum = 12
}

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

private int addIntegers(List<Integer> values) {
  return values.parallelStream()
         .mapToInt(i -> i)
         .sum();
}
  // END addIntegers

代码示例来源:origin: org.springframework/spring-core

/**
 * {@inheritDoc}
 * <p>This implementation creates a single {@link DefaultDataBuffer}
 * to contain the data in {@code dataBuffers}.
 */
@Override
public DefaultDataBuffer join(List<? extends DataBuffer> dataBuffers) {
  Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
  int capacity = dataBuffers.stream().mapToInt(DataBuffer::readableByteCount).sum();
  DefaultDataBuffer result = allocateBuffer(capacity);
  dataBuffers.forEach(result::write);
  dataBuffers.forEach(DataBufferUtils::release);
  return result;
}

代码示例来源:origin: apache/pulsar

@Override
public int numMessagesInQueue() {
  return incomingMessages.size() + consumers.values().stream().mapToInt(ConsumerImpl::numMessagesInQueue).sum();
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {
  if (shouldIgnoreIndex) {
    return null;
  }
  try {
    Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
    if (!urls.hasMoreElements()) {
      return null;
    }
    List<Properties> result = new ArrayList<>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
      result.add(properties);
    }
    if (logger.isDebugEnabled()) {
      logger.debug("Loaded " + result.size() + "] index(es)");
    }
    int totalCount = result.stream().mapToInt(Properties::size).sum();
    return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
  }
  catch (IOException ex) {
    throw new IllegalStateException("Unable to load indexes from location [" +
        COMPONENTS_RESOURCE_LOCATION + "]", ex);
  }
}

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

@Override
public int poolSize() {
  return pools
    .values()
    .stream()
    .mapToInt(Collection::size)
    .sum();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public int serial_lazy_collectIntSum_jdk()
{
  return this.integersJDK.stream().mapToInt(each -> each).sum();
}

代码示例来源:origin: apache/incubator-dubbo

public static int getConsumerAddressNum(String serviceUniqueName) {
    Set<ConsumerInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getConsumerInvoker(serviceUniqueName);
    return providerInvokerWrapperSet.stream()
        .map(w -> w.getRegistryDirectory().getUrlInvokerMap())
        .filter(Objects::nonNull)
        .mapToInt(Map::size).sum();
  }
}

相关文章