Java Lambda: Difference between revisions
Jump to navigation
Jump to search
Line 59: | Line 59: | ||
</source> | </source> | ||
===Collectors » Example 1==== | |||
<source lang="Java"> | <source lang="Java"> | ||
List<Map> list = new ArrayList<>(); | List<Map> list = new ArrayList<>(); | ||
Line 91: | Line 92: | ||
System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.toList())); | System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.toList())); | ||
System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.joining(","))); | System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.joining(","))); | ||
</source> | |||
===Collectors » Example 2==== | |||
<source lang="Java"> | |||
Map<String, String> requestHeaders = new HashMap<>(); | |||
requestHeaders.put("X-Auth-Token", "JWT"); | |||
requestHeaders.put("Content-Type", "Application/Json"); | |||
requestHeaders.put("Accept", "Application/Json"); | |||
Map<String, String> mapper = new HashMap<>(); | |||
mapper.put("X-Auth-Token", "Authorization"); | |||
mapper.put("Content-Type", "Content-Type"); | |||
mapper.put("Accept", "Accept"); | |||
Map<String, String> headers = requestHeaders.entrySet() | |||
.stream().filter(e -> mapper.containsKey(e.getKey())) | |||
.collect(Collectors.toMap(e -> mapper.get(e.getKey()), e -> e.getValue())); | |||
</source> | </source> | ||
Revision as of 20:54, 7 March 2023
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]));
}
Collectors
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Collectors;
Collectors » Example 1=
List<Map> list = new ArrayList<>();
Map<String, Object> commission1 = new HashMap<>();
commission1.put("Total", 1000);
commission1.put("rate", 10);
list.add(commission1);
Map<String, Object> commission2 = new HashMap<>();
commission2.put("Total", 10000);
commission2.put("rate", 10);
list.add(commission2);
Map<String, Object> commission3 = new HashMap<>();
commission3.put("Tutal", 10000);
commission3.put("rate", 10);
list.add(commission3);
//list.stream().filter(m-> m.containsKey("Total")).collect(Collectors.toList());
System.out.println(list.stream().filter(m-> m.containsKey("Total")).collect(Collectors.toList()));
list.stream().filter(m-> m.containsKey("Total")).collect(Collectors.toList()).forEach(m -> System.out.println(m.get("Total")));
//list.stream().filter(m-> m.containsKey("Total")).mapToInt(m -> (int)m.get("Total")).sum()
System.out.println(list.stream().filter(m-> m.containsKey("Total")).mapToInt(m -> (int)m.get("Total")).sum());
//list.stream().filter(m-> m.containsKey("Total")).map(m->m.get("Total")).collect(Collectors.toList())
System.out.println(list.stream().filter(m-> m.containsKey("Total")).map(m->m.get("Total")).collect(Collectors.toList()));
List<Map> filteredList = list.stream().filter(m-> m.containsKey("Total")).collect(Collectors.toList());
System.out.println(filteredList.stream().map(m->m.get("Total")).collect(Collectors.toList()));
System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.toList()));
System.out.println(filteredList.stream().map(m->String.valueOf(m.get("Total"))).collect(Collectors.joining(",")));
Collectors » Example 2=
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("X-Auth-Token", "JWT");
requestHeaders.put("Content-Type", "Application/Json");
requestHeaders.put("Accept", "Application/Json");
Map<String, String> mapper = new HashMap<>();
mapper.put("X-Auth-Token", "Authorization");
mapper.put("Content-Type", "Content-Type");
mapper.put("Accept", "Accept");
Map<String, String> headers = requestHeaders.entrySet()
.stream().filter(e -> mapper.containsKey(e.getKey()))
.collect(Collectors.toMap(e -> mapper.get(e.getKey()), e -> e.getValue()));
References
|