• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Head First Java page 385 streams

 
Ranch Hand
Posts: 789
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output should be:
strings = {I, am , a, list, of, Strings]

result = {I, am , a ,list]

I didn't run this yet, but I'm wondering where result would be coming from.  I don't see it mentioned in the code.

Thanks,

Kevin



 
Marshal
Posts: 4400
567
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you missing a line?  I would expect it to be something like:
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I probably wouldn't declare the Streams in the first place.I said Streams because there are two of them: one in line 4 and one in line 5.
Several minor errors left unchanged, but they will prevent the code from compiling.
The limit() method takes a long as its parameter.
 
kevin Abel
Ranch Hand
Posts: 789
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

My tutor worked on this with me.   I don't completely understand it, but the result and your comments match with what we came up with.  We didn't think of making Limit 4L.  I added an item to the Head First Eretta sheet.  I'm not sure if the book has a mistake.  It is confusing to me as a reader so I mentioned it.  Let's see if they change it in the next edition.



Best,

Kevin
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you copied your line 2 correctly, then it is incorrect and will fail to compile. My line 4 has, on the other hand, the correct types and will run correctly when you correct the minorr spellling misstakes.
Let's go through my version of your code. Lines 1‑2 create a List<String>. List#of() has overloadings and in this case it creates a 6‑element unmodifiable List. All the arguments should be of the same type (otherwise you would probably get a List<Object>), and in this case they are all Strings, so you get a List<String>. That should be pronounced, “List of Strings.”
So far, so good.
See the Collection#stream() method. Line 4 does nothing until the terminal operation (line 6) runs. That is called lazy execution. If line 6 runs (and it does), then line 4 creates a Stream<String> which manipulates all the elements of the List in turn.
Look at Stream#limit(). That takes a long and produces a new Stream of the same type, but in this case it only has four elements. Since you are using one sequential Stream, that is done very simply by taking the first four elements. The link tells you that things can become more complicated if you use parallel Streams. Yes, you are using 4L if you pass an int because the limit() method will convert the 4 to a long. That's 4L.
Line 6 is a terminal operation because it doesn't create another Stream. If you look up collect(), you will find it takes a Collector. You can write your own Collector in three or four parts, but if you look in the Collectors class, you will find a method returning one ready‑made: here. So you call that and the Collector returned produces a List. It doesn't specify what sort of List however.
The remainder of the code should be obvious.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic