@Autowired
private StringRedisTemplate stringRedisTemplate;
public void test(String key) {
// IDEA prompts an error
Map<String, String> entries1 = stringRedisTemplate.opsForHash().entries(key);
// This is OK.
HashOperations<String, String, String> opsForHash = stringRedisTemplate.opsForHash();
Map<String, String> entries = opsForHash.entries(key);
}
Asked
Active
Viewed 123 times
0
-
1What error was shown? Can you please post the error message? – deHaar Sep 29 '20 at 11:01
-
1Required type: Map
Provided: Map – Gray Sep 29 '20 at 11:02 -
2@张善庆 That should be edited into your question. – Polygnome Sep 29 '20 at 11:03
-
That means `stringRedisTemplate.opsForHash().entries(key)` returns a `Map – deHaar Sep 29 '20 at 11:03
-
What type is `stringRedisTemplate.opsForHash()`? – Polygnome Sep 29 '20 at 11:03
-
@deHaar But why `stringRedisTemplate.opsForHash();` retrun a `HashOperations
`, but I am initialized opsForHash as a `HashOperations – Gray Sep 29 '20 at 11:12` is OK -
Similar: [comparing and thenComparing gives compile error](https://stackoverflow.com/questions/40500280/comparing-and-thencomparing-gives-compile-error) – Ole V.V. Sep 29 '20 at 11:22
1 Answers
4
The problem Is that the method opsForHash()
uses 2 generics, This is the signature:
public <HK, HV> HashOperations<K, HK, HV> opsForHash()
If you want to use a single line, you need to set the generics, just like:
Map<String, String> entries1 = stringRedisTemplate.<String, String>opsForHash().entries(key);
In your code, the second approach works because the compiler finds out the generics from the defined variable on the left side of the =
operator.

Roberto
- 8,586
- 3
- 42
- 53