11

I would like to do something like this:

int[] seq = new int[N];
for (int i = 0 ; i < N ; i++) {
    seq[i] = i;
}

...in one line, and i am wondering if it is possible with lambda expression.

If it works with ArrayList<Integer>, it is okay for me.

Lii
  • 11,553
  • 8
  • 64
  • 88
Eliott Roynette
  • 716
  • 8
  • 21
  • 2
    I'm just wondering how useful is this, no offense. Every `seq[k]`, can be replaced with `k` in other parts of code and you need no additional memory... – Betlista Feb 08 '18 at 11:36
  • I want to use 'Collections.shuffle' after – Eliott Roynette Feb 08 '18 at 12:12
  • 3
    [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? It was asked here - https://stackoverflow.com/questions/20058366/shuffle-a-list-of-integers-with-java-8-streams-api – Betlista Feb 08 '18 at 13:47
  • Possible duplicate of [How can I generate a list or array of sequential integers in Java?](https://stackoverflow.com/questions/10242380/how-can-i-generate-a-list-or-array-of-sequential-integers-in-java) – Bernhard Barker Feb 08 '18 at 21:05

3 Answers3

28

Use IntStream.range():

int[] seq = IntStream.range(0, N).toArray();

or IntStream.rangeClosed() if you want to include N:

int[] seq = IntStream.rangeClosed(0, N).toArray();
Lii
  • 11,553
  • 8
  • 64
  • 88
Eran
  • 387,369
  • 54
  • 702
  • 768
14

Starting from Java 9 you can use the three-argument IntStream.iterate:

int[] seq = IntStream.iterate(0, x -> x < N, x -> x + 1).toArray();

Where:

IntStream.iterate​(int seed, IntPredicate hasNext, IntUnaryOperator next):

  • seed - the initial element;
  • hasNext - a predicate to apply to elements to determine when the stream must terminate;
  • next - a function to be applied to the previous element to produce a new element.
Oleksandr Pyrohov
  • 14,685
  • 6
  • 61
  • 90
2

A BitSet would do too. Though then without lambda.

BitSet bset = new BitSet(N); // Initial capacity for N bits.
bset.set(0, N); // Set a range to true for 0 upto N-1.
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Interesting suggestion - but this is neither one line, nor produces an `int[]` or `Collection` as requested by the OP. – Hulk Feb 08 '18 at 12:15
  • 1
    @Hulk and it does not use a lambda stylish syntax. However (1) BitSet is equivalent to a `Set`, (2) BitSet can set ranges, (3) the use case seems to indicate that BitSet might be appropriate, (4) BitSet might be more efficient, (5) the code is actually more compact as a foreach. – Joop Eggen Feb 08 '18 at 13:16
  • Agreed - and it can be converted into an `IntStream` over the indices of the set bits via `bset.stream()`, if needed. I'd still prefer `IntStream.range(0,N)` without further information, but +1 for creativity – Hulk Feb 08 '18 at 13:49
  • 2
    `BitSet` is a highly underestimated class in Java, +1 for helping make this class more known to the world. – fps Feb 08 '18 at 14:12
  • 1
    @JoopEggen Re (3) the use case seems to be applying `Collections.shuffle()` as per OPs comment. Seems a `BitSet` doesn't make much sense then. – BlackJack Feb 08 '18 at 16:18
  • @BlackJack you are right, I overlooked that. Maybe a permutation for something like blackjack. I leave the answer for other similar questions as there is an already accepted answer. – Joop Eggen Feb 08 '18 at 16:33