Java中类似元组的对象上的分组
我有一组独特的位置对象,数据如下Java中类似元组的对象上的分组,java,java-8,Java,Java 8,我有一组独特的位置对象,数据如下 location 1: "USA", "AZ", "Phoenix" location 2: "USA", "AZ", "Scottsdale" location 3: "USA", "AZ", "Peoria" Front end needs following json structure to render UI: "USA"-> [{"AZ" -> ["Phoenix", "Scottsdale","Peoria"]},
location 1: "USA", "AZ", "Phoenix"
location 2: "USA", "AZ", "Scottsdale"
location 3: "USA", "AZ", "Peoria"
Front end needs following json structure to render UI:
"USA"-> [{"AZ" -> ["Phoenix", "Scottsdale","Peoria"]},
{"MD" -> ["Baltimore", "Gaithersburg","OwingsMills"]}
]
用于Location.Java的Java POJO
我已经编写了一系列代码,这些代码是用来迭代一组位置,并使用FacetsGeo对象构建包含一组状态对象的Country对象,包含一组City对象,并从这些数据模型中生成json
我在想,有更好的方法可以做到这一点:可能是使用Java8流式API
如我上面所说,任何帮助都是关于平面位置构造的示例数据
public class Location {
private final String country;
private final String state;
private final String city;
public Location(final String country, final String state, final
String city) {
this.country = country;
this.state = state;
this.city = city;
}
public String getCountry() {
return country;
}
public String getState() {
return state;
}
public String getCity() {
return city;
}
@Override
public boolean equals(Object thatLocation) {
if (thatLocation == this) return true;
if (!(thatLocationCriteria instanceof Location)) {
return false;
}
Location location = (Location) thatLocation;
return Objects.equals(this.country, location.country) &&
Objects.equals(this.state, location.state) &&
Objects.equals(this.city, location.city);
}
@Override
public int hashCode() {
return Objects.hash(this.country, this.state, this.city);
}
}
FacetsGeo.java
public class FacetsGeo {
private String label;
private String value;
private String type;
private String subtype;
private List<FacetsGeo> childFacetsGeo;
}
公共类FacetsGeo{
私有字符串标签;
私有字符串值;
私有字符串类型;
私有字符串子类型;
私人名单;
}
我试图在前端数据结构上生成的内容:
` List<FacetsGeo> facetsList = new ArrayList<FacetsGeo>();
Optional<locations> locationsOptional = Optional.ofNullable(locations);
if(locationsOptional.isPresent()) {
//locations
for (final Location location : locationsOptional.get().getLocations()) {
FacetsGeo facetCountry = new FacetsGeo();
if(location.getCountry() != null
&& location.getCountry().equalsIgnoreCase("United States")
&& location.getState() != null) {
boolean countryExists = false;
for(FacetsGeo facetGeo: facetsList) {
if(facetGeo.getType() != null && facetGeo.getSubtype() != null
&& facetGeo.getType().equalsIgnoreCase("location")
&& facetGeo.getSubtype().equalsIgnoreCase("country")
&& facetGeo.getValue().equalsIgnoreCase(location.getCountry())) {
facetCountry = facetGeo;
if(facetCountry.getSubCriteria() == null) {
facetCountry.setSubCriteria(new ArrayList<FacetsGeo>());
}
countryExists = true;
break;
}
}
if(!countryExists) {
facetCountry.setLabel(location.getCountry());
facetCountry.setValue(location.getCountry());
facetCountry.setType("location");
facetCountry.setSubtype("country");
FacetsGeo subcriteriaState = new FacetsGeo();
subcriteriaState.setLabel(location.getState());
subcriteriaState.setValue(location.getState());
subcriteriaState.setType("location");
subcriteriaState.setSubtype("state");
FacetsGeo subcriteria = new FacetsGeo();
subcriteria.setLabel(location.getCity());
subcriteria.setValue(location.getCity());
subcriteria.setType("location");
subcriteria.setSubtype("city");
subcriteriaState.setSubCriteria(new ArrayList<FacetsGeo>());
subcriteriaState.getSubCriteria().add(subcriteria);
facetCountry.setSubCriteria(new ArrayList<FacetsGeo>());
facetCountry.getSubCriteria().add(subcriteriaState);
searchBarFacetsList.add(searchBarFacetCountry);
} else {
FacetsGeo subcriteriaCity = new FacetsGeo();
subcriteriaCity.setLabel(location.getCity());
subcriteriaCity.setValue(location.getCity());
subcriteriaCity.setType("location");
subcriteriaCity.setSubtype("city");
FacetsGeo facetStateToAdd = new FacetsGeo();
boolean stateExists = false;
for(FacetsGeo facetState: facetCountry.getSubCriteria()) {
if(facetState.getType() != null && facetState.getSubtype() != null
&& facetState.getType().equalsIgnoreCase("location")
&& facetState.getSubtype().equalsIgnoreCase("state")
&& facetState.getValue().equalsIgnoreCase(location.getState())) {
facetStateToAdd = facetState;
if(facetStateToAdd.getSubCriteria() == null) {
facetStateToAdd.setSubCriteria(new ArrayList<FacetsGeo>());
}
stateExists = true;
break;
}
}
if(!stateExists) {
facetStateToAdd.setLabel(location.getState());
facetStateToAdd.setValue(location.getState());
facetStateToAdd.setType("location");
facetStateToAdd.setSubtype("state");
if(facetStateToAdd.getSubCriteria() == null) {
facetStateToAdd.setSubCriteria(new ArrayList<SearchBarFacets>());
}
facetStateToAdd.getSubCriteria().add(subcriteriaCity);
facetCountry.getSubCriteria().add(facetStateToAdd);
} else {
facetStateToAdd.getSubCriteria().add(subcriteriaCity);
facetCountry.getSubCriteria().add(facetStateToAdd);
}
}
}
}
}`
`List facetsList=new ArrayList();
可选位置可选=可选的可用位置(位置);
if(locationsOptional.isPresent()){
//地点
对于(最终位置:locationsOptional.get().getLocations()){
FacetsGeo facetCountry=新FacetsGeo();
如果(location.getCountry()!=null
&&location.getCountry().equalsIgnoreCase(“美国”)
&&location.getState()!=null){
布尔值=false;
用于(facetGeo facetGeo:facetsList){
if(facetGeo.getType()!=null&&facetGeo.getSubtype()!=null
&&facetGeo.getType().equalsIgnoreCase(“位置”)
&&facetGeo.getSubtype().equalsIgnoreCase(“国家”)
&&FaceGeo.getValue().equalsIgnoreCase(location.getCountry()){
facetCountry=facetGeo;
if(facetCountry.getSubCriteria()==null){
facetCountry.setSubCriteria(新的ArrayList());
}
countryExists=true;
打破
}
}
如果(!countryExists){
setLabel(location.getCountry());
setValue(location.getCountry());
facetCountry.setType(“位置”);
facetCountry.setSubtype(“国家”);
FacetsGeo次标准状态=新的FacetsGeo();
setLabel(location.getState());
subcriteriaState.setValue(location.getState());
次标准状态。集合类型(“位置”);
次标准状态。集合子类型(“状态”);
FacetsGeo次标准=新的FacetsGeo();
setLabel(location.getCity());
setValue(location.getCity());
次标准集类型(“位置”);
次标准。设置子类型(“城市”);
setSubCriteriaState.setSubCriteria(新的ArrayList());
subcriteriaState.getSubCriteria().add(subcriteria);
facetCountry.setSubCriteria(新的ArrayList());
facetCountry.getSubCriteria().add(subcriteriaState);
添加(searchBarFacetCountry);
}否则{
FacetsGeo次标准=新FacetsGeo();
setLabel(location.getCity());
setValue(location.getCity());
次标准集类型(“位置”);
次标准。集合子类型(“城市”);
FacetsGeo facetStateToAdd=新FacetsGeo();
布尔stateExists=false;
对于(FacetsGeo facetState:facetCountry.getSubCriteria()){
if(facetState.getType()!=null&&facetState.getSubtype()!=null
&&facetState.getType().equalsIgnoreCase(“位置”)
&&facetState.getSubtype().equalsIgnoreCase(“状态”)
&&facetState.getValue().equalsIgnoreCase(location.getState()){
facetStateToAdd=facetState;
if(facetStateToAdd.getSubCriteria()==null){
facetStateToAdd.setSubCriteria(新的ArrayList());
}
stateExists=true;
打破
}
}
如果(!stateExists){
facetStateToAdd.setLabel(location.getState());
facetStateToAdd.setValue(location.getState());
facetStateToAdd.setType(“位置”);
facetStateToAdd.setSubtype(“状态”);
if(facetStateToAdd.getSubCriteria()==null){
facetStateToAdd.setSubCriteria(新的ArrayList());
}
facetStateToAdd.getSubCriteria().add(次标准);
facetCountry.getSubCriteria().add(facetStateToAdd);
}否则{
facetStateToAdd.getSubCriteria().add(次标准);
facetCountry.getSubCriteria().add(facetStateToAdd);
}
}
}
}
}`
对于Java 8流媒体解决方案,可以按如下方式完成
这个特定的解决方案不会生成与问题中所示完全相同的JSON,因为我认为一个状态对象数组(每个对象有一个键来命名状态)不是一个好的JSON结构
如果JSON必须如问题所示,那么这个解决方案将为实现这一目标提供一个良好的开端
Set<Location> locationSet = Set.of(
new Location("USA", "AZ", "Phoenix"),
new Location("USA", "AZ", "Scottsdale"),
new Location("USA", "AZ", "Peoria"),
new Location("USA", "MD", "Baltimore"),
new Location("USA", "MD", "Gaithersburg"),
new Location("USA", "MD", "OwingsMills"),
new Location("CA", "ON", "Toronto"));
Map<String, Map<String, List<String>>> countryMap = locationSet.stream()
.collect(Collectors.groupingBy(Location::getCountry, TreeMap::new,
Collectors.groupingBy(Location::getState, TreeMap::new,
Collectors.mapping(Location::getCity, Collectors.toList()))));
System.out.println(JSONWriter.valueToString(countryMap));
如何在位置和FacetsGeo之间绘制地图?F中的标签、值等字段是什么意思
{"CA":{"ON":["Toronto"]},"USA":{"AZ":["Phoenix","Scottsdale","Peoria"],"MD":["OwingsMills","Baltimore","Gaithersburg"]}}