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

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

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

IntStream.reduce介绍

[英]Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. This is equivalent to:

int result = identity;

but is not constrained to execute sequentially.

The identity value must be an identity for the accumulator function. This means that for all x, accumulator.apply(identity, x) is equal to x. The accumulator function must be an associative function.

This is a terminal operation.
[中]

代码示例

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

@Override
  public OptionalInt execute() {
    try (final IntStream stream = buildPrevious()) {
      if (hasInitialValue) {
        return OptionalInt.of(stream.reduce(initialValue, combiner));
      } else {
        return stream.reduce(combiner);
      }
    }
  }
}

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

/**
 * Calculates the greatest common denominator (gcd) of an array of numbers
 *
 * @param numbers Array of numbers
 * @return gcd of array of numbers
 */
public static OptionalInt gcd(int[] numbers) {
  return Arrays.stream(numbers)
      .reduce((a, b) -> gcd(a, b));
}

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

default OptionalInt reduce(IntPipeline pipeline, IntBinaryOperator op) {
  requireNonNull(pipeline);
  requireNonNull(op);
  return optimize(pipeline).getAsIntStream().reduce(op);
}

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

/**
 * Calculates the lowest common multiple (lcm) of an array of numbers.
 *
 * @param numbers Array of numbers
 * @return lcm of array of numbers
 */
public static OptionalInt lcm(int[] numbers) {
  IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y);
  return Arrays.stream(numbers)
      .reduce((a, b) -> lcm.applyAsInt(a, b));
}

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

default int reduce(IntPipeline pipeline, int identity, IntBinaryOperator op) {
  requireNonNull(pipeline);
  return optimize(pipeline).getAsIntStream().reduce(identity, op);
}

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

public static int phi(int m) {
    return P33.primeFactorsMult(m)
        .stream()
        .map(entry -> (entry.getKey() - 1) * Math.pow(entry.getKey(), entry.getValue() - 1))
        .mapToInt(Double::intValue)
        .reduce(1, (i1, i2) -> i1 * i2);
  }
}

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

@SuppressWarnings("unchecked")
public static <T extends Tensor> T tensor(int... dimensions)
{
  float[] elements = new float[IntStream.of(dimensions).reduce(1, (a, b) -> a * b)];
  int[][] dimensionsLimit = new int[2][dimensions.length];
  IntStream.range(0, dimensions.length).forEach(i -> dimensionsLimit[1][i] = dimensions[i] - 1);
  T result = null;
  if (dimensions.length == 2)
  {
    result = (T) new Matrix(0, elements, dimensions, dimensionsLimit);
  } else
  {
    result = (T) new Tensor(0, elements, dimensions, dimensionsLimit);
  }
  return result;
}

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

/**
 * Create multiple combined tensors using shared elements array
 * 
 * @param dimensions
 *          - dimensions for each tensor
 * @return array of tensors
 */
public static Tensor[] tensor(int[]... dimensions)
{
  Tensor[] result = new Tensor[dimensions.length];
  float[] elements = new float[Arrays.stream(dimensions).map(d -> {
    return IntStream.of(d).reduce(1, (a, b) -> a * b);
  }).reduce(0, (a, b) -> a + b)];
  for (int i = 0, offset = 0; i < dimensions.length; i++)
  {
    int[] d = dimensions[i];
    int[][] dimensionsLimit = new int[2][d.length];
    IntStream.range(0, d.length).forEach(j -> dimensionsLimit[1][j] = d[j] - 1);
    if (d.length == 2)
    {
      result[i] = new Matrix(offset, elements, d, dimensionsLimit);
    } else
    {
      result[i] = new Tensor(offset, elements, d, dimensionsLimit);
    }
    offset += IntStream.of(d).reduce(1, (a, b) -> a * b);
  }
  return result;
}

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

@Override
public int reduce(int identity, IntBinaryOperator op) {
  return finallyClose(() -> stream().reduce(identity, op));
}

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

@Override
public OptionalInt reduce(IntBinaryOperator op) {
  return finallyClose(() -> stream().reduce(op));
}

代码示例来源: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: Vedenin/useful-java-links

int younger = peoples.stream().filter((p) -> p.getName().contains("e")).mapToInt(People::getAge).reduce((s1, s2) -> s1 < s2 ? s1 : s2).orElse(0);
System.out.println("younger = " + younger); // print  23

代码示例来源:origin: opentripplanner/OpenTripPlanner

public static Map<String, Histogram> buildAll (int[] times, PointSet targets) {
  try {
    int size = IntStream.of(times).reduce(0, (memo, i) -> i != Integer.MAX_VALUE ? Math.max(i, memo) : memo) + 1;

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

public Tensor(int startOffset, float[] elements, int[] globalDimensions, int[][] globalDimensionsLimit)
{
  super();
  if (globalDimensions == null || globalDimensions.length == 0)
  {
    throw new IllegalArgumentException("Please provide dimensions");
  }
  this.startOffset = startOffset;
  this.elements = elements;
  this.globalDimensions = globalDimensions;
  this.globalDimensionsLimit = globalDimensionsLimit;
  this.dimTmp = new int[globalDimensions.length];
  this.dimensions = new int[(int) IntStream.range(0, globalDimensions.length)
      .filter(i -> globalDimensionsLimit[0][i] != globalDimensionsLimit[1][i] || globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1 == globalDimensions[i]).count()];
  for (int i = 0, j = 0; i < globalDimensions.length; i++)
  {
    if (globalDimensionsLimit[0][i] != globalDimensionsLimit[1][i] || globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1 == globalDimensions[i])
    {
      dimensions[j++] = globalDimensionsLimit[1][i] - globalDimensionsLimit[0][i] + 1;
    }
  }
  this.dimMultiplicators = new int[dimensions.length];
  IntStream.range(0, dimMultiplicators.length).forEach(i -> {
    dimMultiplicators[i] = 1;
    Arrays.stream(globalDimensions).skip(i + 1).limit(globalDimensions.length).forEach(j -> dimMultiplicators[i] *= j);
  });
  size = IntStream.range(0, dimensions.length).map(i -> dimensions[i]).reduce(1, (a, b) -> a * b);
}

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

newBaseExpressions = Math.multiplyExact(subPredicates.stream()
    .mapToInt(Set::size)
    .reduce(Math::multiplyExact)
    .getAsInt(), subPredicates.size());

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

int size = Arrays.stream(dimensions).reduce(1, (a, b) -> a * b);
float[] elements = null;
int offset = 0;

代码示例来源:origin: reactor/reactor-core

.stream()
.mapToInt(entry -> entry.getKey() * entry.getValue())
.reduce(Integer::sum)
.getAsInt();

代码示例来源:origin: zstackio/zstack

private static int hashOXR(List lst) {
  return lst.stream().mapToInt(Object::hashCode)
      .reduce(0, (l, r) -> l ^ r);
}

代码示例来源:origin: shekhargulati/30-seconds-of-java

/**
 * Calculates the greatest common denominator (gcd) of an array of numbers
 *
 * @param numbers Array of numbers
 * @return gcd of array of numbers
 */
public static OptionalInt gcd(int[] numbers) {
  return Arrays.stream(numbers)
      .reduce((a, b) -> gcd(a, b));
}

代码示例来源:origin: shekhargulati/30-seconds-of-java

/**
 * Calculates the lowest common multiple (lcm) of an array of numbers.
 *
 * @param numbers Array of numbers
 * @return lcm of array of numbers
 */
public static OptionalInt lcm(int[] numbers) {
  IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y);
  return Arrays.stream(numbers)
      .reduce((a, b) -> lcm.applyAsInt(a, b));
}

相关文章

微信公众号

最新文章

更多