• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detecting an infinite Stream

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've now moved on to chapter 2 question 7 in the book "Java 8 for the really impatient". This question asks the reader to implement a method called isFinite which takes a stream as a parameter and returns true if the stream is finite and false otherwise. So far, I have two streams (a finite and an infinite stream):



I'm now trying to find some way to detect which stream is finite and which one is infinite. My first attempt was to try to collect the last element in the stream:



For the finite stream, it does successfully grab the last element in the list, but for the infinite stream, my program crashes and never terminates. I also tried stream.count() and it failed for the infinite stream. I could try to use iterate or forEach, but I don't know how I would know when I saw the last element. Any hints about how I should approach this problem? I looked through the Stream API and didn't see anything particularly helpful. Thanks again.

Note - the book does admit that writing such a method is not a good idea, but they do imply that it is possible (when they say - "go ahead and write it anyways").
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it is possible. I took that question to mean "go try it and see that it hangs/runs infinitely"
 
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best option I know for this is to grab the spliterator from the stream and ask it for the size estimate:



Generates:



But, notice that method is called "estimate size"
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just keep in mind that you can't use the stream after estimating the size this way, because grabbing the spliterator is a terminal operation. If you want to perform a stream operation after estimating the size, you'll have to use StreamSupport.stream() to create a new stream from the spliterator.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic