该java.util.Objects类被延长了一些新的方法
分别
对象#requireNonNullElseGet() in Java-9.
如果第一个参数为非null,则它们将返回第一个参数,否则返回非null的第二个参数或者supplier.get()的非null值
jshell> String nullStr = null;
nullStr ==> null
jshell> Objects.requireNonNullElse(nullStr,"lorem ipsum");
$13 ==> "lorem ipsum"
jshell> Objects.requireNonNullElseGet(nullStr,() -> "lorem ipsum");
$14 ==> "lorem ipsum"
Run Code Online (Sandbox Code Playgroud)
但是新功能与Optional#orElse和Optional#orElseGetOptional类中已存在的功能重叠
jshell> Optional.ofNullable(nullStr).orElse("lorem ipsum");
$17 ==> "lorem ipsum"
jshell> Optional.ofNullable(nullStr).orElseGet(() -> "lorem ipsum");
$18 ==> "lorem ipsum"
Run Code Online (Sandbox Code Playgroud)
新方法Objects和相应Optional方法之间的唯一区别是供应商的第二个参数或值必须为非null否则Objects抛出NPE:
jshell> Objects.requireNonNullElseGet(nullStr,() -> null);
| java.lang.NullPointerException thrown: supplier.get()
| at Objects.requireNonNull (Objects.java:246)
| at Objects.requireNonNullElseGet …Run Code Online (Sandbox Code Playgroud) org.apache.logging.log4j.core.LoggerContext logContext =
(org.apache.logging.log4j.core.LoggerContext)
LogManager.getContext(false);
Map<String, LoggerConfig> map = logContext.getConfiguration().getLoggers();
Run Code Online (Sandbox Code Playgroud)
我只得到根记录器,但我需要得到所有记录器,我想在运行时更改记录器级别。
请指教。
虽然使用依赖插件的公认答案是当时最好的解决方案,但@ltlBeBoy 的答案利用了自添加到自由行家插件以来的“copyDependencies”支持。使用 'copyDependencies' 通常是一个更好的解决方案,因为它被集成到“开发模式”循环中并且不那么冗长(以支持比依赖插件更少的选项为代价)。
我需要复制derby.jar到 Open Liberty 共享目录中
${project.build.directory}/liberty/wlp/usr/shared/resources/。我在 pom.xml 文件中有以下设置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-derby-dependency</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>derby</includeArtifactIds>
<outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
以及配置开放自由的部分
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>${openliberty.maven.version}</version>
<executions>
<execution>
<id>package-server</id>
<phase>package</phase>
<goals>
<goal>create-server</goal>
<goal>install-apps</goal>
<goal>package-server</goal>
</goals>
<configuration>
<outputDirectory>target/wlp-package</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<assemblyArtifact>
<groupId>io.openliberty</groupId>
<artifactId>openliberty-runtime</artifactId>
<version>${openliberty.version}</version>
<type>zip</type>
</assemblyArtifact>
<configFile>src/main/liberty/config/server.xml</configFile>
<appArchive>${project.build.directory}/${final.name}.war</appArchive>
<packageFile>${project.build.directory}/${final.name}.jar</packageFile>
<include>runnable</include>
<serverName>${final.name}</serverName>
<installAppPackages>project</installAppPackages>
<configDirectory>${project.basedir}/src/main/liberty/server</configDirectory>
<bootstrapProperties>
<project.name>${final.name}</project.name>
<jwt.issuer>https://server.example.com</jwt.issuer>
</bootstrapProperties>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
有了这个设置,我必须执行mvn …
我有一个包含两个成员的类,以及该类的对象列表。现在,我想从对象列表中提取成员列表。
例如:
class student {
int Id;
String studentName;
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要从学生列表中检索一个StudentName列表。java8 Streams如何做到这一点?
不使用流的解决方案:
List<student> studentList;
List<String> nameList = new ArrayList<String>();
Iterator iterator = studentList.iterator();
while(iterator.hasNext()){
nameList.add(iterator.next().getStudentName());
}
Run Code Online (Sandbox Code Playgroud) java ×2
derby ×1
java-9 ×1
java-stream ×1
list ×1
log4j ×1
log4j2 ×1
logging ×1
maven ×1
microprofile ×1
object ×1
open-liberty ×1
optional ×1