0

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.

4

6 回答 6

2

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);
于 2012-11-22T10:38:51.090 回答
1

No, collections don't suport primitive types, so you have to use a wrapper classes for primitive types or array.

于 2012-11-22T10:39:36.923 回答
1

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
于 2012-11-22T10:41:56.467 回答
0

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.

于 2012-11-22T10:38:27.613 回答
0

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.

于 2012-11-22T10:41:29.413 回答
0

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

于 2012-11-22T10:49:17.963 回答