我正在使用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正确运行测试
我错过了什么吗?
功能接口的定义是"功能接口是一个只有一个抽象方法的接口(除了Object的方法),因此代表一个单一的功能契约."
根据这个定义,Comparable<T>它绝对是一个功能界面.
lambda表达式的定义是"lambda表达式就像一个方法:它提供了一个形式参数列表和一个正文 - 一个表达式或块 - 用这些参数表示."
lambda表达式的评估产生功能接口的实例.
因此,lambda表达式的目的是通过实现功能接口的单个功能来创建功能接口的实例.即.允许使用单个函数创建实例.
让我们来看看Comparable<T>,这个界面是否设计用作单一功能?即.它是否仅用于创建具有此单一功能的实例?
Comparable<T>以"This接口开头的文档对每个实现它的类的对象施加了一个总排序.这个排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方法."
上面的句子清楚地表明,Comparable<T>它不是设计用作单个函数,而是总是由一个类实现,它通过添加这个单个函数对其实例具有自然顺序.
这意味着它不是设计为使用lambda表达式创建的?
关键是我们不会有任何仅仅是可比较的对象,它意味着要实现,因此用作类的附加功能.
那么,Java语言中是否有一种方法Comparable<T>可以防止创建lambda表达式?接口的设计者是否可以决定该接口是否由类实现,而不是通过使用lambda表达式使用此单一方法创建为实例?
仅仅因为接口恰好有一个抽象方法,它不应该被视为一个功能接口.
也许,如果Java提供像NotFunctional这样的注释,编译器可以检查该接口是否不用于创建lambda表达式,例如.
@NotFunctional
public interface Comparable<T> { public int compareTo(T t); }
Run Code Online (Sandbox Code Playgroud) 我们最近将我们的消息处理应用程序从Java 7升级到Java 8.自升级以来,我们偶尔会遇到一个流在读取时被关闭的异常.记录显示终结器线程正在调用finalize()保存流的对象(进而关闭流).
代码的基本概要如下:
MIMEWriter writer = new MIMEWriter( out );
in = new InflaterInputStream( databaseBlobInputStream );
MIMEBodyPart attachmentPart = new MIMEBodyPart( in );
writer.writePart( attachmentPart );
Run Code Online (Sandbox Code Playgroud)
MIMEWriter并且MIMEBodyPart是本土MIME/HTTP库的一部分. MIMEBodyPart扩展HTTPMessage,具有以下内容:
public void close() throws IOException
{
if ( m_stream != null )
{
m_stream.close();
}
}
protected void finalize()
{
try
{
close();
}
catch ( final Exception ignored ) { }
}
Run Code Online (Sandbox Code Playgroud)
异常发生在调用链中MIMEWriter.writePart,如下所示:
MIMEWriter.writePart() 写入部件的标题,然后调用 part.writeBodyPartContent( this )MIMEBodyPart.writeBodyPartContent()调用我们的实用工具方法 …免责声明:这不是关于这种情况(虽然错误听起来一样):class从类型java.util.Set和java.util.List继承spliterator()的无关默认值
这就是为什么:
考虑两个接口(在包" a"中)
interface I1 {
default void x() {}
}
interface I2 {
default void x() {}
}
Run Code Online (Sandbox Code Playgroud)
我绝对清楚为什么我们不能宣布这样的类:
abstract class Bad12 implements I1, I2 {
}
Run Code Online (Sandbox Code Playgroud)
(!)但我无法理解这种限制,参考类型变量:
class A<T extends I1&I2> {
List<T> makeList() {
return new ArrayList<>();
}
}
Run Code Online (Sandbox Code Playgroud)
有错误:class java.lang.Object&a.I1&a.I2 inherits unrelated defaults for x() from types a.I1 and a.I2.
为什么我不能定义这样的类型变量?java在这种情况下,为什么要关心不相关的默认值?什么类型的变量可以"打破"?
更新:只是为了澄清.我可以创建几个类的表单:
class A1 implements I1, I2 {
public void x() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将String转换为Instant.你能帮我吗?
我得到以下异常:
引起:java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:java.time.Instant.from上的java.time.format.Parsed.getLong(Parsed.java:203)中的InstantSeconds(Instant.java:373)
我的代码基本上看起来像这样
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = "2016-02-16 11:00:02";
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
Instant result = Instant.from(temporalAccessor);
Run Code Online (Sandbox Code Playgroud)
我使用的是Java 8 Update 72.
在开发maven插件时,构建打印错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.3:descriptor (default-descriptor) on project default-method-demo: Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.3:descriptor failed: syntax error @[8,1] in file:/full/path/to/project/default-method/src/main/java/org/example/Iface.java -> [Help 1]
Run Code Online (Sandbox Code Playgroud)
即使该文件Iface.java是可编译的.
Iface.java:
package org.example;
public interface Iface {
default String getString() {
return "string";
}
}
Run Code Online (Sandbox Code Playgroud)
从 pom.xml
<packaging>maven-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
是什么原因造成的?怎么修好?
我正在学习Java 8中的Streams.我对这个概念感到困惑:
集合是内存中的数据结构,它保存数据结构当前具有的所有值 - 集合中的每个元素必须先计算才能添加到集合中.相反,流是概念上固定的数据结构,其中元素是按需计算的.
我不明白.Collection如何才能保存在添加到集合之前必须已计算的值?而且,Stream与固定数据结构的比较是什么意思?
在Java 8中,@FunctionalInterface引入了注释以表示任何具有一个抽象方法作为功能接口的接口.引入它的原因之一是指示用户(程序员),lambda表达式可以在功能接口的上下文中使用.
该Comparator接口都被注解@FunctionalInterface.但是,两种方法都是抽象的.
int compare(T o1, T o2);
Run Code Online (Sandbox Code Playgroud)
和
boolean equals(Object obj);
Run Code Online (Sandbox Code Playgroud)
在文档中FunctionalInterface,它被明确提及为
从概念上讲,功能界面只有一种抽象方法.
这个equals方法不是被认为是抽象的吗?
我想创建一个Map从List的PointS和有地图从里面用相同的parentId,如映射列表中的所有条目Map<Long, List<Point>>.
我用过Collectors.toMap()但不编译:
Map<Long, List<Point>> pointByParentId = chargePoints.stream()
.collect(Collectors.toMap(Point::getParentId, c -> c));
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
StringBuilder str = new StringBuilder("foo");
for(Field f : fields){
str.append("|" + f);
}
str.append("|" + bar);
String result = str.toString();
Run Code Online (Sandbox Code Playgroud)
我知道编译器会优化字符串连接"|" + f并用StringBuilder替换它.但是,是否会创建一个新的StringBuilder,或者现有的str将在Java 8中使用?Java 9怎么样?
java-8 ×10
java ×8
collections ×1
collectors ×1
comparator ×1
finalize ×1
finalizer ×1
interface ×1
java-9 ×1
java-stream ×1
lambda ×1
maven ×1
maven-plugin ×1
parsing ×1
performance ×1
timestamp ×1