Reputation: 379
Here Below, I'm having one list of HashMap's and I wanna store all of these map's in single key of redis but I'm not getting any method to store all these maps in single key. Please help me in this problem.
Jedis jedis = new Jedis("localhost");
List <HashMap<String, String>> listOfMaps = new ArrayList<HashMap<String, String>>();
listOfMaps.add(new HashMap<String,String>);
listOfMaps.add(new HashMap<String,String>);
listOfMaps.add(new HashMap<String,String>);
listOfMaps.add(new HashMap<String,String>);
.
.
.
and so on lets take upto 10 values
Now, I wanna store these maps in a key like this:
for(int i=0;i<listOfMaps.size();i++){
jedis.hmset("mykey",listofMaps[i]);
}
But in his case hmset overwrites all older values to write new values. Please tell me any alternative to store all these map values in single key mykey.
Upvotes: 3
Views: 6250
Reputation: 243
You can use something like below
public static void main(String[] args) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> valSetOne = new ArrayList<String>();
valSetOne.add("ABC");
valSetOne.add("BCD");
valSetOne.add("DEF");
List<String> valSetTwo = new ArrayList<String>();
valSetTwo.add("CBA");
valSetTwo.add("DCB");
map.put("FirstKey", valSetOne);
map.put("SecondKey", valSetTwo);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
System.out.println("Value of " + key + " is " + values);
}
}
Upvotes: 0
Reputation: 10793
You can use Multimap
object provided by Redisson framework. It allows to store multiple values per map key as list or set. Here is the example:
RMultimap<String, Integer> multimap = redisson.getListMultimap("myMultimap");
for (int i = 0; i < 10; i++) {
myMultimap.put("someKey", i);
}
// returns Redis list object
RList list = myMultimap.get("someKey");
Upvotes: 2