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

Splitting string, flatMapping it and creating objects using Java streams?

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 classes going like this:




Now, I need to create two or more Tickets, depending how many tickets loggedInUser chooses. From my JSP page, I'll get something like this:

String uri = request.getQueryString();

I want to create two Ticket objects for two Seat objects. In uri string, chars 2 and 3 are primary keys for seats.

The idea is to use Java streams to perform splitting and creating objects. This is I have so far.



I have tried this as well:



projection is also object, which I obtained, this is just reference, it is elsewhere in servlet. loggedInUser I got from session. I'm newbie regarding Java streams, any additional explanation would be excellent. I hope I correctly used flatMap to avoid Stream<String[]>.

I get that map() requires IntUnaryOperator lambda expression, something like this:


I don't know how to "map" from int (primary key for Seat) to Seat object.

Cheers.
 
Ranch Hand
Posts: 139
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should test output of each individual transformation you are applying, if you are not sure. For your case:
1. When you are using String::split to remove "seats=" prefix, the first part is an empty string, and the second part is the string that you want. So, you should use [1] to access that part. So, you should remove Arrays::stream method, because you have only one part per "seats=xx" string.
2. For constructing Seat from integer, you can use constructor as a method reference - Seat::new. Of course, you can use plain lambda as well like this: (i -> new Seat(i))
 
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to: map(s -> s.split("seats=")[1])
could be something like: map(s -> s.replaceFirst("seats=(\\d+)", "$1"))
or maybe: map(s -> s.replace("seats=", ""))
 
Mike Gosling
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Salil Wadnerkar wrote:...So, you should use [1] to access that part. So, you should remove Arrays::stream method, because you have only one part per "seats=xx" string.



Thank you Salil, I actually did what you proposed right after I posted the question, and it worked. Tho, I am having different kind of problem.



I'm getting exception

 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use request.getParameterValues? That way you don't have to do any custom splitting. For instance:
 
Mike Gosling
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob for your reply.

I didn't know about


Now, I do.

I the meanwhile, I realized that my string from jsp page, actually looks like this



Then, I got my solution working, like this:




Probably I'm gonna modify to Rob's solution.
 
Rancher
Posts: 4616
47
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely use the methods provided by the request object.
There should be no reason to manually split the URL in order to get the parameters.
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Dave. Code that uses streams generally should tell intent, which is kind of the point of functional style programming (vs. imperative style). By using the stream to split the string into component parts like that, you're adding a lot of implementation detail into the mix.  If anything, keep the parsing of the input separate from the stream code.
 
    Bookmark Topic Watch Topic
  • New Topic