Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 streaming and sql like join [closed]

Tags:

java

streaming

I have 2 csv files so defined 2 objects i.e. Person and Address. Person can have multiple addresses objects. I need to join and perform aggregation on data. This is similar to having 2 separate tables in database and performing join. What would be right approach to achieve this? Any options in steaming/Lambda? If not any framework you can recommend?

Thanks, Kam

like image 474
kami Avatar asked Apr 20 '26 06:04

kami


1 Answers

I don't think there is an equivalent of join in standard library. The closest you can do is something like

Map<String, Person> pMap = persons.stream
                                  .collect(Collectors.groupingBy(Person::getId);
List<Pair<Person,Address>> joined = addresses.stream
                                      .map (a -> new Pair(pMap.get(a.personId()), a));

etc.

(Note, that I made up a class Pair here, because there is (still) no such thing as a tuple in java).

like image 118
Dima Avatar answered Apr 21 '26 20:04

Dima



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!