Maven can';t使用流操作链编译java
正如标题所说,当我试图编译一段包含一系列流操作的代码时,我遇到了一个问题 这在IDE中不会发生(使用eclipse和IntelliJ进行了尝试),但当我在控制台中尝试“mvn clean install”时,它会抛出以下错误:Maven can';t使用流操作链编译java,java,eclipse,maven,intellij-idea,java-stream,Java,Eclipse,Maven,Intellij Idea,Java Stream,正如标题所说,当我试图编译一段包含一系列流操作的代码时,我遇到了一个问题 这在IDE中不会发生(使用eclipse和IntelliJ进行了尝试),但当我在控制台中尝试“mvn clean install”时,它会抛出以下错误: [ERROR] .collect(Collectors.joining("&")); [ERROR] ^^^^^^^ [ERROR] The method collect(Collector<? super Object,A,R>) in the t
[ERROR] .collect(Collectors.joining("&"));
[ERROR] ^^^^^^^
[ERROR] The method collect(Collector<? super Object,A,R>) in the type Stream<Object> is not applicable for the arguments (Collector<CharSequence,capture#1-of ?,String>)
[ERROR]
[ERROR] Found 1 error and 0 warnings.
[ERROR] -> [Help 1]
[ERROR].collect(收集器.加入(&));
[错误]^^^^^^^
[错误]方法collect(Collector在调用mvn之前尝试导出JAVA\u HOME。看来,您的默认设置是java7。我无法用JDK 1.8.0\u 74(使用Maven 3.3.9)重现它。第一个代码段编译得很好。在我们真正帮助解决maven问题之前,您需要发布pom中的构建插件设置。@Tunaki有一个好主意,在javac
补丁中解决了许多流问题。Eclipse将使用与maven不同的编译器。请尝试最新版本的Java,这在我遇到类似问题。也无法使用JDK 1.8.051重新编程。是否可以发布pom.xml
?您可以运行mvn帮助:effective pom
来展开所有变量、托管插件和依赖项以及配置文件,以查看最终的设置。检查输出中的编译器插件设置,验证您是否使用了编译器你以为你是。
package com.stackoverflow.test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamMavenTest {
public static void main(String[] args) {
List<Department> depts = initDepts();
String unpaidReferences = depts.stream()
.flatMap(dpto -> dpto.people.stream()
.filter(ppl -> ppl.lastname != null && ppl.name != null)
.map(ppl -> ppl.name + "|" + ppl.lastname))
.collect(Collectors.joining("&"));
System.out.println(unpaidReferences);
}
private static List<Department> initDepts() {
List<Department> depts = new ArrayList<Department>();
Department dep1 = new Department();
dep1.people = new ArrayList<Person>();
dep1.people.add(new Person("Baudouin","Quy"));
dep1.people.add(new Person("Sigmund","Zebedaios"));
dep1.people.add(new Person("Bernd","Edwyn"));
depts.add(dep1);
Department dep2 = new Department();
dep2.people = new ArrayList<Person>();
dep2.people.add(new Person("Olanrewaju","Jorgen"));
dep2.people.add(new Person("Stanislovas","Adalberto"));
dep2.people.add(new Person("Manuel","'Avshalom"));
depts.add(dep2);
return depts;
}
public static class Department{
List<Person> people;
}
public static class Person{
String name;
String lastname;
public Person(String name, String lastname){
this.name = name;
this.lastname = lastname;
}
}
}
public static void main(String[] args) {
List<Department> depts = initDepts();
Stream<String> flatMap = depts.stream()
.flatMap(dpto -> dpto.people.stream()
.filter(ppl -> ppl.lastname != null && ppl.name != null)
.map(ppl -> ppl.name + "|" + ppl.lastname));
String unpaidReferences = flatMap
.collect(Collectors.joining("&"));
System.out.println(unpaidReferences);
}