4

I am trying to pass a list of keys to jedis and get their values in return.
mget operation does this but it does not return key value pair it gives all values as a list.
Is there any way we can know the key value pair in this query. OR it is confirmed the values returned in list are in same order as the keys.

List<String> lt =jedis.mget(mapArray);
            int j = 0;
            for( String key : mapArray) {
                System.out.println(key+" : "+lt.get(j));
                j++;
            }

Thanks

viren
  • 1,073
  • 1
  • 17
  • 41

1 Answers1

8

There is no way to return the key:value pairs list, since Redis MGET command just returns a list of values.

But it is confirmed that the values are returned in the same order as you specified the list of keys, so you know that the first element returned in the list is the value for the first element you passed in the list, same for the second and so on.

You can check it not only in Redis MGET doc here:

https://redis.io/commands/mget#examples

but also in Jedis repository by taking a look at mget tests here:

https://github.com/xetorthio/jedis/blob/710ec9c824c6c333809dc7650e6b2084b2c24796/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java#L35

Averias
  • 931
  • 1
  • 11
  • 20
  • Thanks @Averias I saw code implementation and it is confirmed https://github.com/antirez/redis/blob/d680eb6dbdf2d2030cb96edfb089be1e2a775ac1/src/t_string.c#L285-L301 – viren Apr 03 '18 at 03:29