Java Lambda: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 53: | Line 53: | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/30089469/ Sum values in a Map with a stream] | * [https://stackoverflow.com/questions/30089469/ Sum values in a Map with a stream] | ||
* [https://stackoverflow.com/questions/32704551/ Else method for ifPresent Stream] | |||
* [https://stackoverflow.com/questions/44101297/ Java stream filter sum of values] | * [https://stackoverflow.com/questions/44101297/ Java stream filter sum of values] | ||
Revision as of 07:25, 1 March 2022
Map<String, String> insured = Stream.of("Adults:1", "Children:1", "Senior Citizen:10")
.map(pair -> pair.split(":"))
.collect(Collectors.toMap(pair -> pair[0], pair->pair[1]));
Double totalInsured = insured.values().stream().mapToDouble(d -> Double.parseDouble(d)).sum();
Double totalInsured = insured.values().stream().mapToDouble(Double::parseDouble).sum();
Integer totalInsured = insured.values().stream().mapToInt(d -> Integer.parseInt(d)).sum();
Integer totalInsured = insured.values().stream().mapToInt(Integer::parseInt).sum();
Map<String, String> filter = insured.entrySet().stream()
.filter(map -> !"Children".equals(map.getKey()))
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));
Integer totalInsured = filter.values().stream().mapToInt(Integer::parseInt).sum();
Map<String, String> mapping = Stream.of("travel_adult:Adults", "travel_children:Children", "travel_senior_citizen:Senior Citizen")
.map(pair -> pair.split(":"))
.collect(Collectors.toMap(pair -> pair[0], pair->pair[1]));
Map<String, String> reverse = mapping.entrySet().stream()
.collect(Collectors.toMap(map -> map.getValue(), map -> map.getKey()));
int totalInsured = properties.entrySet().stream()
.filter(map-> ArrayUtils.contains(INSURED_OBJECT_MAP.values().toArray(), map.getKey()))
.collect(Collectors.toMap(map -> map.getKey(), map -> (Integer)map.getValue()))
.values().stream().mapToInt(Integer::new).sum();
private static Map<String, Object> decode(String encode) {
return Stream.of(encode.split(LIST_SPLITTER))
.map(String::trim).collect(Collectors.toList())
.stream().map(pair -> pair.split(PAIR_SPLITTER))
.collect(Collectors.toMap(array -> array[0], array -> array[1]));
}
References
|