Java Data Structures with Redis - java

In my code I have declared this data structure below:
LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>> GroupsOfaSignature = new LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>>();
I want to save it in redis which has its own types of data structures(Strings,hash,...) .In the redis documentation they said that we can store hashmap Where the key and the values are Strings so my question is if i can store a hashmap where the keys or values are not strings,for example a linkedhashMap like the example above

You need to convert them as strings. You can either use some Json libraries like gson to give you the equivalent Json string or you can use message pack to achieve the same.
http://msgpack.org/index.html
Hope this helps

You have to convert them to String though you can also store bitmaps . I would say when you have to store such a a representation either prefix key with some unique identifier for all the internal maps or use Json representation
You can also think of a Redis hash as a JSON object (with non-nestable objects).
Key: "usernameToUidMapping:a"
Value: "alpha" => "1"
"adam" => "312"
"acrobat" => "333"
"aromatic" => "664"
Refer below link
Redis Data Types

Related

How to parse a JSON map in Java or Kotlin where the map has a key

I'm sending a Map from a server to a Android app as a JSON file. Previously I was sending Arrays, Strings, Booleans, and Longs which were decoded by null checks and
json.getJSONArray(key)
or similar, where json is a JSONObject and key is a String.
.getJSONMap(key) does not exist and other similar questions do not provide answers that include the case where the JSON has other data and needs a key to specify where the map is.
What can I use to parse the JSON to a map given a key?
You want json.getJSONObject(key)

how to encode Map<String,Object> to String value in java

As per my understanding, we can use Base.64 to encode a string or map to string. But I am facing trouble here, converting the map to string isn't working its giving empty JSON.
So is there any way we can directly encode Map<String,Object> to String value either by using Base64 or converting map to string value.
Base64.getEncoder().encodeToString(“actualString”.getBytes());
I tried converting Map to String. It isn't working.
Now, instead of string, I want to pass a map. Please suggest some single line optimized code to encode Map<String,Object> to String value.
Base64.getEncoder().encodeToString(String.format("string1","string2","string3").getBytes());
First look at How to convert hashmap to JSON object in Java.
Then you can convert the resulting JSON to a base64 encoding.
GSON or Jackson are good libraries for JSON manipulation.
If you want to convert your map to string, getting only the values, you could do something like this:
String mapConverted = map.entrySet().stream()
.map(e -> e.getValue())
.collect(Collectors.joining(","));
In this case, the string mapConverted, contains all values of your map separated with a comma.
See following link. In Java How to Convert Map / HashMap to JSONObject? [4 Different Ways]
https://crunchify.com/in-java-how-to-convert-map-hashmap-to-jsonobject-4-different-ways/
Please check the below link where it show how to create Map from java POJO class using Jackson API in generic way and same can be used to convert from Map to String also.
https://www.thetechnojournals.com/2019/10/how-to-convert-java-object-to-map-or.html

Java JSON object insertion order maintenance

I am using a Java JSON object to store some data. But when I printed it, I found that it stores the data randomly. For example, I stored data like this:
obj.put("key1","val1");
obj.put("key2","val2");
And when I printed it:
{"key2":"val2","key1":"val1"}'
I googled it and found that JSON objects are unordered sets of key value pair. So it doesn't store the order of data.
I need some help in storing data in a JSON object with their order.
Arrays are ordered so use an array of key-value objects [ {key1: val1}, {key2: val2} ]

Java reevaluate string as hashmap

I have a socket server and Im sending information from client to server and back.
Sock servers will only send strings or byte arrays of information.
The information I need to transfer is java hashmap, but to send it, I convert it to a string.
activeUsers = new HashMap<Object, Object>();
socket.send(activeUsers.toString());
upon receiving this this string, I need to reconstruct it as a hashmap.
I have seen examples like this, however, I think this would not work for my case.
For instance:
an item in the hashmap can be while the next item is <string, hashmap> and the items in that hashmap could be a combinations of the two.
I'm used to python where I can say evaluate string as dictionary and it does so.
How can I achieve the same thing in java.
Convert the your HashMap to JSON using the technique given here:
How to convert hashmap to JSON object in Java
Then after transferring the string data, convert it back to HashMap using the Gson or Jackson library for Java.

Java Map class that can contains two or more keys for one value

I'm looking for a Java Map Class that can contains two or more keys for one value. It almost like MultiKeyMap in apache common collections, but it can use only one of the keys to retrieve the value instead of using all of keys.
For example to create an entry in the map for value "Hello World" with keys "key1" and "key2":
map.put("Hello World", "key1", "key2");
Then if I want to get the value, I can use two possible ways:
String value = map.get("key1");
or
String value = map.get("key2");
In MultiKeyMap, you need to specify all of the keys to retrive the value:
String value = map.get("key1", "key2");
UPDATE:
People tell me to use regular Map class but I'm not sure if a map with two keys pointing to a same value will generate two duplicate values or not in memory. So anyone can confirm this?
Why not just repeat the Value for each Key in a regular map?
Hm, it seems to me like you can approximate what you want by doing:
map.put("key1", "Hello World");
map.put("key2", "Hello World");
Then key1 and key2 will both return "Hello World".
Of course, what this won't do is consolidate logically duplicate values down to a single reference. But would you even want to do that? It seems like such a thing could lead to confusing side-effects down the road, if you are placing any sort of mutable types in the map.

Categories