Reputation: 3297
I have a List<BatchDTO>
with the following class
public class BatchDTO {
private String batchNumber;
private Double quantity;
.
.
//Getters and setters
}
What I have to do is to sum up the total if the batchNumber is duplicate. I have used a LinkedHashMap to implement this, and did the iterations. But what I would like to have is a more optimized way. Can I use stream to do this in an optimized way.
private static List<BatchDTO > getBatchDTO (Map<String, BatchDTO > batchmap) {
return batchmap.values().stream().collect(Collectors.toList());
}
private static Map<String, BatchDTO > getBatchMap(List<BatchDTO > batchList, Map<String, BatchDTO > batchMap) {
for (BatchDTO batchDTO : batchList) {
batchMap = getBatchMap(batchMap, batchDTO );
}
return batchMap;
}
private static Map<String, BatchDTO > getBatchMap(Map<String, BatchDTO > batchMap, BatchDTO batchObject) {
String batchCode = batchObject.getBatchNumber();
if(!batchMap.containsKey(batchCode)) {
batchMap.put(batchCode, batchObject);
} else {
batchObject.setQuantity(getTotalQuantity(batchMap,batchObject));
batchMap.put(batchCode, batchObject);
}
return batchMap;
}
private static Double getTotalQuantity(Map<String, BatchDTO > batchmap, BatchDTO batchObject) {
return batchmap.get(batchObject.getBatchNumber()).getQuantity() + batchObject.getQuantity();
}
Upvotes: 3
Views: 126
Reputation: 3628
Probably a little unreadable with comments in the code, but this is all I have time for.
// Result will be a Map where the keys are the unique 'batchNumber's, and the
// values are the sum of the 'quantities' for those with that 'batchNumber'.
public Map<String, Double> countBatchQuantities(final List<BatchDTO> batches) {
// Stream over all the batches...
return batches.stream()
// Group them by 'batch number' (gives a Map<String, List<BatchDTO>>)
.collect(Collectors.groupingBy(BatchDTO::getBatchNumber))
// Stream over all the entries in that Map (gives Stream<Map.Entry<String, List<BatchDTO>>>)
.entrySet().stream()
// Build a map from the Stream of entries
// Keys stay the same
.collect(Collectors.toMap(Entry::getKey,
// Values are now the result of streaming the List<BatchDTO> and summing 'getQuantity'
entry -> entry.getValue().stream().mapToDouble(BatchDTO::getQuantity).sum()));
}
Note: I make no promises that this is more optimised than your existing approach... But it does the job with Streams.
Note: This will throw an exception if the quantity
is null
for any of your BatchDTO
s...
Upvotes: 2
Reputation: 6300
You could try using stream api the way @Naman suggested in comments. And assuming BatchDTO
has all args constructor you could return back from Map
to List<BatchDTO>
List<BatchDTO> collect = list.stream()
.collect(groupingBy(BatchDTO::getBatchNumber, summingDouble(BatchDTO::getQuantity)))
.entrySet().stream()
.map(entry -> new BatchDTO(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
JavaDoc: groupingBy(), summingDouble()
Upvotes: 3