番石榴缓存泛型错误 [英] Guava cache generics error

查看:96
本文介绍了番石榴缓存泛型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建番石榴缓存时出现魔术错误:

Magical error appears when trying to create guava cache:

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;

import java.util.concurrent.ConcurrentMap;

public class Main {

    private static ConcurrentMap<Long, Object> cache = CacheBuilder
            .newBuilder()
            .build(new CacheLoader<Long, Object>() {
                @Override
                public Object load(Long key) throws Exception {
                    return null;
                }
            }).asMap();

}

java编译错误:

Error:(17, 21) java: C:\JavaWorkspace\untitled\src\...\Main.java:17: incompatible types
found   : java.util.concurrent.ConcurrentMap<java.lang.Object,java.lang.Object>
required: java.util.concurrent.ConcurrentMap<java.lang.Long,java.lang.Object>

番石榴版本为20.0 Java版本是1.6

guava version is 20.0 java version is 1.6

同时使用Java 1.8和番石榴23.0-可以!

while using java 1.8 and guava 23.0 - it is OK!

问题是我只需要使用1.6 Java

The problem is I have to use only 1.6 java

推荐答案

由于在JDK 6上,您会遇到类型不匹配的情况:

On JDK 6 you get type mismatch:

found   : ConcurrentMap<Object, Object>
required: ConcurrentMap<Long, Object>

因为cache的类型不能在没有类型提示的情况下进行推断.

because cache's type cannot be inferred without a type hint.

这篇关于番石榴缓存泛型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆