Java - Combining multiple list data into a separate separate column list

I would like to know if it is possible to combine data in different streams into one stream. By combining, I mean adding it to separate columns not to an existing column.

So far, I have been able to collect data on individual Maps, as shown in the example below. I believe that I should combine these Cards into one, but I’m not sure how to do it or I’m doing something wrong.

I did some searches, found some streams in FlatMap, Concat, etc., but since I am new to these Streams and Map functions, I am looking for a guide. (I currently work without using the Map / Streams function, just using the standard list with Add and AddALL)

For example: Data List - 1 Map<String, int> carMileage

  • [carOne, 10]
  • [carTwo, 20]
  • [carThree, 25]

Data List - 2 Map<String, String> carGearBox

  • [carThree, automatic]
  • [carTwo manual]
  • [carOne, mixed]

Data List - 3 Map<String, Double> carMaxSpeed

  • [carTwo, 160.75]
  • [carThree, 200.25]
  • [carOne, 250.55]

Exit: [something like: Map<String, dataCar>]

  • [carOne, 10, mixed, 250.55]
  • [carTwo, 20, manual, 160.75]
  • [carThree, 25, automatic, 200.25]
+4
source share
1 answer

Assuming that all 3 cards have the same set of keys, a direct solution would be to create a stream over the elements of one card (for example, carMileage) and collect it on another card DataCar. If there is a constructor DataCar(mileAge, String gearBox, Double maxSpeed), you can get the following:

Map<String, DataCar> result =
    carMileage.entrySet()
              .stream()
              .collect(Collectors.toMap(
                Map.Entry::getKey,
                e -> new DataCar(e.getValue(), carGearBox.get(e.getKey()), carMaxSpeed.get(e.getKey()))
              ));
+5
source

All Articles