This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Exercices (preferably with test cases) for Streams  RSS feed

 
Ranch Foreman
Posts: 60
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to learn about Streams. Im reading Ivor Horton "Beginning Java", but I have trouble following the chapter about Streams.  I experienced that it is easier for me to understand the theory after I have used something in my Code for a while. Is there anything in the web you know of?
If not I'll just create my own exercices or play around with Streams a bit but I like the concept of exercices that one can complete.
Louis
 
Ranch Hand
Posts: 99
Java Linux Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand Java's lambda functions?
 
Louis Müller
Ranch Foreman
Posts: 60
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I'm working my way through "Beginning Java" And so far there was no mention of Lambda functions..
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:No. I'm working my way through "Beginning Java" And so far there was no mention of Lambda functions..



Not sure what Beginning Java is? Is that a book?
 
Louis Müller
Ranch Foreman
Posts: 60
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.wrox.com/WileyCDA/WroxTitle/Ivor-Horton-s-Beginning-Java-Java-7-Edition.productCd-0470404140.html

Sidenote:
I have been looking into the "Why are beginners not familiar with Streams" Thread on the ranch. Just wanted to note that, from a beginnery perspective, I am not able to find a lot of stuff that explains Streams to me in a way I can understand it. For example I am using Scanner. As far as I know it is only one class the Scanner class and all the methods I need can be found in it. I therefore only need to check the oracle docs and I can easily understand how to read Input, how to check if there is still Input to com etc. With Streams it occours different to me. There is InputStream, InputStreamReader and stuff like this.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:http://www.wrox.com/WileyCDA/WroxTitle/Ivor-Horton-s-Beginning-Java-Java-7-Edition.productCd-0470404140.html

Sidenote:
I have been looking into the "Why are beginners not familiar with Streams" Thread on the ranch. Just wanted to note that, from a beginnery perspective, I am not able to find a lot of stuff that explains Streams to me in a way I can understand it. For example I am using Scanner. As far as I know it is only one class the Scanner class and all the methods I need can be found in it. I therefore only need to check the oracle docs and I can easily understand how to read Input, how to check if there is still Input to com etc. With Streams it occours different to me. There is InputStream, InputStreamReader and stuff like this.



My apologies. I thought you were talking about collections and streams. BTW, that's what the link is referring to.

Here's a good link on Java IO. BTW, IO(the underneath stuff) can get very complicated, so I'm not surprised you are confused. IO is a difficult subject.

IO Streams

The beginning of Java IO

Java IO
 
Marshal
Posts: 63843
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, you have got confused. So I shall confuse you even more. Note the type 1 and 2 phrases are only for use in this post; nobody else says that.

  • 1: There is the Streams API, which is intended to introduce functional‑style programming and comprises classes and interfaces for processing other datatypes. You can read about it in this package, but that isn't a beginner's introduction. I think that is what you mean. The best introduction I can think of is in Urma Fusco and Mycroft's book Java8 in Action (old edition: Manning 2014) or Modern Java in Action (newer edition, Manning 2017?). That sort of Stream is a program which manipulates and processes multiple values in order. You will get a hint from the link I just gave you. There are some exercises (not many) in Urma Fusco and Mycroft.
  • 2: There are streams that transmit data. You can read about them in the Java™ Tutorials. An InputStream is an example of that sort of stream. It is used for reading data from a file or some other source, and doesn't process or alter those data.


  • Streams type 1 are designed to stop when the source is exhausted, or when some “downstream” part of the programming gives an instruction to stop. In fact, they don't start running until the  “downstream” program starts; this is what “laziness‑seeking” means.
    Streams type 2 stop whenever their input is finished, which is easy enough to achieve when reading a file, but a network input or the keyboard may not have a defined endpoint.

    As Cay Horstmann says, Oracle seem to have forgotten methods to create a Stream<String> (type 1) from a Scanner; I also noticed there weren't methods to stop a stream if it encounters a sentinel value. All those shortcomings were remedied in Java9 (September 2017). You can start or stop a stream with sentinel values using two methods: 1 2. Scanner now has three methods to return plain simple tokens or something matching a regex: 1 2 3. Remember, if you are reading from an infinite source, you will probably have to stop the reading with one of the methods to stop a Stream (two previous links and the limit() and skip() methods).
     
    Sheriff
    Posts: 13391
    221
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can find lots of interesting problems right here. For example, there's another recent thread about a Sudoku puzzle checker. I just spent a couple of hours playing around with a Stream-based implementation.

    Here's some of my code:

    The imperative code for that was:

    You can try implementing acceptsAllColumns() and acceptsAllBlocks() using Streams. Both my implementations look very similar to acceptsAllRows() I show above.

    Another thing I do to practice is to go and find examples of Streams usage and try to recreate them myself, kind of like reading a story then trying to tell the story in your own words without referring to the original text.
     
    Rancher
    Posts: 3127
    110
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:

    Slighly shorter is:

    If you make board an argument of this method, and you have an 'int[][] transpose([][]int)' method, you could then have


    But OP is talking about InputStreams and the like. So I think Gerards reply is spot on.
     
    Junilu Lacar
    Sheriff
    Posts: 13391
    221
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Slightly shorter...


    Whachutalkinbout, Piet?! That is waay shorter and it allows for removal of the negation in the predicate and it still shortcuts. All that's very nice so have a cow, er, taco, on me

    Edit: I shortened it up a little more with a method reference:

    although I'm not sure that's more readable. Seems to lose a little context of what's being accepted; looks more "magical" even, maybe.
     
    Hang a left on main. Then read this tiny ad:
    global solutions you can do in your home or backyard
    https://coderanch.com/t/708587/global-solutions-home-backyard
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!