Я хочу получить отсортированную карту, используя два поля (штат и муниципалитет) из несортированного списка.
Вот мои занятия
class StateMunicipality {
private StateMunicipalityId id;
private String nameState;
private String nameMunicipality;
public StateMunicipality(StateMunicipalityId id, String nameState, String nameMunicipality) {
this.id = id;
this.nameState = nameState;
this.nameMunicipality = nameMunicipality;
}
public StateMunicipalityId getId() {
return id;
}
public void setId(StateMunicipalityId id) {
this.id = id;
}
public String getNameState() {
return nameState;
}
public void setNameState(String nameState) {
this.nameState = nameState;
}
public String getNameMunicipality() {
return nameMunicipality;
}
public void setNameMunicipality(String nameMunicipality) {
this.nameMunicipality = nameMunicipality;
}
@Override
public String toString() {
return id + ", nameState=" + nameState + ", nameMunicipality=" + nameMunicipality;
}
}
Идентификатор
class StateMunicipalityId {
private Long codeState;
private Long codeMunicipality;
public StateMunicipalityId(Long codeState, Long codeMunicipality) {
this.codeState = codeState;
this.codeMunicipality = codeMunicipality;
}
public Long getCodeState() {
return codeState;
}
public void setCodeState(Long codeState) {
this.codeState = codeState;
}
public Long getCodeMunicipality() {
return codeMunicipality;
}
public void setCodeMunicipality(Long codeMunicipality) {
this.codeMunicipality = codeMunicipality;
}
@Override
public String toString() {
return "codeState=" + codeState + ", codeMunicipality=" + codeMunicipality;
}
}
Теперь я заполняю список
System.out.println("Example unsorted List");
List<StateMunicipality> statmuns = new ArrayList<>();
statmuns.add(new StateMunicipality(new StateMunicipalityId(1L, 1L), "Atlántico", "Barranquilla"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(3L, 3L), "Antioquia", "Envigado"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(2L, 3L), "Valle", "Yumbo"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(3L, 2L), "Antioquia", "Itagüí"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(2L, 1L), "Valle", "Cali"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(3L, 1L), "Antioquia", "Medellín"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(1L, 2L), "Atlántico", "Soledad"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(1L, 3L), "Atlántico", "Puerto Colombia"));
statmuns.add(new StateMunicipality(new StateMunicipalityId(2L, 2L), "Valle", "Palmira"));
statmuns.stream().forEach(System.out::println);
Вывод, как был вставлен!
Example unsorted List
codeState=1, codeMunicipality=1, nameState=Atlántico, nameMunicipality=Barranquilla
codeState=3, codeMunicipality=3, nameState=Antioquia, nameMunicipality=Envigado
codeState=2, codeMunicipality=3, nameState=Valle, nameMunicipality=Yumbo
codeState=3, codeMunicipality=2, nameState=Antioquia, nameMunicipality=Itagüí
codeState=2, codeMunicipality=1, nameState=Valle, nameMunicipality=Cali
codeState=3, codeMunicipality=1, nameState=Antioquia, nameMunicipality=Medellín
codeState=1, codeMunicipality=2, nameState=Atlántico, nameMunicipality=Soledad
codeState=1, codeMunicipality=3, nameState=Atlántico, nameMunicipality=Puerto Colombia
codeState=2, codeMunicipality=2, nameState=Valle, nameMunicipality=Palmira
Я ХОЧУ ПОЛУЧИТЬ (во вложенных отсортированных картах) в соответствии с этим порядком, сначала на nameState
, затем nameMunicipality
.
codeState=3, codeMunicipality=3, nameState=Antioquia, nameMunicipality=Envigado
codeState=3, codeMunicipality=2, nameState=Antioquia, nameMunicipality=Itagüí
codeState=3, codeMunicipality=1, nameState=Antioquia, nameMunicipality=Medellín
codeState=1, codeMunicipality=1, nameState=Atlántico, nameMunicipality=Barranquilla
codeState=1, codeMunicipality=3, nameState=Atlántico, nameMunicipality=Puerto Colombia
codeState=1, codeMunicipality=2, nameState=Atlántico, nameMunicipality=Soledad
codeState=2, codeMunicipality=1, nameState=Valle, nameMunicipality=Cali
codeState=2, codeMunicipality=2, nameState=Valle, nameMunicipality=Palmira
codeState=2, codeMunicipality=3, nameState=Valle, nameMunicipality=Yumbo
Потом я генерировал свой EDITED код, НО я что-то упустил !! ...
Map<Long, Map<Long, StateMunicipality>> sortedMapOfMaps = statmuns
.stream().sorted(
Comparator.comparing(StateMunicipality::getNameState).thenComparing(
Comparator.comparing(StateMunicipality::getNameMunicipality))
).collect(Collectors.groupingBy(
statmuni -> statmuni.getId().getCodeState(),
Collectors.toMap(
statmuni -> statmuni.getId().getCodeMunicipality(),
Function.identity(),
(e1, e2) -> e1, LinkedHashMap::new)
));
System.out.println("sortedMapOfMaps");
sortedMapOfMaps.entrySet().stream().forEach(
set -> {
//System.out.println(set.getKey() + " = " + set.getValue());
set.getValue().forEach((key, val) -> {
System.out.println(key + " = " + val.toString());
});
}
);
Я довольно близко!
sortedMapOfMaps
1 = codeState=1, codeMunicipality=1, nameState=Atlántico, nameMunicipality=Barranquilla
3 = codeState=1, codeMunicipality=3, nameState=Atlántico, nameMunicipality=Puerto Colombia
2 = codeState=1, codeMunicipality=2, nameState=Atlántico, nameMunicipality=Soledad
1 = codeState=2, codeMunicipality=1, nameState=Valle, nameMunicipality=Cali
2 = codeState=2, codeMunicipality=2, nameState=Valle, nameMunicipality=Palmira
3 = codeState=2, codeMunicipality=3, nameState=Valle, nameMunicipality=Yumbo
3 = codeState=3, codeMunicipality=3, nameState=Antioquia, nameMunicipality=Envigado
2 = codeState=3, codeMunicipality=2, nameState=Antioquia, nameMunicipality=Itagüí
1 = codeState=3, codeMunicipality=1, nameState=Antioquia, nameMunicipality=Medellín
Мой код для SortedState.
System.out.println("sortedMapState");
Map<Long, StateMunicipality> sortedMapState = statmuns.stream().sorted(
Comparator.comparing(StateMunicipality::getNameState)
).collect(
Collectors.toMap(stateMunicipality -> stateMunicipality.getId().getCodeState(),
Function.identity(),
(e1, e2) -> e1, LinkedHashMap::new)
);
sortedMapState.forEach((key, val) -> {
System.out.println(key + " = " + val.getNameState());
});
У меня заказанная карта states
sortedMapState
3 = Antioquia
1 = Atlántico
2 = Valle
Какая часть мне не хватает?