Java 8流保持特定的映射键顺序,通过操作从组中返回。

问题描述 投票:0回答:1

我有一个包含TitleIsbnBean对象集合的列表。我写了下面的代码片断来按学习区域类型对这个集合进行分组,如下所示。

titleListByLearningArea = nonPackageIsbnList.stream()
      .collect(groupingBy(TitleIsbnBean::getKeylearningarea,
                          LinkedHashMap::new,Collectors.toList())

              );

但我想在上述流的地图返回中保留以下特定顺序。

titleListByLearningArea.put("Commerce", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("English", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Health & PE", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Humanities", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Mathematics", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Music & the Arts", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Science", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Others", new ArrayList<TitleIsbnBean>() {});

但我一旦使用流按集合进行分组,就会得到不同的顺序。如何才能在使用流分组操作时保持特定的顺序。

class TitleIsbnBean {

  private String titleName;
  private String isbn;
  private int status;
  private String keylearningarea;

  public TitleIsbnBean(String titleName, String isbn, int status, String keylearningarea){
    super();
    this.titleName = titleName;
    this.isbn = isbn;
    this.status = status;
    this.setKeylearningarea(keylearningarea);
  }

}

ArrayList<TitleIsbnBean> nonPackageIsbnList = new ArrayList<>();
Map<String,List<TitleIsbnBean>> titleListByLearningArea = new LinkedHashMap<>();

titleListByLearningArea.put("Commerce", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("English", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Health & PE", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Humanities", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Mathematics", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Music & the Arts", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Science", new ArrayList<TitleIsbnBean>() {});
titleListByLearningArea.put("Others", new ArrayList<TitleIsbnBean>() {});

titleListByLearningArea = nonPackageIsbnList.stream()
.collect(Collectors.groupingBy(TitleIsbnBean::getKeylearningarea,
                                LinkedHashMap::new,Collectors.toList()));
java-8 stream
1个回答
4
投票

考虑到你需要一个所需的键的顺序,以便为你的 Map 您的收藏品,您可以收藏到一个地方。TreeMap 带着 Comparator 基于 index 指定,如。

Collection<TitleIsbnBean> nonPackageIsbnList = .... //initialisation
List<String> orderedKeys = List.of("Commerce", "English", "Health & PE", "Humanities",
        "Mathematics", "Music & the Arts", "Science", "Others");

Map<String, List<TitleIsbnBean>> titleListByLearningArea = nonPackageIsbnList.stream()
        .collect(Collectors.groupingBy(TitleIsbnBean::getKeylearningarea,
                () -> new TreeMap<>(Comparator.comparingInt(orderedKeys::indexOf)),
                Collectors.toList()));

2
投票

首先用 Comparator 以您所需的顺序,然后以组为单位收集列表。

List<String> orderKeys = List.of("Commerce", "English", "Health & PE", "Humanities",
        "Mathematics", "Music & the Arts", "Science", "Others");

Map<String, List<TitleIsbnBean>> titleListByLearningArea = nonPackageIsbnList.stream()
                   .sorted(Comparator.comparingInt(t -> orderKeys.indexOf(t.getKeylearningarea())))
                   .collect(Collectors.groupingBy(TitleIsbnBean::getKeylearningarea,LinkedHashMap::new,Collectors.toList()));
© www.soinside.com 2019 - 2024. All rights reserved.