我看到 JDK9 中支持预告片(该HttpResponse.trailers\xe2\x80\x8b()
方法),但在 JDK 10 上不再支持(该HttpResponse.trailers\xe2\x80\x8b()
方法不存在)。
我目前正在使用 JDK11,当我尝试我的请求时:
\nHttpRequest request = HttpRequest.newBuilder()\n .uri(uri)\n .GET()\n .build();\n\n\nHttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString());\n
Run Code Online (Sandbox Code Playgroud)\n我面对一个java.util.concurrent.ExecutionException: java.io.IOException: no statuscode in response
. 仅当我请求带有预告片标题的页面时,它才会失败。\n欢迎任何帮助。谢谢
如果我有一个将单方法接口作为参数的方法,我可以这样调用它:
foo(new Bar() {
@Override
public String baz(String qux) {
return modify(qux) + transmogrify(qux);
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果我必须foo
在一个紧密循环中调用数百万次,我可能更愿意避免每次通过循环创建匿名类的新实例:
final Bar bar = new Bar() {
@Override
public String baz(String qux) {
return modify(qux) + transmogrify(qux);
}
};
while (...) {
foo(bar);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我用 lambda 表达式替换匿名类:
while (...) {
foo(qux -> modify(qux) + transmogrify(qux));
}
Run Code Online (Sandbox Code Playgroud)
这个 lambda 表达式是否等同于上述匿名类示例中的第一个或第二个片段?
ConcurrentMap.getOrPut
Kotlin从一开始(1.0)就有扩展功能。它与 Java 具有相同的签名computeIfAbsent
并执行类似的操作,但有一些细微的差异:
getOrPut
可以在条目中放入空值,但computeIfAbsent
不支持。
getOrPut
不保证默认函数只会被调用一次,而 Java 则computeIfAbsent
保证这一点。
发布时getOrPut
,最新的 Java 版本是 Java 8。在 Java 8 中,computeIfAbsent
即使值已经存在,也始终锁定条目。因此,Kotlin 可能具有更好的性能,因为它在不需要时不会锁定。
然而,在最新版本的 Java 中(我刚刚检查了 17),第 3 点中提到的锁定开销不再存在。所以 KotlingetOrPut
不再有这个优点,但它有第 2 点的缺点。现在在什么情况下我们应该继续使用getOrPut
(假设我们不想在映射中放入 null)或者现在应该将其视为“遗留” ?
我有一个枚举类:
enum class E { A, B, C, D }
Run Code Online (Sandbox Code Playgroud)
初始化包含所有 E 值作为键且每个初始值为 0 的 EnumMap 的最简洁方法是什么?
val map = ...?
assert(map is EnumMap<E, Int>)
assert(map[E.A] == 0)
assert(map[E.B] == 0)
assert(map[E.C] == 0)
assert(map[E.D] == 0)
Run Code Online (Sandbox Code Playgroud)
我能想到的最简洁的是:
val map = E.values().associateWithTo(EnumMap(E::class.java)) { 0 }
Run Code Online (Sandbox Code Playgroud)
然而,名称的重复E
打破了 DRY 原则。而且这个词associateWithTo
有点拗口。有没有更简洁易读的方式?我希望有这样的EnumMap.allOf()
东西EnumSet.allOf()
。
我正在查看来自https://github.com/apache/kafka 的ProducerPerformance.java 。
查看文件的这一部分:
byte[] payload = null;
Random random = new Random(0);
if (recordSize != null) {
payload = new byte[recordSize];
for (int i = 0; i < payload.length; ++i)
payload[i] = (byte) (random.nextInt(26) + 65);
}
Run Code Online (Sandbox Code Playgroud)
有一个检查说“条件i < payload.length
总是错误的”。
我不明白它怎么可能总是假的。recordSize
是Integer
来自命令行参数。有什么我在这里看不到的吗?
我找不到任何allAny()
我能理解的文档。官方文档将其描述为“一个特殊的匹配器,它使用 any() 而不是 eq() 来作为简单参数提供的匹配器”。我不明白那是什么意思。
我有一条线
every { mockObject.method(any(), any(), any(), any(), any(), any(), any(), any(), any()) } returns 0
Run Code Online (Sandbox Code Playgroud)
我以为allAny()
也许可以代替重复使用any()
,但当然mockObject.method(allAny())
是语法错误,因为参数太少了。
那么有什么用呢allAny()
?
例如,如何将可选列表对象从一种类型转换为另一种类型
Optional<List<ProductMultipleOptionViewModel>> productOptionType1 // One type
Optional<List<ProductMultipleOption>> productOptionType2 // Other type
Run Code Online (Sandbox Code Playgroud)
ProductMultipleOptionViewModel
类型 1
@Introspected
public record ProductMultipleOptionViewModel(
ProductOptionViewModel productOption,
String optionName) {
}
Run Code Online (Sandbox Code Playgroud)
类型 2
@Introspected
public record ProductMultipleOption(
ProductOptionViewModel productOption,
String optionName) {
}
Run Code Online (Sandbox Code Playgroud)
我想从 转换Optional<List<ProductMultipleOption>>
为其他Optional<List<ProductMultipleOptionViewModel>>
. 我试过下面的代码
Optional<List<ProductMultipleOptionViewModel>> conveertItem = Optional.ofNullable(product.getProductMultipleOption())
.orElseGet(null)
.stream()
.map(option -> {
return new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(//Access the option value//), //access the option value//
);
})
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我无法访问 map 方法中的选项值
如果product.getProductMultipleOption()
为 null,则返回 null 或空列表。
我正在尝试使用现代函数式语言功能重构以下函数,使其更加符合 Kotlin 习惯:
fun foobar(): List<Account> {
var pageOffset = 0
val accounts: MutableList<Account> = ArrayList()
var chunk: List<Account> = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
while (chunk.isNotEmpty()) {
accounts.addAll(chunk)
pageOffset += MAX_POLL_SIZE
chunk = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
}
return accounts
}
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是用 buildList 替换可变列表,但它仍然不是很实用的风格:
fun foobar2(): List<Account> {
var pageOffset = 0
return buildList {
var chunk: List<Account> = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
while (chunk.isNotEmpty()) {
addAll(chunk)
pageOffset += MAX_POLL_SIZE
chunk = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
}
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想用while
类似的东西替换整个循环accountsService.getAccounts(...).map { ... }
,但我不知道如何重构一个 while …
JEP 421在即将发布的 Java 18 中发布,不赞成终结。我理解这意味着该finalize()
方法已被弃用。然而,它还提到了该try/finally
块并提到了 try-with-resources 作为替代方案,所以我很困惑 - 这是说 try/finally 将被弃用吗?我们是否应该开始更改遗留代码以用 try-with-resources 替换 try/finally ?
我以为这个 JEP 只是关于finalize()
方法,但是互联网上的一些页面(例如https://metebalci.com/blog/what-is-new-in-java-18/)说 try/finally 是被弃用,这听起来有点令人担忧。
Stream.toList的实现(和文档)是这样的:
Collections.unmodifiableList(new ArrayList<>(Arrays.asList(this.toArray())))
Run Code Online (Sandbox Code Playgroud)
我想知道为什么需要将返回的列表Arrays.asList
复制到新的ArrayList
. 仅返回以下内容还不够吗?
Collections.unmodifiableList(Arrays.asList(this.toArray()))
Run Code Online (Sandbox Code Playgroud)
我想知道,如果我编写一个返回它创建的列表的方法,如果我不费心制作它的防御性副本,是否会出现任何问题?
java ×6
kotlin ×4
collections ×1
enum-map ×1
java-11 ×1
java-14 ×1
java-stream ×1
lambda ×1
list ×1
mockk ×1
try-finally ×1