gpt4 book ai didi

java - Map的订单输出

转载 作者:行者123 更新时间:2023-12-02 03:25:02 25 4
gpt4 key购买 nike

编写REST API,请求和响应都是Json。我将 json 响应作为 Map 进行处理,并想知道为什么它在屏幕上的输出不是按照我制作的顺序排列的?我的 Controller 的示例代码,我在其中构建 map :

@ExceptionHandler(ApiErrorNotFound.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public @ResponseBody Map handleDaoException(HttpServletRequest req, ApiErrorNotFound exc) {
Map<String, String> res = new HashMap<>();
Date date = new Date();
res.put("timestamp", String.valueOf(new Timestamp(date.getTime())));
res.put("status", String.valueOf(exc.getStatus()));
res.put("error", "Not Found");
res.put("exception", ApiErrorNotFound.class.getName());
res.put("message", exc.getMessage());
res.put("path", req.getServletPath());
return res;
}

输出是:

{
"exception": "com.zzheads.HomeAutomation.exceptions.ApiErrorNotFound",
"path": "/room/9",
"error": "Not Found",
"message": "Can't find room with 9 id. (com.zzheads.HomeAutomation.controller.RoomController.getRoomById(RoomController.java:63))",
"timestamp": "2016-08-21 15:49:01.961",
"status": "404"
}

最佳答案

不保证HashMap的迭代顺序与插入顺序相同。

如果你想在迭代时保持插入顺序,请查看:

LinkedHashMap

关于java - Map的订单输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39064447/

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