gpt4 book ai didi

java - 我需要一个保留插入顺序的不可变键值结构

转载 作者:IT老高 更新时间:2023-10-28 20:51:27 27 4
gpt4 key购买 nike

我想找到类似 ImmutableLinkedHashMap<> 的内容在 Guava 图书馆。我需要使用带有插入顺序的不可变键值数据结构。那么,我应该使用什么?

最佳答案

我不确定我是否完全理解您所追求的,但如果它真的是不可变的 Map ,你可能想看看 ImmutableMap

如文档中所述:

An immutable, hash-based Map with reliable user-specified iteration order. Does not permit null keys or values.

Unlike Collections.unmodifiableMap(java.util.Map<? extends K, ? extends V>), which is a view of a separate map which can still change, an instance of ImmutableMap contains its own data and will never change. ImmutableMap is convenient for public static final maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided to your class by a caller

例如,您可以以类似的方式使用它:

Map<Integer, String> m = ImmutableMap.of(5,"Five",6,"Six",7,"Seven");

希望这就是你所追求的。

关于java - 我需要一个保留插入顺序的不可变键值结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14982820/

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