我想请求一些代码的解释,我将其作为我在Java课程中进行的练习的解决方案.练习是我有一个List<>具有各种属性(薪水,姓名,姓氏,电子邮件......)的员工,我正在尝试检索收入最高的员工并打印他们的姓名和薪水.
我能够检索最高薪水而不是员工姓名,如下所示:
Integer maxSalary;
maxSalary = roster
.stream()
.map(Employee :: getSalary)
.collect(Collectors.reducing(Integer :: max)
.get();
Run Code Online (Sandbox Code Playgroud)
然后给了我这个小块的代码,它完全正常,但我不太清楚为什么它的工作原理:
Integer maxSalary;
Employee emp2 = roster
.stream()
.max((p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary()))
.get();
System.out.println("The employee who earns the most is :" + emp2.getName + " and earns : " + emp2.getSalary + " a month.");
Run Code Online (Sandbox Code Playgroud)
我明白这是一个Lambda表达式使用.max我似乎无法理解为什么以及如何工作?
我制作了一个小型应用程序,它从 Excel(xls 文件)中读取内容并将内容显示到 JTable 中。Eclipse 中一切正常,但是当我创建 jar 文件并尝试运行它时,出现以下问题:
java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
Run Code Online (Sandbox Code Playgroud)
我发现奇怪的事情是,问题出在行上,当工作簿和工作表在行之前被调用时,不会产生任何麻烦(至少从我所看到的)。
我进行了很多研究,主要似乎是 jar 文件不在类路径中,但打开 jar 和清单文件,我可以看到所有 jar 都存在。
Class-Path: poi-ooxml-4.0.1.jar poi-4.0.1.jar commons-codec-1.11.jar commons-collections4-4.2.jar commons-math3-3.6.1.jar commons-compress-1.18.jar curvesapi-1.05.jar poi-ooxml-schemas-4.0.1.jar xmlbeans-3.0.2.jar
Run Code Online (Sandbox Code Playgroud)
这是我的 pom.xml 文件中的内容:
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix>
<mainClass>com.clientdb.classes.DynamicRegForm</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我还尝试下载 jar 文件并将它们添加到项目中,而不是将依赖项添加到 pom 文件中,但仍然出现相同的错误。有任何想法吗?