简体   繁体   中英

Is there any KV data structure in Java which accepts key in primitive type?

I need to store information in Key Value manner. But the built-in Map interface cannot fit for my requirement. Java Map requires both Key and Values to be reference, while I need to use primitive value as key.

Is there any data structure something like Map ? Thanks for your help!

Requirement Details:

My server written in Java runs as a daemon listening a tcp port. When a user first connect in, details about the user need to be stored in KV manner, and the second time the user connect in, his details should be able to read from the KV data structure.

I cannot use the user object as key, for it will be destructed when disconnect, and reconstructed in the second connection. The two objects are not the same reference. Integer key doesn't fit for my requirement either for the same reason.

In other words, I need to use value as key, not reference.

Keys could be considered are: UUID(long), id(int) and so on. They are all primitive type.

Still you can go with java Map as wrapper classes are available for all primitive type and java supports auto boxing, So you can use java.util.Map . ex -

Map<Long,Integer> map = new HashMap<Long,Integer>();
long uuid=10; int i= 10;
map.put(uuid,i);

不,集合不支持原始类型,因此您必须对原始类型或数组使用包装器类。

I fail to see why you can't simply wrap your primitive type in it's corresponding non-primitive class and use that as your key in a regular java map.

Map<Integer, Object> map = new HashMap<Integer, Object>();
Integer key = Integer.valueOf(5);
Object test = new Object();
map.put(key, test);
Object test2 = map.get(Integer.valueOf(5));

test.equals(test2); // will be true

What you are looking for is called a Hashmap.

Hashmap<Long, Integer> dict=new HashMap<Long, Integer>();
dict.put(24,10);
dict.put(13,63);
dict.get(13); // Equals 63

Essentially, a HashMap will take the first argument as a key, and the second as the value, exactly as you requested. You can assign any type, including a Long for larger integers than normal, although you can't pass primitives. Still, this hasn't ever been an issue for me.

The HashMap class is fine for using key-value pairs, but there is no such thing which accepts primitive types.

We'll still try to use a primitive type in the context of a Map.

HashMap<Integer, V> map = new HashMap<>();
map.put(12, someV);

As we write map.put(12, someV) , in fact, one cannot use a primitive type as the first argument of the method 'put' of class java.util.Map . But in Java, the integer '12' will automatically be 'converted' ( auto-boxed ) into the correspondenting wrapper class, in this case Integer .

So that means that there is actually an object of type Integer in the HashMap , but it is reflected as an int .

various implementations exist elsewhere, but not in the standard java library. See for example LongHashMap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM