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

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

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

IntStream.iterate介绍

暂无

代码示例

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

public static <T> List<T> reverse_IntStream(List<T> list) {
  if (list == null) {
    throw new IllegalArgumentException("list can't be null");
  }
  int size = list.size();
  return IntStream.iterate(size - 1, el -> el - 1).limit(size).mapToObj(list::get).collect(toList());
}

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

/**
 * Chunks an array into smaller arrays of a specified size.
 *
 * @param numbers Input array of numbers
 * @param size    The chunk size
 * @return Smaller chunks
 */
public static int[][] chunk(int[] numbers, int size) {
  return IntStream.iterate(0, i -> i + size)
      .limit((long) Math.ceil((double) numbers.length / size))
      .mapToObj(cur -> Arrays.copyOfRange(numbers, cur, cur + size > numbers.length ? numbers.length : cur + size))
      .toArray(int[][]::new);
}

代码示例来源:origin: shekhargulati/strman-java

public static String[] chop(String input, int step) {
  if (input == null || input.length() == 0) {
    return EMPTY_ARRAY;
  }
  if (step == 0) {
    return new String[]{input};
  }
  int strLength = input.length();
  int iterations = strLength % step == 0 ? strLength / step : strLength / step + 1;
  return IntStream.iterate(0, i -> i + step)
      .limit(iterations)
      .mapToObj(i -> input.substring(i, (i + step) < strLength ? i + step : strLength))
      .toArray(String[]::new);
}

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

/**
 * Find last index of element in the array. Return -1 in case element does not exist.
 * <p>
 * Uses IntStream.iterate().limit().filter() to find index of the element in the array.
 *
 * @param elements input array
 * @param el       element to find
 * @return index of the element
 */
public static int lastIndexOf(int[] elements, int el) {
  return IntStream.iterate(elements.length - 1, i -> i - 1)
      .limit(elements.length)
      .filter(idx -> elements[idx] == el)
      .findFirst()
      .orElse(-1);
}

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

/** */
private static int keyInSamePartition(Ignite ignite, String cacheName, int key) {
  Affinity<Object> affinity = ignite.affinity(cacheName);
  return IntStream.iterate(key + 1, i -> i + 1)
    .filter(i -> affinity.partition(i) == affinity.partition(key))
    .findFirst().getAsInt();
}

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

/** */
  private static int keyInDifferentPartition(Ignite ignite, String cacheName, int key) {
    Affinity<Object> affinity = ignite.affinity(cacheName);

    return IntStream.iterate(key + 1, i -> i + 1)
      .filter(i -> affinity.partition(i) != affinity.partition(key))
      .findFirst().getAsInt();
  }
}

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

@Test
  public void testMassive()
  {
    BlockBuilder inputBlockBuilder = BIGINT.createBlockBuilder(null, 5000);

    TypedHistogram typedHistogram = new SingleTypedHistogram(BIGINT, 1000);
    IntStream.range(1, 2000)
        .flatMap(i -> IntStream.iterate(i, IntUnaryOperator.identity()).limit(i))
        .forEach(j -> BIGINT.writeLong(inputBlockBuilder, j));

    Block inputBlock = inputBlockBuilder.build();
    for (int i = 0; i < inputBlock.getPositionCount(); i++) {
      typedHistogram.add(i, inputBlock, 1);
    }

    MapType mapType = mapType(BIGINT, BIGINT);
    BlockBuilder out = mapType.createBlockBuilder(null, 1);
    typedHistogram.serialize(out);
    Block outputBlock = mapType.getObject(out, 0);
    for (int i = 0; i < outputBlock.getPositionCount(); i += 2) {
      assertEquals(BIGINT.getLong(outputBlock, i + 1), BIGINT.getLong(outputBlock, i));
    }
  }
}

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

/**
 * Chunks an array into smaller arrays of a specified size.
 *
 * @param numbers Input array of numbers
 * @param size    The chunk size
 * @return Smaller chunks
 */
public static int[][] chunk(int[] numbers, int size) {
  return IntStream.iterate(0, i -> i + size)
      .limit((long) Math.ceil((double) numbers.length / size))
      .mapToObj(cur -> Arrays.copyOfRange(numbers, cur, cur + size > numbers.length ? numbers.length : cur + size))
      .toArray(int[][]::new);
}

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

/**
 * Find last index of element in the array. Return -1 in case element does not exist.
 * <p>
 * Uses IntStream.iterate().limit().filter() to find index of the element in the array.
 *
 * @param elements input array
 * @param el       element to find
 * @return index of the element
 */
public static int lastIndexOf(int[] elements, int el) {
  return IntStream.iterate(elements.length - 1, i -> i - 1)
      .limit(elements.length)
      .filter(idx -> elements[idx] == el)
      .findFirst()
      .orElse(-1);
}

代码示例来源:origin: poetix/protonpack

/**
 * Cycles through a set of items indefinitely.
 * @param items The items to cycle through.
 * @param <T> The type of the items.
 * @return An infinite stream cycling through the supplied items.
 */
public static <T> Stream<T> cycle(T...items) {
  return IntStream.iterate(0, i -> i == items.length - 1 ? 0 : i + 1).mapToObj(i -> items[i]);
 }

代码示例来源:origin: cmeza20/spring-data-generator

private static void br(int iterate) {
  IntStream.iterate(0, i -> i++).limit(iterate).forEach(j ->{
    if (mavenLogger == null) {
      commonsLogger.info(StringUtils.EMPTY);
      return;
    }
    mavenLogger.info(StringUtils.EMPTY);
  });
}

代码示例来源:origin: yandex/yoctodb

@NotNull
@Override
public Iterator<IntToIntArray> ascending(
    @NotNull
    final BitSet valueFilter) {
  final ArrayBitSet bs = LongArrayBitSet.zero(valueFilter.getSize());
  return IntStream.iterate(0, i -> i + 1)
      .mapToObj(i -> getIntToIntArray(valueFilter, bs, i))
      .limit(keysCount)
      .iterator();
}

代码示例来源:origin: yandex/yoctodb

@NotNull
@Override
public Iterator<IntToIntArray> ascending(
    @NotNull
    final BitSet valueFilter) {
  final ArrayBitSet bs = LongArrayBitSet.zero(valueFilter.getSize());
  return IntStream.iterate(0, i -> i + 1)
      .mapToObj(i -> getIntToIntArray(valueFilter, bs, i))
      .limit(keysCount)
      .iterator();
}

代码示例来源:origin: kousen/java_8_recipes

public int nextPrime(int num) {
    OptionalInt optionalInt = IntStream.iterate(num + 1, n -> n + 1)
                      .filter(this::isPrime)
                      .findFirst();

    return optionalInt.orElseThrow(IllegalArgumentException::new);
  }
}

代码示例来源:origin: geosdi/geo-platform

/**
 * @return {@link IGPMessageKeyValue}
 * @throws Exception
 */
@Override
public IGPMessageKeyValue[] build() throws Exception {
  this.checkArguments();
  return IntStream.iterate(0, n -> n + 1)
      .limit(this.key.length)
      .boxed().map(n -> new GPMessageKeyValue(key[n], value[n]))
      .toArray(GPMessageKeyValue[]::new);
}

代码示例来源:origin: qaprosoft/zafira

public List<UserType> createUsers(int count) {
    List<UserType> userTypes = new ArrayList<>();
    IntStream.iterate(0, i -> i++).limit(count).forEach(index -> userTypes.add(createUser(new UserTypeBuilder())));
    return userTypes;
  }
}

代码示例来源:origin: aruld/java-oneliners

public static void main(String[] args) {
 // Range is half-open (exclusive) as in Python, unlike Scala.
 range(1, 1000).sum();
 range(1, 1000).reduce(0, Integer::sum);
 Stream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
 IntStream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
}

代码示例来源:origin: jlink/jqwik

private Stream<String> fizzBuzz() {
  return IntStream.iterate(1, i -> i + 1).mapToObj(i -> {
    boolean divBy3 = i % 3 == 0;
    boolean divBy5 = i % 5 == 0;
    return divBy3 && divBy5 ? "FizzBuzz" : divBy3 ? "Fizz" : divBy5 ? "Buzz" : Integer.toString(i);
  });
}

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

package com.company;

import rx.Observable;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public class Main {

  public static void main(String[] args) {

    final IntPredicate[] p={(x)->true};
    IntStream primesStream=IntStream.iterate(2,n->n+1).filter(i -> p[0].test(i)).peek(i->p[0]=p[0].and(v->v%i!=0)   );

    Observable primes = Observable.from(()->primesStream.iterator());

    primes.take(10).forEach((x) -> System.out.println(x.toString()));

  }

}

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

private Object[] getKeys(boolean isOwner, int num) {
   return IntStream.iterate(0, i -> i + 1)
      .mapToObj(i -> "key" + i)
      .filter(k -> cache(0, DIST).getAdvancedCache().getDistributionManager().getLocality(k).isLocal() == isOwner)
      .limit(num).toArray(Object[]::new);
  }
}

相关文章