作为List.of(...)或Collections.unmodifiableList()的一个特例- 指向空的和不可变列表的首选Java 9方式是什么?
继续写
Collections.emptyList();
Run Code Online (Sandbox Code Playgroud)
或切换到
List.of();
Run Code Online (Sandbox Code Playgroud) MethodType methodType = MethodType.methodType(void.class, ByteBuffer.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(type, methodType);
Function<ByteBuffer, Object> = handle; // ???
Run Code Online (Sandbox Code Playgroud)
是否有可能让最后的作业有效?反转方式不起作用:是否可以将方法引用转换为MethodHandle?
这是另一个可复制的例子:
new Integer("123");
MethodType methodType = MethodType.methodType(void.class, String.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(Integer.class, methodType);
Function<String, Integer> function1 = Integer::new;
Function<String, Integer> function2 = handle.toLambda(); // ???
Run Code Online (Sandbox Code Playgroud) 在List.of()或Collections.emptyList()和List.of(...)或Collections.unmodifiableList()中给出的注释和答案的上下文中, 我提出了两条以下经验法则(也适用于Set和Map工厂相应).
继续使用Collections.emptyList()以提高可读性,例如初始化懒惰的字段成员,例如:
class Bean {
private List<Bean> beans = Collection.emptyList();
public List<Bean> getBeans() {
if (beans == Collections.EMPTY_LIST) { beans = new ArrayList<>(); }
return beans;
}
}
Run Code Online (Sandbox Code Playgroud)
在使用参数List.of()调用可执行文件时,使用新工厂和变体作为快速且较少类型的版本List.以下是我目前的替代模式:
Collections.emptyList() --> List.of()
Collections.singletonList(a) --> List.of(a)
Arrays.asList(a, ..., z) --> List.of(a, ..., z)
Run Code Online (Sandbox Code Playgroud)
在虚构的用法中Collections.indexOfSubList,以下几行
Collections.indexOfSubList(Arrays.asList(1, 2, 3), Collections.emptyList());
Collections.indexOfSubList(Arrays.asList(1, 2, 3), Collections.singletonList(1));
Collections.indexOfSubList(Arrays.asList(1, 2, 3), Arrays.asList(1));
Collections.indexOfSubList(Arrays.asList(1, 2, 3), Arrays.asList(2, 3));
Collections.indexOfSubList(Arrays.asList(1, …Run Code Online (Sandbox Code Playgroud) 如果你有一个List<String> strings实例,你会继续写:
Collections.unmodifiableList(strings)
Run Code Online (Sandbox Code Playgroud)
或切换到:
List.of(strings.toArray(new String[strings.size()]))
Run Code Online (Sandbox Code Playgroud)
实例的性能(内存和运行时)的初始影响是什么?List.of变体中是否有运行时优势?
无论项目属性设置为什么,Google Plugin for Eclipse都会在web.xml中自动生成这些行:
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
有没有办法阻止插件这样做?
如何使用非零错误代码/exit进行jshell会话?
/exityield:进程以退出代码0结束/exit 1yield:进程以退出代码0结束throw new Error("1")yield:java.lang.Error thrown:1 at(#24:1)`和Process以退出代码0结束System.exit(1)产量:状态引擎终止.使用以下命令恢复定义:/ reload -restore ...并且jshell会话不会终止.类似的bash命令set -e不可用.
如在如何忽略系统默认区域设置以检索resourceBundle中所述,您可以在Java 8或更早版本中配置为不通过以下方式回退到默认区域设置:
ResourceBundle.getBundle("MyResources",
new Locale("en", "US"),
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES))
Run Code Online (Sandbox Code Playgroud)
在Java 9 的ResourceBundle.Control使用抛出一个UnsupportedOperationException名为模块使用时:ResourceBundle.Control是不是在一个名为模块的支持.
如何实现/配置自定义"MyResourcesProvider [Impl]"以实现与ResourceBundle.Control.getNoFallbackControl提供的相同的行为?
我想从命令行运行一个包含JUnit 5测试的类.不幸的是,我有一些外部依赖项阻止我使用Maven,Gradle或其他构建系统.
在JUnit 4中,我可以完成这个
java .:"lib/*" org.junit.runner.JUnitCore TestClass
Run Code Online (Sandbox Code Playgroud)
JUnit 5有相同的功能吗?我只想知道测试是否成功与IntelliJ中的运行时间相似.
TestClass.java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.*;
public class TestClass {
private static ArrayList<Student> students;
private static ArrayList<Student> inAgeOrderStudents;
private static ArrayList<Student> inNameOrderStudents;
@BeforeAll
static void setUp(){
initializeStudents();
initSortedAgeStudents();
initSortedNameStudents();
}
@BeforeEach
void reloadStudents() {
Collections.shuffle(students);
}
static void initializeStudents(){
students = new ArrayList<Student>();
students.add(new Student(18, "Tim"));
students.add(new Student(18, "Tim"));
students.add(new Student(16, "Jean"));
students.add(new Student(14, "Lin"));
students.add(new Student(19, "Sam"));
}
static …Run Code Online (Sandbox Code Playgroud) 我知道我们可以在 JUnit 4 中使用@Rule,TestName但是我正在使用 JUnit 5(Jupiter)并且正在努力寻找一种方法来打印方法中的测试方法(待执行)名称@BeforeEach。
如何转换以下代码块:
List<ExecutableElement> methods = ...
List<ExecutableElement> hiddens = new ArrayList<>();
for (ExecutableElement hider : methods) {
for (ExecutableElement hidden : methods) {
if (elements.hides(hider, hidden) || elements.overrides(hider, hidden, type)) {
hiddens.add(hidden);
}
}
}
methods.removeAll(hiddens);
Run Code Online (Sandbox Code Playgroud)
变成这样的东西:
methods.removeAll(methods.stream().filter(... ...().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) Gradle 4.6增加了对JUnit5的支持.
这对我有用,只要我没有其他源集,例如集成测试:我不知道如何useJUnitPlatform()在我的集成测试中启用.
我能做的是让test任务使用新的JUnit5支持,但我的testInt任务是使用JUnit5控制台并运行测试,因为它将从命令行运行.最后,我放弃了对gradle和回滚的JUnit5支持,以便在两个测试中使用JUnit5控制台.
如何在其他任务上启用Gradle 4.6 JUnit5支持test?
java ×7
java-9 ×5
collections ×4
junit5 ×3
java-8 ×2
lambda ×2
command-line ×1
gradle ×1
java-10 ×1
java-stream ×1
jshell ×1
reflection ×1
unit-testing ×1