What is the Java equivalent of Objective-C's NSDictionary?

你。 提交于 2019-12-17 10:59:47

问题


What is the closest implementation of Objective-C's NSDictionary in Java? To me, it looks like HashMap<String, Object>, but I'm very new to Objective-C.

Thanks


回答1:


NSDictionary is a class cluster (see the "Class Cluster" section in The Cocoa Fundamentals Guide), meaning that the actual implementation is hidden from you, the API user. In fact, the Foundation framework will choose the appropriate implementation at run time based on amount of data etc. In addition, NSDictionary can take any id as a key, not just NSString (of course, the -hash of the key object must be constant).

Thus, closest analog is probably Map<Object,Object>.




回答2:


For practical usage: HashMap<String, Object> would be the way to go for a simple, non-parallel dictionary. However, if you need something that is thread-safe (atomic), you'd have to use HashTable<String, Object> which is thread safe, but - notably - does not accept null as a valid value or key.




回答3:


I'm admittedly an Android newbie, but to me ContentValues seems to be the closest thing to NSDictionary.



来源:https://stackoverflow.com/questions/8128408/what-is-the-java-equivalent-of-objective-cs-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!