英文:
Difference between MAP in Groovy and Java
问题
Currently, we are using Groovy as frontend and Java Spring as backend.
In Groovy, we define a property Map parameters = [:]
, which will assign to private Map<String, Object> parameters;
in Java.
I have a question regarding the instance of this MAP interfaces in both languages. As I read, Groovy creates, by default, an instance of LinkedHashMap
, and Java creates, by default, an instance of HashMap
. Is it true?
My overall requirement is to preserve the order of data, which LinkedHashMap
does, but HashMap
does not.
So, as the frontend is already using LinkedHashMap
, it will pass ordered data into the backend.
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
Is it true?
Do I need to change the model in JAVA (backend) from Map
to LinkedHashMap
?
英文:
Currently, we are using Groovy as frontend and Java Spring as backend.
In groovy we define a property Map parameters = [:]
which will assign to private Map<String, Object> parameters;
in Java.
I have a question regarding the instance of this MAP interfaces in both languages. As I read groovy create by default instance of LinkedHashMap
and Java create by default instance of HashMap
is it true?
My overall requirement is to preserve the order of data, which LinkedHashMap
does but HashMap
not.
So as frontend is already LinkedHashMap
, it will pass ordered data into the backend.
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
Is it true?
Do I need to change model in JAVA (backend) from Map
to LinkedHashMap
?
答案1
得分: 3
Java实际上不会默认创建任何类型的映射。开发人员可以选择实例化哪种Map
实现(几乎总是如此)。Java没有类似Groovy的映射字面量[:]
的对应物,因此在Java中没有要实例化的“默认”类型。至于Groovy,确实映射字面量会创建一个LinkedHashMap
:
groovy:000> [:].getClass()
===> class java.util.LinkedHashMap
是的。在运行时,传递给setParameters()
的Map
对象将与您的“前端”代码生成的对象相同。调用行为与从Java代码中创建一个LinkedHashMap
并将其传递给该方法的方式完全相同。
运行时不会将对象重塑为HashMap
(即使它必须执行某种操作,使其成为HashMap
也会显得奇怪)。这里重要的是类型兼容性,LinkedHashMap
是 一个 Map
。
英文:
> As I read groovy create by default instance of LinkedHashMap and Java create by default instance of HashMap is it true?
Java, in fact, doesn't create any kind of map by default. The developer picks which Map
implementation they instantiate (almost always). Java doesn't have a counterpart for Groovy's map literal [:]
, so there's no "default" type to instantiate in Java. As for Groovy, it's true that the map literal creates a LinkedHashMap
:
groovy:000> [:].getClass()
===> class java.util.LinkedHashMap
> So as frontend is already LinkedHashMap, it will pass ordered data into the backend. [...] is it true?
Yes. During runtime, the Map
object that's passed to setParameters()
will be as generated by your "front-end" code. The call would simply behave the same way as though you'd created a LinkedHashMap
from your Java code and passed it to the method.
The runtime won't re-shape the object into a HashMap
(even if it had to do something like that, making it a HashMap
would be curiously arbitrary). All that matters here is that the types are compatible, and LinkedHashMap
is a Map
.
答案2
得分: 1
Map
是一个接口。因此,如果您要求一个 Map
作为参数或参数,您只是请求那些保证。
如果您需要进一步的保证(例如,要求 LinkedHashMap
保留插入顺序),那么最好明确要求这一点。
由于您谈论了“后端”和“前端”,还涉及两者之间的传输问题。如果您使用的传输方式(而不是直接调用)不能保持地图中的顺序(例如,JSON),那么最好不依赖于不同的数据类型。例如,使用键值对元组的列表。
关于默认值的注意事项
Groovy有一个用于快速创建地图的字面量([:]
或带有键的形式,如[a: 42]
)。这些地图是LinkedHashMap
,正如您所述。
===> class java.util.LinkedHashMap
由于截至到Java 15,没有用于地图的字面量,因此没有“默认值” - 默认值取决于您的需求。
总之,最好始终考虑您实际需要的数据类型。这也意味着Groovy的地图字面量可能是错误的选择。
英文:
Map
is
an interface. So if you demand a Map
as argument or parameter, you
are only requesting those guarantees.
If you demand further guarantees (e.g. that LinkedHashMap preserve
insertion order), then you are better off requesting that explicitly.
Since you are talking "backend" and "frontend" there is also the matter
of transport between the two. If you are using transports (and not
direct calls), that are no able to hold order in maps (e.g. JSON), then
you are better off to not rely on the different data types. E.g. use
a list of key-value tuples instead.
Side note about the defaults
Groovy has a literal to quickly create maps ([:]
or forms with keys
like [a: 42]
). Those maps are LinkedHashMap
as you stated.
groovy:000> [:].getClass()
===> class java.util.LinkedHashMap
Since Java as of up to 15 has no literal for maps there is no "default"
-- the default is what you make it.
In general it's best to always think about the data type you are
actually in need of. This also means, that the map literal of Groovy
can be the wrong choice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论