简体   繁体   中英

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). I changed the version of Java in my pom.xml to 1.7, and the following lines no longer work:

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. 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?

You can use a previous version of Guava library:

Guava comes in two flavors.

The JRE flavor requires JDK 1.8 or higher. If you need support for JDK 1.7 or Android, use the Android flavor. You can find the Android Guava source in the android directory.

or change your code to this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM