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

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

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

IntStream.collect介绍

[英]Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is a mutable result container, such as an ArrayList, and elements are incorporated by updating the state of the result rather than by replacing the result. This produces a result equivalent to:

R result = supplier.get();

Like #reduce(int,IntBinaryOperator), collect operations can be parallelized without requiring additional synchronization.

This is a terminal operation.
[中]在此流的元素上执行{$0$}操作。可变缩减是指缩减值是可变结果容器(如ArrayList),通过更新结果状态而不是替换结果来合并元素。这将产生相当于以下内容的结果:

R result = supplier.get();

与#reduce(int,IntBinaryOperator)类似,collect操作可以并行化,而不需要额外的同步。
这是一个terminal operation

代码示例

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

private String indent(int level) {
  return IntStream.range(0, level)
    .collect(StringBuilder::new, (sb, i) -> sb.append("    "), StringBuilder::append)
    .toString();
}

代码示例来源:origin: SonarSource/sonarqube

@CheckForNull
 private static String removeCharZeros(@Nullable String str) {
  if (str == null || str.isEmpty()) {
   return str;
  }
  return str.codePoints()
   .filter(c -> c != "\u0000".codePointAt(0))
   .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
   .toString();
 }
}

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

@Override
  public R execute() {
    try (final IntStream stream = buildPrevious()) {
      return stream.collect(supplier, accumulator, combiner);
    }
  }
}

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

default <R> R collect(IntPipeline pipeline, Supplier<R> supplier,
  ObjIntConsumer<R> accumulator,
  BiConsumer<R, R> combiner) {
  requireNonNull(pipeline);
  requireNonNull(accumulator);
  requireNonNull(combiner);
  return optimize(pipeline).getAsIntStream().collect(supplier, accumulator, combiner);
}

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

private String randomizeCase(Random r, String name) {
  return name.chars()
    .map(c -> r.nextBoolean() ? Character.toUpperCase(c) : Character.toLowerCase(c))
    .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
    .toString();
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
   * Get an unused part index based on the namePattern, which doesn't exist yet
   * and has the lowest positive index
   *
   * @param nameTemplate
   *      The template for new part names containing a {@code '#'} for the index,
   *      e.g. "/ppt/slides/slide#.xml"
   * @return the next available part name index
   * @throws InvalidFormatException if the nameTemplate is null or doesn't contain
   *      the index char (#) or results in an invalid part name 
   */
  public int getUnusedPartIndex(final String nameTemplate) throws InvalidFormatException {
    if (nameTemplate == null || !nameTemplate.contains("#")) {
      throw new InvalidFormatException("name template must not be null and contain an index char (#)");
    }

    final Pattern pattern = Pattern.compile(nameTemplate.replace("#", "([0-9]+)"));
    
    final ToIntFunction<String> indexFromName = name -> {
      Matcher m = pattern.matcher(name);
      return m.matches() ? Integer.parseInt(m.group(1)) : 0;
    };
    
    return packagePartLookup.keySet().stream()
      .mapToInt(indexFromName)
      .collect(BitSet::new, BitSet::set, BitSet::or).nextClearBit(1);
  }
}

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

@Override
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) {
  return finallyClose(() -> stream().collect(supplier, accumulator, combiner));
}

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

@Test
public void testShuffled()
{
  List<Integer> list = IntStream.range(0, INPUT_SIZE).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
  Collections.shuffle(list);
  test(list.stream().mapToInt(Integer::intValue),
      list.stream().mapToInt(Integer::intValue).mapToObj(key -> Integer.toString(key * 2)),
      MAX_ELEMENTS_COMPARATOR,
      IntStream.range(INPUT_SIZE - OUTPUT_SIZE, INPUT_SIZE).mapToObj(key -> Integer.toString(key * 2)).iterator());
  test(list.stream().mapToInt(Integer::intValue),
      list.stream().mapToInt(Integer::intValue).mapToObj(key -> Integer.toString(key * 2)),
      MIN_ELEMENTS_COMPARATOR,
      IntStream.range(0, OUTPUT_SIZE).map(x -> OUTPUT_SIZE - 1 - x).mapToObj(key -> Integer.toString(key * 2)).iterator());
}

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

@Test
public void testShuffled()
{
  List<Integer> list = IntStream.range(0, INPUT_SIZE).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
  Collections.shuffle(list);
  test(list.stream().mapToInt(Integer::intValue),
      MAX_ELEMENTS_COMPARATOR,
      IntStream.range(INPUT_SIZE - OUTPUT_SIZE, INPUT_SIZE).iterator());
  test(list.stream().mapToInt(Integer::intValue),
      MIN_ELEMENTS_COMPARATOR,
      IntStream.range(0, OUTPUT_SIZE).map(x -> OUTPUT_SIZE - 1 - x).iterator());
}

代码示例来源:origin: SonarSource/sonarqube

private String randomizeCase(String s) {
 return s.chars()
  .map(c -> random.nextBoolean() ? Character.toUpperCase(c) : Character.toLowerCase(c))
  .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
  .toString();
}

代码示例来源:origin: mahmoudparsian/data-algorithms-book

/**
 * Sort a single string
 * 
 * @param word a string
 * @return a sorted word
 */
public static String sort8(final String word) {
  String sorted = word.chars()
      .sorted()
      .collect(StringBuilder::new,
           StringBuilder::appendCodePoint,
           StringBuilder::append)
      .toString();
  return sorted;
}

代码示例来源:origin: elunez/eladmin

private static <K, V> Map<K, V> toMap(
    Class<K> keyType, Class<V> valueType, Object... entries) {
  if (entries.length % 2 == 1)
    throw new IllegalArgumentException("Invalid entries");
  return IntStream.range(0, entries.length / 2).map(i -> i * 2)
      .collect(HashMap::new,
          (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
          Map::putAll);
}

代码示例来源:origin: elunez/eladmin

private static <K, V> Map<K, V> toMap(
      Class<K> keyType, Class<V> valueType, Object... entries) {
    if (entries.length % 2 == 1)
      throw new IllegalArgumentException("Invalid entries");
    return IntStream.range(0, entries.length / 2).map(i -> i * 2)
        .collect(HashMap::new,
            (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
            Map::putAll);
  }
}

代码示例来源:origin: com.opencredo/concursus-mapping

public static ParameterArgs forMethod(Method method, int skip) {
  checkNotNull(method, "method must not be null");
  checkArgument(method.getParameterCount() >= skip,
      "method %s must have at least %s arguments", method, skip);
  Parameter[] parameters = method.getParameters();
  String[] names = Stream.of(parameters).skip(skip).map(ParameterArgs::getParameterName).toArray(String[]::new);
  Type[] types = Stream.of(method.getGenericParameterTypes()).skip(skip).toArray(Type[]::new);
  Map<String, Type> typesByName = IntStream.range(0, names.length)
      .collect(HashMap::new, (m, i) -> m.put(names[i], types[i]), null);
  return new ParameterArgs(names, typesByName);
}

代码示例来源:origin: exercism/java

public Cipher() {
  this.key = IntStream.range(0, 100)
      .map(x -> ALPHABET.toCharArray()[random.nextInt(ALPHABET.length())])
      .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
      .toString();
}

代码示例来源:origin: one.util/streamex

/**
 * {@inheritDoc}
 * 
 * @see #collect(IntCollector)
 */
@Override
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) {
  if (context.fjp != null)
    return context.terminate(() -> stream().collect(supplier, accumulator, combiner));
  return stream().collect(supplier, accumulator, combiner);
}

代码示例来源:origin: cdapio/cdap

/**
  * Generates a legal Java method name from the given method descriptor.
  */
 private String generateMethodName(Method method) {
  return "lambda$" + method.toString().codePoints().map(c -> Character.isJavaIdentifierPart(c) ? c : '_')
   .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
   .toString();
 }
}

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

/**
  * Generates a legal Java method name from the given method descriptor.
  */
 private String generateMethodName(Method method) {
  return "lambda$" + method.toString().codePoints().map(c -> Character.isJavaIdentifierPart(c) ? c : '_')
   .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
   .toString();
 }
}

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

protected String randomStringFullOfInt(Random random, int digits) {
   return random.ints(digits, 0, 10).collect(StringBuilder::new, StringBuilder::append,
       StringBuilder::append).toString();
  }
}

代码示例来源:origin: io.prestosql/presto-main

@Test
public void testShuffled()
{
  List<Integer> list = IntStream.range(0, INPUT_SIZE).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
  Collections.shuffle(list);
  test(list.stream().mapToInt(Integer::intValue),
      MAX_ELEMENTS_COMPARATOR,
      IntStream.range(INPUT_SIZE - OUTPUT_SIZE, INPUT_SIZE).iterator());
  test(list.stream().mapToInt(Integer::intValue),
      MIN_ELEMENTS_COMPARATOR,
      IntStream.range(0, OUTPUT_SIZE).map(x -> OUTPUT_SIZE - 1 - x).iterator());
}

相关文章