@org.junit.Test
public void question01() {
List<Policy> policyList = PolicyService.buildPolicyList();
LocalDateTime now = LocalDateTime.now();
List<Policy> policies = policyList.stream().filter(policy -> now.isAfter(policy.getEffectiveDate())).collect(toList());
policies.forEach(System.out::println);
}
@org.junit.Test
public void question02() {
List<Policy> policyList = PolicyService.buildPolicyList();
Stream<List<Risk>> listStream = policyList.stream()
.map(policy -> policy.getRiskList()).filter(risks -> risks != null && risks.isEmpty());
Stream<Risk> riskStream = listStream.flatMap(List::stream);
Stream<String> stringStream = riskStream.map(Risk::getName);
String collect = stringStream.collect(Collectors.joining(","));
System.out.println(collect);
}
@org.junit.Test
public void question03() {
List<Policy> policyList = PolicyService.buildPolicyList();
Map<String, LocalDateTime> collect = policyList
.stream()
.collect(Collectors.toMap(Policy::getPolicyNo, Policy::getExpireDate));
LocalDateTime now = LocalDateTime.now();
Map<String, String> collect2 = collect.entrySet()
.stream()
.collect(toMap(sEntry -> sEntry.getKey(), sEntry -> {
String sDay = Duration.between(now, sEntry.getValue()).toDays() + "";
return sDay;
}));
System.out.println(collect2);
}
@org.junit.Test
public void question04() {
List<Policy> policyList = PolicyService.buildPolicyList();
policyList.forEach(policy -> System.out.println(policy.getPremium()));
Double avg = policyList.stream().collect(Collectors.averagingDouble(Policy::getPremium));
System.out.println("平均保费为:" + avg);
}
@org.junit.Test
public void question05() {
LocalDateTime start = LocalDateTime.now();
List<Policy> policyList = PolicyService.buildPolicyList();
PolicyService policyService = new PolicyService();
List<Policy> list = policyList.parallelStream().map(policy ->
policyService.save(policy)
).collect(toList());
list.stream().forEach(policy -> System.out.println(policy));
LocalDateTime end = LocalDateTime.now();
System.out.println("查询总耗时:::" + Duration.between(start, end).toMillis());
}
@org.junit.Test
public void question06() {
LocalDateTime start = LocalDateTime.now();
List<Policy> policyList = PolicyService.buildPolicyList();
ExecutorService executor = Executors.newFixedThreadPool(15);
List<CompletableFuture<Policy>> completablePolicyList = policyList.stream().map(policy -> CompletableFuture.supplyAsync(() -> {
Policy save = new PolicyService().save(policy);
return save;
}, executor)).collect(toList());
completablePolicyList.stream().forEach(policyCompletableFuture -> System.out.println(policyCompletableFuture.join()));
LocalDateTime end = LocalDateTime.now();
System.out.println("查询总耗时:::" + Duration.between(start, end).toMillis());
}
@org.junit.Test
public void question07() {
List<Policy> policyList = PolicyService.buildPolicyList();
ArrayList<Risk> risks = new ArrayList<>();
risks.add(new Risk("法外狂徒", 100));
List<Policy> policyList1 = policyList.stream().map(policy -> {
List<Risk> risks1 = ofNullable(policy.getRiskList()).orElse(risks);
policy.setRiskList(risks1);
return policy;
}).collect(toList());
policyList1.stream().forEach(System.out::println);
}
@org.junit.Test
public void question08() {
List<Policy> policyList = PolicyService.buildPolicyList();
Stream<Risk> riskStream = policyList.stream()
.map(Policy::getRiskList)
.filter(risks -> risks != null)
.flatMap(List::stream);
Map<String, Integer> riskMap = riskStream.collect(toMap(Risk::getName, Risk::getAge));
System.out.println(riskMap);
}
@org.junit.Test
public void question09() {
LocalDate localDate = LocalDate.now().plusDays(500);
System.out.println(localDate);
}
public interface FatherPolicyService {
default Policy save(Policy policy){
try {
Thread.sleep(2000);
return policy;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}