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

前端 未结 3 1149
悲&欢浪女
悲&欢浪女 2020-12-03 07:23

What is the closest implementation of Objective-C\'s NSDictionary in Java? To me, it looks like HashMap, but I\'m very new t

相关标签:
3条回答
  • 2020-12-03 07:33

    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>.

    0 讨论(0)
  • 2020-12-03 07:38

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

    0 讨论(0)
  • 2020-12-03 07:43

    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.

    0 讨论(0)
提交回复
热议问题