问题:您如何处理这些用例?
在日常业务中,我经常需要检查日期 a <= 日期 b 或日期 a >= 日期 b。
互联网经常建议使用 isBefore/isAfter 方法的否定版本。
在实践中我发现我
我想我的一部分仍然希望我只是忽略了 API 中的相应方法(请!)。
/**
* @return true if candidate >= reference </br>
* or in other words: <code>candidate.equals(reference) || candidate.isAfter(reference)</code> </br>
* or in other words: <code>!candidate.isBefore(reference) </br>
* or in other words: <code>candidate.compareTo(reference) >= 0
*/
public static boolean isEqualOrAfter(LocalDate candidate, LocalDate reference)
{
return !candidate.isBefore(reference);
}
/**
* @return true if candidate …Run Code Online (Sandbox Code Playgroud) 我下面有两个测试函数,我希望返回相同的结果。第一个 (monoTest3) 在 '.then()' 方法中使用 lambda 表达式。第二个没有。
为什么我会得到我所做的结果?
使用 ReactiveX 和 lambda 表达式时有哪些注意事项?
在下面的代码中,monoTest3 产生
monoVoid called
Success
----
monoVoid2 called
Success
Run Code Online (Sandbox Code Playgroud)
和 monoTest4 产生(所需的结果):
monoVoid called
monoVoid2 called
Success
----
monoVoid2 called
monoVoid called
Success
Run Code Online (Sandbox Code Playgroud)
最后是代码。
private static Mono<Void> monoVoid() {
System.out.println("monoVoid called");
return Mono.empty();
}
private static Mono<Void> monoVoid2() {
System.out.println("monoVoid2 called");
return Mono.just("Hello").then();
}
@Test
public void monoTest3() throws Exception {
monoVoid()
.then( v -> monoVoid2())
.doOnSuccess(v -> System.out.println("Success"))
.block();
System.out.println("----");
monoVoid2()
.then( v -> monoVoid())
.doOnSuccess(v …Run Code Online (Sandbox Code Playgroud) 我有一个需要方法引用的方法:
expectsMethodRef(obj::someMethod);
Run Code Online (Sandbox Code Playgroud)
我现在只在编译时通过反射检索方法。如何从 Method 对象获取方法引用?
Method method = obj.class.getMethod(methodNameStr);
expectsMethodRef(<how to input my method here?>);
Run Code Online (Sandbox Code Playgroud) 我正在使用 apachecommons-text:RandomStringGenerator来生成这样的随机数String:
//Utilities
private static RandomStringGenerator generator(int minimumCodePoint, int maximumCodePoint, CharacterPredicates... predicates) {
return new RandomStringGenerator.Builder()
.withinRange(minimumCodePoint, maximumCodePoint)
.filteredBy(predicates)
.build();
}
public static String randStringAlpha(int length) {
return generator('A', 'z', CharacterPredicates.LETTERS).generate(length);
}
public static String randStringAlphaNum(int length) {
return generator('1', 'z', CharacterPredicates.LETTERS, CharacterPredicates.DIGITS).generate(length);
}
//Generation
private void foo() {
String alpha = randStringAlpha(255);
String num = randStringAlphaNum(255);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种使用相同库生成以下内容的方法:
我似乎对 IntelliJ 有关于setOnActionlambda 函数的问题。经过大量研究,我找不到我的问题的答案。
我正在学习用 Java(最新的 JDK 8 版本)编程,最近从 NetBeans 转移到 IntelliJ IDE。我编写了一个非常简单的程序,代码在 NetBeans 中完美运行,但是我在使用 IntelliJ 时遇到了问题。
setOnActionIntelliJ 无法识别该函数。我已经配置了 IDE(项目结构/模块/并选择了 8 - Lambda's,类型注释,...)但没有成功。我手动添加:import javafx.event.ActionEvent;
我还配置了(设置/常规/自动导入/动态添加明确的导入)。
该程序包含两个类,一个 Main 类和一个 GUI 类。
主要类:
package fxvb0203;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class fxvb0203 extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
FlowPane root = new FlowPane();
Scene scene = new Scene(root);
new GUI(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show(); …Run Code Online (Sandbox Code Playgroud) 我有这样一个地图,Map<String,String[]>我想总结它的值,值是字符串表,我知道它们是数字。请告诉我如何使用流来做到这一点。我试过了 :
Map<String, String[]> map = new HashMap<>();
int sum = map.values().stream().mapToInt(Integer::parseInt).sum();
Run Code Online (Sandbox Code Playgroud) 我有一个函数 ( findByNames) 接受传播参数,如下例所示:
List<Users> findByNames(String... names)
{
...
}
Run Code Online (Sandbox Code Playgroud)
作为参数,我有一个列表:
List<String> names = asList("john","abraham");
Run Code Online (Sandbox Code Playgroud)
所以我想将names列表转换为传播对象以使用findByNames函数,这可以使用 Java 8 吗?我试过这个解决方案:
MapUtils.getMap(names.toArray(new String[names.size()]))
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
谢谢你的时间。
在java 8中,以下之间有什么真正的区别:
try (OutputStream os = Files.newOutputStream(path)) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
和
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
我阅读了这个SO question and answers,但它让我很困惑。
PS:Java 11 有什么变化?
我可以使用一个 lambda 表达式来完成所有逻辑吗?
boolean isTrue = myList.stream().anyMatch(m -> m.getName().equals("a");
if(isTrue) { do something }
else { do something }
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Java 中使用流,我有一个学生课程:
@Entity
@Data @AllArgsConstructor @NoArgsConstructor
public class Student {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
Run Code Online (Sandbox Code Playgroud)
我添加了一些学生:
Stream.of("John","Sophie","emilia").forEach(s->{
studentRepository.save(new Student(s));
});
Run Code Online (Sandbox Code Playgroud)
问题出在以下代码中:
int[] empIds = { 1, 2, 3 };
List<Student> students= Stream.of(empIds)
.map(studentRepository::findById).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我收到此错误:方法引用中的返回类型错误:无法将 java.util.Optional 转换为 R。我的 IDE 在 studentRepository::findById 下划线。非常感谢。