我想测试一种返回流的方法。
class Foo {
public void getClicks(boolean somecondition) {
Stream s1 = computeAndGetAllClicks(new Date());
if (somecondition) {
Stream s2 = computeAndGetAllClicks(new Date());
}
return Stream.concat(s1, s2);
}
Stream computeAndGetAllClicks(Date d) {
// return stream
}
}
Run Code Online (Sandbox Code Playgroud)
现在我已经为 somecondition = true 的情况编写了以下测试
final ArgumentCaptor<Date> argumentCaptorTodayDate = ArgumentCaptor.forClass(Date.class);
doReturn(Arrays.asList(new Click.Builder().build()).stream())
.when(fooInstance)
.addAtlasLineItem(computeAndGetAllClicks(argumentCaptorTodayDate.capture))
final ArgumentCaptor<Date> argumentCaptorEndDate = ArgumentCaptor.forClass(Date.class);
doReturn(Arrays.asList(new Click.Builder().build()).stream())
.when(fooInstance)
.addAtlasLineItem(computeAndGetAllClicks(argumentCaptorEndDate.capture))
fooInstance.getClicks(true);
Run Code Online (Sandbox Code Playgroud)
但是我得到异常: java.lang.IllegalStateException:流已被操作或关闭。
我该如何解决它?
我在 Spring Boot 2 应用程序中编写了代码,以使用 HTTPUrlConnection 进行第三方 API 调用。
public String loginApi(LoginDTO loginDto)
{
String responseData = null;
HttpURLConnection conn = null;
try {
link = authBaseUrl + loginUrl;
url = new URL(link);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty(CONTENT_TYPE, MEDIA_TYPE);
String body = getAuth0LoginDto(loginDto);
// =====================
// For POST only - START
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(body.getBytes(StandardCharsets.UTF_8));
os.flush();
os.close();
// For POST only - END
// ====================
try (BufferedReader br = (conn.getResponseCode() >= 400
? new BufferedReader(new InputStreamReader(conn.getErrorStream()))
: …Run Code Online (Sandbox Code Playgroud) 我正在测试如何CompletableFuture工作。我对如何并行执行任务感兴趣:
try {
CompletableFuture one = CompletableFuture.runAsync(() -> {
throw new RuntimeException("error");
});
CompletableFuture two = CompletableFuture.runAsync(() -> System.out.println("2"));
CompletableFuture three = CompletableFuture.runAsync(() -> System.out.println("3"));
CompletableFuture all = CompletableFuture.allOf(one, two, three);
all.get();
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,他们将全部被处决。
1 . 当其中一个线程出现异常时,是否可以中断所有正在运行的线程?
2 . 当此代码位于可以从不同线程调用的类方法内部时,它是线程安全的吗?
我有以下数据结构
public class Zones {
private List<Zone> zones;
}
public class Zone {
private int id;
private String name;
private List<Part> parts;
}
public class Part {
private int id;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
这是我的问题。我有一个区域实例,比如 z。
我想流式传输 z 并执行以下操作:使用以下条件构造 z 的映射:如果密钥(基于区域的“Id”)是新的,则使用该密钥和该密钥在映射中创建一个条目区。如果该键是重复的,则将该重复区域的所有“部分”附加到现有区域的部分列表中。最后,我应该有一个以区域的“Id”为键、以区域为值的地图。
在 Java8 中如何使用流来做到这一点?
我一直在尝试遵循一个教程,其中他们基本上使用 java 7,目前我正在 java 8 环境上工作,所以我想知道我正在尝试处理的代码的 java8 版本是什么 - 目前我收到很多错误Optional,使用 eclipse 建议的修复一些初始错误,但我陷入了一些错误。
java7代码如下:
@RequestMapping("/reservations")
public Reservation updateReservation1(ReservationUpdateRequest request) {
Reservation reservation = reservationRepo.findOne(request.getId());
reservation.setNumberOfBags((request.getNumOfBags());
reservation.setCheckedIn(request.getCheckedIn());
return reservationRepo.save(reservation);
}
Run Code Online (Sandbox Code Playgroud)
java8有错误
@RequestMapping("/reservations")
public Optional<Reservation> updateReservation(ReservationUpdateRequest request) {
Optional<Reservation> reservation = reservationRepo.findById(request.getId());
reservation.setNumberOfBags((request.getNumOfBags());
reservation.setCheckedIn(request.getCheckedIn());
return reservationRepo.save(reservation);
}
Run Code Online (Sandbox Code Playgroud)
在java8代码中,eclipse ide在第3行给了我错误 - 也就是说当我试图将值设置到reservation.setNumberOfBags()它显示时The method setNumberOfBags(int) is undefined for the type Optional<Reservation>- 如果有人可以帮忙吗?
foo.thenCompose(fooResponse -> {
...
return bar.thenCompose(barResponse -> {
...
});
}).exceptionally(e -> {
...
});
Run Code Online (Sandbox Code Playgroud)
这也会.exceptionally()捕获从嵌套bar.thenComposelambda 内部抛出的异常吗?或者我需要这样写:
foo.thenCompose(fooResponse -> {
...
return bar.thenCompose(barResponse -> {
...
}).exceptionally(nestedE -> {
...
});
}).exceptionally(e -> {
...
});
Run Code Online (Sandbox Code Playgroud)
然后又吐了?
我想制作一个 javafx TableView,其中的行可能(或可能没有)“相关”行。基本上,我认为它可能是表格视图的表格视图,但是(a)我不确定这是否有效,(b)我的直觉告诉我有一种更简单的方法。
想象一下账单或材料,例如
item material quantity name
1 wood 1 base
2 wood 4 drawer
wood 4 drawer sides
wood 1 drawer base
hardware 1 pull
hardware 8 nails
3 aluminum 4 leg
plastic 1 foot
hardware 1 screws
Run Code Online (Sandbox Code Playgroud)
现在,它按名称(底座、抽屉、腿)排序。如果我按材料排序,我希望项目为 3, 1, 2(或 3, 2, 1):铝,木头,木头。我需要“子项目”与编号项目保留在一起。
我可以制作“复杂的行”吗?(我什至不知道我该怎么称呼它!)或者我是否需要一种自定义排序来将组保持在一起?
在java 8中,当我这样做时,
list.stream().parallel().map(/**/).unordered().filter(/**/).collect(/**/);
Run Code Online (Sandbox Code Playgroud)
list.stream().parallel().unordered().map(/**/).filter(/**/).collect(/**/);
Run Code Online (Sandbox Code Playgroud)
由于两个流都是并行的,我可以理解每个操作(如过滤器、映射等)的所有对象将并行执行,但操作本身将按照定义的顺序顺序执行。
1.在Type1中,我确实在map()操作之后说unordered()。那么,map() 操作是否尝试处理“排序”,因为它位于 unOrdered() 之前?
2.在Type2中,排序不是跨地图维护的,过滤器操作对吗?我的理解正确吗?
我的 Android 应用程序使用的 JAR 依赖于 中的一些类java.time,例如LocalTime,因此至少需要 Java 8。
网上的多个消息来源指出,更高版本的 Android 和工具链支持 Java 8。
我使用 gradle 构建我的应用程序,但不使用(或希望使用)Android Studio。我使用该插件的 3.1.4 版本(最新版本与 Gradle 4.4 兼容,该版本随撰写本文时最新的 Ubuntu LTS 18.04 一起提供)。最低 SDK 为 14,目标为 26。构建工具版本为 27.0.3。
该应用程序构建并安装得很好,但是当它尝试使用其中一个类时,它会崩溃并出现异常:
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalTime;
问题:
java.time适用于支持 Java 8 的 Android 版本?我正在尝试获取整数的最高有效十进制数字,我的方法如下:
我正在尝试获取整数(N)中的数字总数(计数);
我将 Integer(N) 除以 1(....count 乘以 0)。
我已经完成了以下代码,它工作得很好,我只是想使用 java-8 来实现它,如果可能的话。
int N = 456778889;
double expo = Math.log10(N);
int expNum = (int)Math.floor(expo)+1; //gets the total digits in the Integer N
Run Code Online (Sandbox Code Playgroud)
现在,由于我有了数字 N 中的总位数,因此我可以将 N 除以 1 后跟零的总数,以实现此 1 后跟 X 数量的零,我的逻辑如下:
StringBuilder sb = new StringBuilder("1");
sb.setLength(expNum);
String f = sb.toString().replaceAll("[^0-9]","0"); // It makes the 1 followed by X amount of zero that i require to get my MSB
int mostSigNum = N/(Integer.valueOf(f));
System.out.println(mostSigNum);
Run Code Online (Sandbox Code Playgroud)
我知道我的方法有点不同,因为我主要使用日志和其他数学函数,但我真的想以不同的方式做。
java 8和流 …
java-8 ×10
java ×6
java-stream ×3
exception ×2
mockito ×2
android ×1
future ×1
java-7 ×1
java-time ×1
javafx ×1
spring ×1
spring-boot ×1
tableview ×1
treeview ×1
unit-testing ×1