gpt4 book ai didi

java - 获取 LinkedHashMap 中每个条目的最后一个元素?

转载 作者:行者123 更新时间:2023-12-04 13:08:03 28 4
gpt4 key购买 nike

我有一个 map ,我想从该 map 中提取每个键的最后一个值,但我无法获取:

import java.util.LinkedHashMap;
import java.util.Map;

class MyTestClass {

private static final String almacenNombreClase = "Professor";

private static final String TXT = "TXT";
private static final String NUM = "NUM";

public static void main(String[] args) {
Map<String, Map<String, String>> mapNombreClaseYPropiedades = new LinkedHashMap<>();
mapNombreClaseYPropiedades.put("Professor", Map.of("texto", TXT,
"tipo", TXT,
"prueba", NUM,
"dificultad", NUM ));
mapNombreClaseYPropiedades.put("Exam", Map.of("texto", TXT,
"nivel", NUM ));

//values of mapNombreClaseYPropiedades
Map<String, String> valores = mapNombreClaseYPropiedades.get(almacenNombreClase);

valores.forEach((k, v) -> {
// Here I want to check the last element of each entry
if (v instanceof String ) {
System.out.println("String " + k + ",");
} else {
System.out.println("Integer " + k + ",");
}
});
// I want to see output
"dificultad=NUM"
"nivel=NUM"
}
}

摘录:

dificultad=NUM
nivel=NUM

即每个键的最后 NUM 个条目

最佳答案

您可以使用 reduceMap<String,String> 中提取最后一个值然后使用 toMap 收集它们进入LinkedHashMap保存广告订单

但作为保留顺序的注释,您必须使用 LinkedHashMap也用于输入和输出

    Map<String,String> valores = mapNombreClaseYPropiedades.values()
.stream()
.map(map->map.entrySet().stream().reduce((prev,next)->next))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (prev,next)->next,LinkedHashMap::new));

关于java - 获取 LinkedHashMap 中每个条目的最后一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68461609/

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