I know how to make Map<String, Car>
but how to make Map<String, List<Car>>
in lambdaj?
This is code I want to write in LambdaJ:
Map<String, List<Car>> driverCarsMap = new HashMap<String, List<Car>>();
for (Car car : cars)
{
String driver = car.getDriver();
if (!driverCarsMap.containsKey(driver))
driverCarsMap.put(driver, new ArrayList<Car>());
driverCarsMap.get(driver).add(car);
}
Unfortunately the code:
Map<String, List<Car>> driverCarsMap = index(cars, on(Car.class).getDriver());
creates the Map, but value is not being extended but overwritten. So, de facto we do note have a List but single object.
What you want to do is similar to LambdaJ index method. The problem with index is its limitation, its documentation says:
So, what you have to use is LambdaJ groups. For your example, it would be:
You can take a look at grouping items here: https://code.google.com/p/lambdaj/wiki/LambdajFeatures#Grouping_items