Java Lambda: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
Integer totalInsured = insured.values().stream().mapToInt(d -> Integer.parseInt(d)).sum(); | Integer totalInsured = insured.values().stream().mapToInt(d -> Integer.parseInt(d)).sum(); | ||
Integer totalInsured = insured.values().stream().mapToInt(Integer::parseInt).sum(); | Integer totalInsured = insured.values().stream().mapToInt(Integer::parseInt).sum(); | ||
</source> | |||
<source lang="java"> | |||
Map<String, String> filter = insured.entrySet().stream() | |||
.filter(map -> !"Children".equals(map.getKey())) | |||
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); | |||
</source> | </source> | ||
---- | ---- | ||
<source lang="java"> | <source lang="java"> | ||
Map<String, String> mapping = Stream.of("travel_adult:Adults", "travel_children:Children", "travel_senior_citizen:Senior Citizen") | 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() | Map<String, String> reverse = mapping.entrySet().stream() |
Revision as of 23:19, 5 September 2021
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()));
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()));
References
|