[英]What is the best practice for KV object in java?
What is the best practice for creating a single key value object in java? 在Java中创建单个键值对象的最佳实践是什么? I know there is: Object, Pair, Map.Entry , KeyValue.
我知道有:对象,配对,Map.Entry,KeyValue。 For a simple KV use such as {"name":"coolName"} what should i use?
对于简单的KV使用,例如{“ name”:“ coolName”},我应该使用什么?
The most standard way to represent a key-value pair is with Map.Entry
: 表示键值对的最标准方法是使用
Map.Entry
:
A map entry (key-value pair).
映射条目(键值对)。
This is better than the others you suggest because: 这比您建议的其他方法要好,因为:
Object
: has no notion of it having any meaningful structure Object
:不具有任何有意义的结构的概念 Pair
: better, but does not semantically convey that the first thing is a key and the second is a value. Pair
:更好,但在语义上没有传达第一件事是键,第二件事是值。 KeyValue
: don't know what this is, but I don't think it's part of the standard SDK. KeyValue
:不知道这是什么,但我认为它不是标准SDK的一部分。 Map.Entry
is in java.util
, so it's available everywhere. Map.Entry
在java.util
,因此随处可见。
You can create an instance using: 您可以使用以下方法创建实例:
new AbstractMap.SimpleEntry<>(key, value)
As pointed out by Michael , Java 9 adds a helper method : 正如Michael指出的那样,Java 9添加了一个辅助方法 :
Map.entry(key, value)
您可以使用Pair<K,V>
(来自commons-lang3的Map.Entry<K,V>
的实现)和Java 11中的相关方法: Map.entry 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.