• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can you combine two objects into one, using Streams?  RSS feed

 
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
So in the following method, I have an object that has a constructor that takes two other objects as arguments. The solution that I've written works, but I was wondering if you guys know of a more compact solution. With less code.



This class receives the values of it's fields from a webpage. The method puts them in a List, transforms them into Enums, and collects them into 2 lists. Then with the last Stream, it uses the first two lists to create a number of Objects of type Planet.

I was wondering if the 3 Streams can be rewritten into only 1 or 2.  The code above is the equivalent of:



It all boils down to if it's recommended to use Streams when you have two different lists, and the objects into those lists must be combine into another one, 1:1. By 1:1, I mean that the object at index 0 in one list will have to be combined with the object at index 0 in the other list. Also the lists will always have the same number of objects.

Sorry for the long post.
Thank you in advance!


 
Alexandru Puiu
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've managed to reduce the code. But using reflection, so the question above remains .
 
Saloon Keeper
Posts: 8720
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem mostly stems from the fact that you have a fixed number of fields, rather than a dynamically growing list.

How are your deserializing your form? Can't you design the form in such a way that you end up with a list of planets directly, rather than with a fixed number of decoupled properties?
 
Alexandru Puiu
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how I can do that. I mean, on the website, the user has to choose the planet type and size. The planet values are in direct correlation with those two. I must have a method somewhere that constructs them. But like I said a number of five "if"'s  would have resolved the problem.

This question was more about Streams, and if someone knows a way to use two different lists(in term of the object type that they contain) and with a Stream to combine them 1:1 into another list.   
 
Alexandru Puiu
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By 1:1, I mean that the object at index 0 in one list will have to be combined with the object at index 0 in the other list. Also the lists will always have the same number of objects.
 
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wouldn't the following work?

 
Alexandru Puiu
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first the fields must be a string, not an array, because the values are updated with the values entered by the used in a web page. The values are sent to the class as a string. So I would still have to put them into the array, just like I would in the list.

Second, I was aiming on using the Java 8, Steam.API.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the fields must be a string, not an array, because the values are updated with the values entered by the used in a web page


Can I ask how you getting values from the web?  What technology is limiting you to only Strings?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!