Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections, skip and limit paging  RSS feed

 
Markus Schmider
Ranch Hand
Posts: 146
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to learn Bulk Operations on Collections.
In the book I am reading it is mentioned that skip and limit can be used for paging, without giving a real example.
The following code I have written terminates with an IllegalStateException.
I do not understand why, since I do not call a terminal operation on iteratingValues.

 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book?
Can you really get a page from skip and limit? Yes, you probably can if you use the same Stream again.
What happens if your skip and limit numbers don't match the size of the stream? It appears not to be a problem with IntStream#skip because that simply returns empty if n > size.
What about IntStream#limit. It doesn't say anyjthing about problems there. either.

So. Don't know. Sorry.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Markus,

you can only use an IntStream once. So, simply put the IntStream inside your loop

Alternatively, you could simply do it in one line:


Or you could implement a class that somehow mimics
a Stream (in the Scala sense), that can be used more than once.
I have a class, with members ArrayList<E> and a function f(E) -> E,
that does this.

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for that Exception does not supply enough information to help us. What does the stack trace say?
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply looking at the code should give you already enough information.
Or just run the code and see what exception you get.
(I did both).
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Markus,

you can only use an IntStream once. So, simply put the IntStream inside your loop

Piet is right. Although it's mentioned nowhere for IntStream (and I assume LongStream and DoubleStream), I found the following in the class documentation for Stream (bold added by me):
A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh...
I thought that this 'write once use once' was a well- and widely-known characteristic.
I've had this exception quite some times since using Java 8 on my wife's laptop.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!