gpt4 book ai didi

Java 8 流维护特定的映射键顺序从分组操作返回

转载 作者:行者123 更新时间:2023-12-03 18:36:59 24 4
gpt4 key购买 nike

我有一个包含 TitleIsbnBean 对象集合的列表。我编写了以下代码片段,按以下学习区域类型按此集合分组。

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

);

但我想在上述流的 map 返回中保留以下特定顺序。
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()));

最佳答案

考虑到您需要 Map 的所需键顺序您正在收集,您可以收集到TreeMapComparator基于 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()));

关于Java 8 流维护特定的映射键顺序从分组操作返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61436453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com