我正在使用OpenJDK 1.8编写Maven应用程序并使用TestNG运行测试.
当我从命令行运行Maven时一切正常,但是当我尝试在IntelliJ中运行测试时,make进程显示以下错误:
java: javacTask: source release 8 requires target release 1.8
Run Code Online (Sandbox Code Playgroud)
我的项目设置指向1.8 JDK和Project Language Level 8.
在Maven内部我有以下块(我猜测它还没有被调用,因为它似乎是造成问题的原因)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我甚至将Maven Runner JRE配置为指向1.8 JDK.
我似乎无法让IntelliJ 12.0.4正确运行测试
我错过了什么吗?
看看下面的课程我做了:
public class FibonacciSupplier implements Iterator<Integer> {
private final IntPredicate hasNextPredicate;
private int beforePrevious = 0;
private int previous = 1;
private FibonacciSupplier(final IntPredicate hasNextPredicate) {
this.hasNextPredicate = hasNextPredicate;
}
@Override
public boolean hasNext() {
return hasNextPredicate.test(previous);
}
@Override
public Integer next() {
int result = beforePrevious + previous;
beforePrevious = previous;
previous = result;
return result;
}
public static FibonacciSupplier infinite() {
return new FibonacciSupplier(i -> true);
}
public static FibonacciSupplier finite(final IntPredicate predicate) {
return new FibonacciSupplier(predicate);
} …Run Code Online (Sandbox Code Playgroud) 我是一个ETL进程,我从Spring Data Repository中检索了很多实体.然后我使用并行流将实体映射到不同的实体.我可以使用使用者将这些新实体逐个存储在另一个存储库中,或者将它们收集到List中并将其存储在单个批量操作中.第一种是昂贵的,而后者可能超过可用的内存.
有没有一种很好的方法来收集流中的一定数量的元素(如限制),消耗该块,并继续并行处理直到所有元素都被处理?
在Java的API 8说:
在执行管道的终端操作之前,不会开始遍历管道源.
那么为什么以下代码抛出:
java.lang.IllegalStateException:stream已经被操作或关闭
Stream<Integer> stream = Stream.of(1,2,3);
stream.filter( x-> x>1 );
stream.filter( x-> x>2 ).forEach(System.out::print);
Run Code Online (Sandbox Code Playgroud)
根据API的第一个过滤操作不应该在Stream上操作.
我已经使用lambda表达式对集合实现了排序以进行比较.我必须检查空值,所以我想出了比较器的这个解决方案
(a,b)->(
(a.getStartDate() == null)
? ( (b.getStartDate() == null) ? 0 : -1)
: ( (b.getStartDate() == null)?1:a.getStartDate().compareTo(b.getStartDate()) )
);
Run Code Online (Sandbox Code Playgroud)
我已经检查了一些问题,比如这个,但它们都是指lambda前代码.
java lambda表达式让我有机会避免两个'if'语句吗?我能以更干净的方式执行任务吗?
Scala foldLeft在Java 8中的优势是什么?
我很想想它reduce,但是减少必须返回与它减少的相同类型的东西.
例:
import java.util.List;
public class Foo {
// this method works pretty well
public int sum(List<Integer> numbers) {
return numbers.stream()
.reduce(0, (acc, n) -> (acc + n));
}
// this method makes the file not compile
public String concatenate(List<Character> chars) {
return chars.stream()
.reduce(new StringBuilder(""), (acc, c) -> acc.append(c)).toString();
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码中的问题是accumulator:new StringBuilder("")
因此,任何人都可以指出我正确的foldLeft/修复我的代码?
我们刚刚在Amazon Linux上升级到Java 8.我们正在使用Spring 4.3.8.RELEASE.过去我们可以通过在应用程序上下文文件中设置bean来获取我们的机器主机名,就像这样...
<bean id="localhostInetAddress" class="java.net.InetAddress" factory-method="getLocalHost" />
<bean id="hostname" factory-bean="localhostInetAddress" factory-method="getHostName" />
Run Code Online (Sandbox Code Playgroud)
但是对于Java 8,bean"hostname"现在包含字符串
localhost
Run Code Online (Sandbox Code Playgroud)
在Java 8之前,它曾用于包含在命令行上运行的"hostname"值,即
[myuser@machine1 ~]$ hostname
machine1.mydomain.org
Run Code Online (Sandbox Code Playgroud)
如何重新配置我们的bean,以便它获取命令行列出的主机名?我不想在任何地方硬编码.
起初我认为Java Streams必然与I/O相关,但它们看起来非常像.Net中的IEnumerable接口.
比较公平吗?
我有一个地图,其中值是字符串,键是列表:Map<String, List<BoMLine>> materials.我想用它的值过滤这个地图; 这样的事情:
materials.entrySet().stream()
.filter(a->a.getValue().stream()
.filter(l->MaterialDao.findMaterialByName(l.getMaterial()).ispresent)
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用.有人有想法吗?
谢谢.