gpt4 book ai didi

java - 将国家域功能转换为 Java8 Lambda

转载 作者:行者123 更新时间:2023-11-30 06:41:10 25 4
gpt4 key购买 nike

我有以下代码,它根据我从 REST API 获得的传入国家详细信息获取所有域列表

private static List<Domain> testDataInPlain() {
List<Domain> allDomains = new ArrayList<>();
List<Country> countries = buildIncomingCountries();
List<Country> availableCountries = availableCountries();

for (Country availableCountry : availableCountries) {
if(countries.contains(availableCountry)) {
List<Domain> availableDomains = getBusinessCountryDomains(availableCountry);
if(availableDomains == null) {
return new ArrayList<>();
} else {
for (Domain availableDomain : availableDomains) {
if(availableCountry.getName().equals(availableDomain.getCountry())) {
allDomains.add(availableDomain);
}
}
}
}
}
return allDomains;
}

我正在尝试将上面的代码转换为 Java8 Lambda 函数,到目前为止我转换的代码如下所示,但它不正确

private static List<?> testDataInLambda() {
List<Domain> allDomains = new ArrayList<>();
List<Country> countries = buildIncomingCountries();
List<Country> availableCountries = availableCountries();

return availableCountries.stream()
.filter(countries::contains)
.map(country -> {
List<Domain> availableDomains = getBusinessCountryDomains(country);
return availableDomains;
})
.filter(allDomains::contains)
.collect(Collectors.toList());
}

我无法将完整的转换带入 Lambda,因为我一直在为

带入 lambda 函数
if(availableCountry.getName().equals(availableDomain.getCountry()))

谁能帮我解决这个问题?

我的完整源代码如下所示

package com.example.demo;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
public static void main(String[] args) {
System.out.println(testDataInPlain());
System.out.println(testDataInLambda());
}

private static List<Domain> testDataInPlain() {
List<Domain> allDomains = new ArrayList<>();
List<Country> countries = buildIncomingCountries();
List<Country> availableCountries = availableCountries();

for (Country availableCountry : availableCountries) {
if(countries.contains(availableCountry)) {
List<Domain> availableDomains = getBusinessCountryDomains(availableCountry);
if(availableDomains == null) {
return new ArrayList<>();
} else {
for (Domain availableDomain : availableDomains) {
if(availableCountry.getName().equals(availableDomain.getCountry())) {
allDomains.add(availableDomain);
}
}
}
}
}
return allDomains;
}

private static List<?> testDataInLambda() {
List<Domain> allDomains = new ArrayList<>();
List<Country> countries = buildIncomingCountries();
List<Country> availableCountries = availableCountries();

return availableCountries.stream()
.filter(countries::contains)
.map(country -> {
List<Domain> availableDomains = getBusinessCountryDomains(country);
return availableDomains;
})
.filter(allDomains::contains)
.collect(Collectors.toList());
}

private static List<Country> buildIncomingCountries() {
// I am mocking the below details
List<Country> countries = new ArrayList<>();
countries.add(new Country("IND",1));
countries.add(new Country("USA",2));
countries.add(new Country("GER",3));
return countries;
}

private static List<Country> availableCountries() {
// I am mocking the below details
List<Country> countries = new ArrayList<>();
countries.add(new Country("IND",1));
countries.add(new Country("KEN",2));
countries.add(new Country("GER",3));
countries.add(new Country("FRA",4));
countries.add(new Country("JPN",5));
countries.add(new Country("CHN",6));
countries.add(new Country("UAE",7));
countries.add(new Country("IRE",8));
return countries;
}

private static List<Domain> getBusinessCountryDomains(Country country) {
// I am mocking the below details based on the country
List<Domain> domains = new ArrayList<>();
domains.add(new Domain(".in","IND"));
domains.add(new Domain(".zim","ZIM"));
domains.add(new Domain(".den","DEN"));
domains.add(new Domain(".fra","FRA"));
return domains;
}
}

@Data
@AllArgsConstructor
class Country {
private String name;
private Integer code;
}

@Data
@AllArgsConstructor
class Domain {
private String name;
private String country;
}

最佳答案

这可能有效:

availableCountries.stream()
.filter(availableCountries::contains) // Stream<Country> of qualified countries
.map(country -> Optional // attempt to map each Cuntry to Domain
.ofNullable(availableDomains) // Optional<List<Domain>>
.orElse(Collections.emptyList()) // or else an empty List
.stream()
.filter(availableDomain -> availableDomain.getName().equals(country.getCountry()))
.findAny()
.orElse(null)) // if the name doesn't match, then null
.filter(Objects::nonNull) // filter the nulls out
.collect(Collectors.toList()); // and produce a List<Domain>

orElse(Collections.emptyList())保证整个Stream产生一个空的 List<Domain>因为没有Country如果与空的 List<Domain> 相比,将是合格的.

关于java - 将国家域功能转换为 Java8 Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com