如何先分组,然后使用Java流应用过滤?
示例:考虑此类Employee:我希望按部门分组,其中包含薪水大于2000的员工列表.
public class Employee {
private String department;
private Integer salary;
private String name;
//getter and setter
public Employee(String department, Integer salary, String name) {
this.department = department;
this.salary = salary;
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何做到这一点
List<Employee> list = new ArrayList<>();
list.add(new Employee("A", 5000, "A1"));
list.add(new Employee("B", 1000, "B1"));
list.add(new Employee("C", 6000, "C1"));
list.add(new Employee("C", 7000, "C2"));
Map<String, List<Employee>> collect = list.stream()
.filter(e -> e.getSalary() > 2000)
.collect(Collectors.groupingBy(Employee::getDepartment));
Run Code Online (Sandbox Code Playgroud)
产量
{A=[Employee [department=A, salary=5000, name=A1]],
C=[Employee [department=C, …Run Code Online (Sandbox Code Playgroud) 我试图熟悉Java 9模块以及如何在IntelliJ中定义它们.除此之外,我想使用--patch-module编译器/ JVM标志解决拆分包问题,我不知道如何使它在IntelliJ中工作.
我正在使用IntelliJ IDEA 2017.2.1使用Java HotSpot(TM)64位服务器VM构建#IC 172.3544.35(内置9 + 180,混合模式).
这是我的源文件MyImmutableList.java:
package com.google.common.collect;
public class MyImmutableList extends RegularImmutableList {
public MyImmutableList(Object[] array) {
super(array);
}
}
Run Code Online (Sandbox Code Playgroud)
它属于我的模块com.effjava.collect有module-info.java:
module com.effjava.collect {
// do not require module guava; instead patch this module with guava.19-0.jar via:
// javac --patch-module com.effjava.collect=guava-19.0.jar module-info.java com/google/common/collect/MyImmutableList.java
// requires guava;
exports com.google.common.collect;
}
Run Code Online (Sandbox Code Playgroud)
为了编译我的模块,我--patch-module使用Settings=> Build,Execution,Deplyoment=> Compiler=> 在IntelliJ中指定了标志,Shared build process VM options如此处所述 …
java intellij-idea java-platform-module-system java-9 java-module
我在Maven pom.xml中包含了这些依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我试图在module-info.java中添加这种依赖,如下所示:
module io.github.wildcraft.restclient {
requires httpcore; // no compilation problem
requires httpclient; // no compilation problem
requires commons-io; // shows compilation error
}
Run Code Online (Sandbox Code Playgroud)
对于commons-io,我收到编译错误.我怎样才能做到这一点?
模块与open关键字之前和之后有什么区别?
例如:
open module foo {
}
module foo {
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始看看Java 9模块系统,我想知道一个类是否有可能知道它所在的模块.
为此我创建了以下模块
module de.test {
exports de.test.myexport;
}
Run Code Online (Sandbox Code Playgroud)
并编译了一个看起来像的jar文件
> jar --print-module-descriptor --file=Java9Test-1.0-SNAPSHOT.jar
de.test
requires mandated java.base
exports de.test.myexport
Run Code Online (Sandbox Code Playgroud)
在包中de.test,我有一个叫做Overview调用的类
Module module = Overview.class.getModule();
Run Code Online (Sandbox Code Playgroud)
但是,返回的模块对象是未命名的,没有ModuleDescriptor.
我getModule()在这里使用正确,还是有其他方法加载类的模块?
我在OS X上使用JDK 9 build 120.
鉴于Java 9已经出现在我们身上,我们终于可以拥有一个java REPL了,jshell我希望有一种方法可以将一个shebang添加到脚本中并对其进行jshell解释.
我尝试创建test.jsh:
#!/usr/bin/env jshell -s
System.out.println("Hello World")
/exit
Run Code Online (Sandbox Code Playgroud)
然而,这给了:
? ./test.jsh
| Error:
| illegal character: '#'
| #!/usr/bin/env jshell -s
| ^
| Error:
| illegal start of expression
| #!/usr/bin/env jshell -s
| ^
Hello World
Run Code Online (Sandbox Code Playgroud)
事实证明,在OpenJDK https://bugs.openjdk.java.net/browse/JDK-8167440中有一个增强请求.
有没有其他方法可以做到这一点?
我想在eclipse氧气里面的gradle项目中使用java9.当我跑:
Run as> Gradle Test on GreeterTest.java
Run Code Online (Sandbox Code Playgroud)
使用以下代码:
package hello.test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import hello.Greeter;
class GreeterTest {
@Test
void test() {
Greeter greeter = new Greeter();
assertEquals("Hello world", greeter.sayHello());
}
}
Run Code Online (Sandbox Code Playgroud)
和我上课一样测试:
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息
无法定位平台:'Java SE 9'使用工具链:'JDK 8(1.8)'.
我的eclipse.init是
Run Code Online (Sandbox Code Playgroud)-startup ../Eclipse/plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar --launcher.library /Users/stein/.p2/pool/plugins/org.eclipse.equinox.launcher.cocoa.macosx.x86_64_\1.1.550.v20170928-1359 -product org.eclipse.epp.package.jee.product -showsplash org.eclipse.epp.package.common --launcher.defaultAction openFile --launcher.a/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/jre/bin -vmargs -Dosgi.requiredJavaVersion=1.9 -Dosgi.instance.area.default=@user.home/eclipse-workspace -XX:+UseG1GC -XX:+UseStringDeduplication --add-modules=ALL-SYSTEM -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts -Dosgi.requiredJavaVersion=1.9 -Xms256m -Xmx1024m --add-modules=ALL-SYSTEM -Xdock:icon=../Resources/Eclipse.icns …
是否有一个函数接受对lambda表达式的引用并返回一个布尔值,表示lambda表达式是否为无状态?如何确定lambda表达式的有状态?
java ×10
java-9 ×10
java-module ×4
java-8 ×2
java-platform-module-system ×2
collectors ×1
gradle ×1
jar ×1
java-stream ×1
jshell ×1
lambda ×1
maven ×1
module-info ×1
shebang ×1
string ×1