Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

map function and String.split method

 
Javier Gonzalez
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!
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • 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?
 
Ron McLeod
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus 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
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus 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: 69847
278
  • 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.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic