gpt4 book ai didi

java - 从 Linked hashMap 派生时数组的顺序是什么?

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

我创建了一个链接 HashMap ,然后使用该映射生成了如下所示的映射键数组:-

Map<String, String> map = new LinkedHashMap<>();
map.put("Str1", "Str1");
map.put("Str1", "Str1");

String[] keyArray= map.keySet().stream.toArray(String[]::new);

现在的问题是 keyArray 的顺序是什么?它与链接 HashMap 的插入顺序相同还是不同?如果不同,那么我们如何生成一个与 linkedhashmap 具有相同插入顺序的数组?

最佳答案

来自java.util.stream docs :

Streams may or may not have a defined encounter order. Whether or not a stream has an encounter order depends on the source and the intermediate operations. Certain stream sources (such as List or arrays) are intrinsically ordered, whereas others (such as HashSet) are not. Some intermediate operations, such as sorted(), may impose an encounter order on an otherwise unordered stream, and others may render an ordered stream unordered, such as BaseStream.unordered().

来自LinkedHashMap docs

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order)

由于 LinkedHashMap 用作源并且它维护排序,因此结果数组预计与其源具有相同的排序。

关于java - 从 Linked hashMap 派生时数组的顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306823/

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