This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

map function and String.split method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I have this code snippet and when I run it with the split mehtod is not doing what I expect, divide the strings on the "a", instead it is just printing the hash code.
And when I run the code with the toUpperCase method is doing what I expect, changing all letters to capital letters.

Why is printing the hashcode instead of dividing the Strings of the text?
Thank you in advance!
 
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably isn't simply printing the hash code, but something else before that. Please show us a typical output.
If you have an IDE to hand, run that code on your IDE. But before that, hover your mouse on each method name in the Stream code, and see whether it tells you the return type of that method.
What compiler error did you get when you had line 4 commented in?
 
Saloon Keeper
Posts: 2955
377
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at what is happening with each operation in the pipeline.

Files.lines(path) will return Stream<String>
map(p -> p.split("a")) will return Stream<String[]>
so forEach(System.out::println) will be printing a type String[], not String as you expected

What you want to do is map Stream<String[]> to Stream<String>
then, forEach() would be iterating through types of String rather than String[]

This can be done using Arrays::stream, but if you used map(Arrays::stream), you would end up with type of Stream<Stream<String>>, since Arrays::stream itself creates a wraps the elements in a stream, which is not what you want.  flatMap() works similarly to map(), however it will unwrap/flatten the Stream<Stream<String>> to Stream<String>
 
Ron McLeod
Saloon Keeper
Posts: 2955
377
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output that you are probably seeing is something like: [Ljava.lang.String;@1234abc

You can decode this as:
[ - type is an array
L - the type inside the array is an object
java.lang.String - the object is specifically String object
1234abc - hash code for the array
 
Javier Gonzalez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much guys, I can see now what is going on!
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look up the documentation for Stream#flatMap(), you will find a suggestion very similar to your current problem. You will also find out about flatMap() in books like Java8 in Action (new editions called Modern Java in Action) by Urma Fusco and Mycroft (Manning). Agree with Ron that flatMap() is probably what you want.

Is this a cert exam question? I shall add it to our streams/λs forum in case it isn't.
 
Javier Gonzalez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbel,

Yes it is a cert exam question OCP 8, it is a modification of one snippet code from the Boyarsky-Selikoff book.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!