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

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

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

IntStream.count介绍

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

return mapToLong(e -> 1L).sum();

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

return mapToLong(e -> 1L).sum();

这是一个terminal operation

代码示例

代码示例来源:origin: biezhi/30-seconds-of-java8

/**
 * Counts the occurrences of a value in an array.
 *
 * @param numbers Array of numbers
 * @param value   the value for which we have to count occurrences
 * @return count of total number of occurrences of the value
 */
public static long countOccurrences(int[] numbers, int value) {
  return Arrays.stream(numbers)
      .filter(number -> number == value)
      .count();
}

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

@Override
    // BEGIN functional
public long countPrimes(int upTo) {
  return IntStream.range(1, upTo)
          .filter(this::isPrime)
          .count();
}

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

@Override
public int poolSize() {
  return (int) connectionManagers.values().stream()
    .mapToInt(man -> man.counter.get())
    .filter(c -> c == 0L)
    .count();
}

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

public static long phi(int m) {
    return IntStream.rangeClosed(1, m).filter(r -> P38.coprime(r, m)).count();
  }
}

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

@Override
    // BEGIN parallel_functional
public long countPrimes(int upTo) {
  return IntStream.range(1, upTo)
          .parallel()
          .filter(this::isPrime)
          .count();
}

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

public static int countLowercaseLetters(String string) {
  return (int) string.chars()
            .filter(Character::isLowerCase)
            .count();
}

代码示例来源:origin: google/error-prone

private boolean isProbablyType(String name) {
 Symbol typeSymbol = FindIdentifiers.findIdent(name, state, KindSelector.TYP);
 return typeSymbol instanceof TypeSymbol
   || name.chars().filter(c -> c == '.').count() >= 3
   || name.contains("#");
}

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

@Benchmark
public long java8() {
  // Using Java8
  return testString.chars().filter(ch -> ch =='.').count();
}

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

@Benchmark
public long java8_1() {
  // Using Java8 (case 2)
  return testString.codePoints().filter(ch -> ch == '.').count();
}

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

@Benchmark
public long java8_1() {
  // Using Java8 (case 2)
  return testString.codePoints().filter(ch -> ch == '.').count();
}

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

@Benchmark
public long java8() {
  // Using Java8
  return testString.chars().filter(ch -> ch =='.').count();
}

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

@Override
public <T> long apply(
  final SqlStreamOptimizerInfo<ENTITY> info,
  final SqlStreamTerminator<ENTITY> sqlStreamTerminator,
  final IntPipeline pipeline
) {
  requireNonNull(info);
  requireNonNull(sqlStreamTerminator);
  requireNonNull(pipeline);
  return sqlStreamTerminator.optimize(pipeline).getAsIntStream().count();
}

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

default long count(IntPipeline pipeline) {
  requireNonNull(pipeline);
  return optimize(pipeline).getAsIntStream().count();
}

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

public static long countUniqueCharacters(String input) {
  return input.chars()
      .distinct()
      .count();
}

代码示例来源:origin: ivan-vasilev/neuralnetworks

/**
 * creates a new tensor based on a parent tensor
 * @param parent - the parent tensor
 * @param dimensionsLimit -
 * @param reduceChildDimensions
 */
public Tensor(Tensor parent, int[][] dimensionsLimit, boolean reduceChildDimensions)
{
  this.startOffset = parent.startOffset;
  this.globalDimensions = parent.globalDimensions;
  this.elements = parent.elements;
  this.dimMultiplicators = parent.dimMultiplicators;
  this.globalDimensionsLimit = dimensionsLimit;
  this.dimTmp = new int[globalDimensions.length];
  this.dimensions = new int[reduceChildDimensions ? (int) IntStream.range(0, globalDimensions.length).filter(i -> dimensionsLimit[0][i] != dimensionsLimit[1][i]).count() : parent.dimensions.length];
  for (int i = 0, j = 0; i < globalDimensions.length; i++)
  {
    if (dimensionsLimit[0][i] != dimensionsLimit[1][i] || !reduceChildDimensions)
    {
      dimensions[j++] = dimensionsLimit[1][i] - dimensionsLimit[0][i] + 1;
    }
  }
  size = IntStream.range(0, dimensions.length).map(i -> dimensions[i]).reduce(1, (a, b) -> a * b);
}

代码示例来源:origin: ivan-vasilev/neuralnetworks

/**
 * @param parent
 * @param dimensionsLimit - limit over the parent dimensions
 * @param reduceChildDimensions - reduce the child vector dimensions if they are limited to 1
 * @return
 */
@SuppressWarnings("unchecked")
public static <T extends Tensor> T tensor(Tensor parent, int[][] dimensionsLimit, boolean reduceChildDimensions)
{
  T result = null;
  long dimensions = 0;
  if (reduceChildDimensions) {
    dimensions = IntStream.range(0, dimensionsLimit[0][0]).filter(i -> dimensionsLimit[0][i] != dimensionsLimit[1][i]).count();
  } else {
    dimensions = parent.getDimensions().length;
  }
  if (dimensions <= 2)
  {
    result = (T) new Matrix(parent, dimensionsLimit);
  } else
  {
    result = (T) new Tensor(parent, dimensionsLimit, reduceChildDimensions);
  }
  return result;
}

代码示例来源:origin: prestodb/presto

.mapToInt(Block::getPositionCount)
.distinct()
.count() == 1);

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

@Test
public void testAppend() throws InterruptedException, ExecutionException {
 AsyncTable<?> table = getTable.get();
 int count = 10;
 CountDownLatch latch = new CountDownLatch(count);
 char suffix = ':';
 AtomicLong suffixCount = new AtomicLong(0L);
 IntStream.range(0, count).forEachOrdered(
  i -> table.append(new Append(row).addColumn(FAMILY, QUALIFIER, Bytes.toBytes("" + i + suffix)))
    .thenAccept(r -> {
     suffixCount.addAndGet(Bytes.toString(r.getValue(FAMILY, QUALIFIER)).chars()
       .filter(x -> x == suffix).count());
     latch.countDown();
    }));
 latch.await();
 assertEquals((1 + count) * count / 2, suffixCount.get());
 String value = Bytes.toString(
  table.get(new Get(row).addColumn(FAMILY, QUALIFIER)).get().getValue(FAMILY, QUALIFIER));
 int[] actual = Arrays.asList(value.split("" + suffix)).stream().mapToInt(Integer::parseInt)
   .sorted().toArray();
 assertArrayEquals(IntStream.range(0, count).toArray(), actual);
}

代码示例来源:origin: oracle/opengrok

@Test
public void shouldHandleDocsOfLongerLength() {
  //                                  0             0
  //                    0-- -  5-- - -1--- - 5--- - 2-
  final String INPUT = "ab\r\ncde\r\nefgh\r\nijk\r\nlm";
  SourceSplitter splitter = new SourceSplitter();
  splitter.reset(INPUT);
  assertEquals("split count", 5, splitter.count());
  assertEquals("split position", 0, splitter.getPosition(0));
  assertEquals("split position", 4, splitter.getPosition(1));
  assertEquals("split position", 9, splitter.getPosition(2));
  assertEquals("split position", 15, splitter.getPosition(3));
  assertEquals("split position", 20, splitter.getPosition(4));
  assertEquals("split position", 22, splitter.getPosition(5));
  /*
   * Test findLineOffset() for every character with an alternate
   * computation that counts every LFs.
   */
  for (int i = 0; i < splitter.originalLength(); ++i) {
    char c = INPUT.charAt(i);
    int off = splitter.findLineOffset(i);
    long numLF = INPUT.substring(0, i + 1).chars().filter(ch ->
      ch == '\n').count();
    long exp = numLF - (c == '\n' ? 1 : 0);
    assertEquals("split find-offset of " + i, exp, off);
  }
}

代码示例来源:origin: oracle/opengrok

int off = splitter.findLineOffset(i);
long numLF = INPUT.substring(0, i + 1).chars().filter(ch ->
  ch == '\n').count();
long exp = numLF - (c == '\n' ? 1 : 0);
assertEquals("split find-offset of " + i, exp, off);

相关文章