Java Lambda: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:
<source lang="java">
<source lang="java">
Double totalInsured = insured.values().stream().mapToDouble(d -> Double.parseDouble(d)).sum();
Double totalInsured = insured.values().stream().mapToDouble(d -> Double.parseDouble(d)).sum();
Double totalInsured = insured.values().stream().mapToDouble(Double::parseDouble).sum();
</source>
</source>


<source lang="java">
<source lang="java">
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();
</source>
</source>



Revision as of 22:58, 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();

References