简体   繁体   English

降级到Java 1.7会中断com.google.common.collect的使用

[英]downgrading to Java 1.7 breaks com.google.common.collect usage

I have a program which was working fine in Java 1.8 (it was actually originally written for 1.6). 我有一个在Java 1.8中运行良好的程序(实际上最初是为1.6编写的)。 I changed the version of Java in my pom.xml to 1.7, and the following lines no longer work: 我将pom.xml中的Java版本更改为1.7,以下行不再起作用:

import com.google.common.collect.Lists;

List<WekaFeature<Document>> myFeats = null;
if (abc == 123) {
    myFeats = Lists.newArrayList(new WekaFeature[]{new AFeature(), new BFeature(), new CFeature()});
}

The error is: 错误是:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project myproject: Compilation failure: Compilation failure:
[ERROR] /tmp/build_7d4663fc58427c700939b5e36e6e7e1d/src/main/java/com/example/MyService.java:[252,49] incompatible types
[ERROR]   required: java.util.List<my.tools.feature.core.WekaFeature<my.tools.document.core.Document>>
[ERROR]   found:    java.util.ArrayList<my.tools.feature.core.WekaFeature>

I'm new to Java and I don't have any familiarity with the com.google.common.collect.Lists library. 我是Java的新手,并且对com.google.common.collect.Lists库不熟悉。 I know that List is an interface and ArrayList is an implementation, so why doesn't it work now and why would it work in Java 1.8? 我知道List是一个接口,而ArrayList是一个实现,那么为什么它现在不起作用以及为什么在Java 1.8中起作用?

You can use a previous version of Guava library: 您可以使用旧版本的Guava库:

Guava comes in two flavors. 番石榴有两种口味。

The JRE flavor requires JDK 1.8 or higher. JRE版本需要JDK 1.8或更高版本。 If you need support for JDK 1.7 or Android, use the Android flavor. 如果您需要对JDK 1.7或Android的支持,请使用Android风格。 You can find the Android Guava source in the android directory. 您可以在android目录中找到Android Guava源代码。

or change your code to this one: 或将您的代码更改为此代码:

myFeats = new ArrayList<>();
myFeats.add(new AFeature());
myFeats.add(new BFeature());
myFeats.add(new CFeature());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle'错误:包com.google.common.collect不存在' - Gradle 'error: package com.google.common.collect does not exist' Selenium java 错误 com/google/common/collect/ImmutableMap 在 ubuntu - Selenium java error com/google/common/collect/ImmutableMap in ubuntu Java:合并两个com.google.common.collect.Table - Java: Merge two com.google.common.collect.Table Google通讯录-java.lang.NoSuchMethodError:com.google.common.collect.ImmutableSet.of([Ljava / lang / Object;)Lcom / google / common / collect / ImmutableSet; - Google Contacts - java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet; 卡夫卡连接 java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator; - Kafka Connect java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator; com.google.common.collect.MapMaker中的过期时间 - expiration in com.google.common.collect.MapMaker NoClassDefFoundError com.google.common.collect.Lists - NoClassDefFoundError com.google.common.collect.Lists java.lang.NoSuchMethodError:带有Selenium和Java的com.google.common.collect.ImmutableSet.of([Ljava / lang / Object;) - java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of([Ljava/lang/Object;) with Selenium and Java java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap 在 Java 中通过 Selenium 使用 GeckoDriver Firefox 时出错 - java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Java "java.lang.NoClassDefFoundError: com\/google\/common\/collect\/ImmutableMap 在 Java Selenium 中使用带有 Maven 依赖关系的 WebDriver" - java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap while using WebDriver with Maven Dependencies in Java Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM