Java 8使用两个字段获取地图的排序地图

时间:2019-01-09 19:27:07

标签: java sorting java-8 java-stream linkedhashmap

我想从未排序的列表中使用两个字段(州和市)来获取已排序的地图。

这是我的课程

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;
  }

}

Id

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

然后我正在生成我的已编辑代码,但是我缺少了一些东西!...

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

我想念的是什么?

0 个答案:

没有答案