Java8流通过操作维护从组返回的特定映射键顺序

Java8流通过操作维护从组返回的特定映射键顺序,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个包含TitleIsbnBean对象集合的列表。我编写了下面的代码片段,按学习区域类型按此集合分组,如下所示 titleListByLearningArea = nonPackageIsbnList.stream() .collect(groupingBy(TitleIsbnBean::getKeylearningarea, LinkedHashMap::new,Collectors.toList())

我有一个包含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()));

考虑到要收集的映射需要所需的键顺序,可以使用比较器根据指定的索引收集到树映射,例如:

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()));

考虑到要收集的映射需要所需的键顺序,可以使用比较器根据指定的索引收集到树映射,例如:

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()));
首先使用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()));
首先使用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()));

我想在地图返回中保留以下特定顺序。。。你的意思是钥匙的顺序应该是一样的?这里什么是非包装ISBNLIST?您可以添加详细信息吗。@Eklavya nonPackageIsbnList是一个包含TitleIsbnBean对象集合的列表@Naman keys order应该等于Commerce、English、Health&PE………@Eklavya没有特定顺序。我想在地图返回中保留以下特定顺序。。。你的意思是钥匙的顺序应该是一样的?这里什么是非包装ISBNLIST?您可以添加有关它的详细信息吗。@Eklavya nonPackageIsbnList是一个包含标题BNBean对象集合的列表@Naman keys order应该等于Commerce、English、Health和PE………@Eklavya没有特定的顺序。在Comparator Comparator.ComparingInt->orderKeys.indexOft.getKeylearningarea中。排序后,它保留了新的顺序,我认为是在Comparator Comparator.comparingitt->orderKeys.indexOft.getKeylearningarea中。我想在分类之后,它保留了新的秩序