public static double getAttackSpeed(ItemStack item) {
Attributes ab = new Attributes(item);
for(Attributes.Attribute attribute : ab.values())
Flawlord.log(attribute.getName());
return FlawlordHelper.stream(ab.values()).filter(a -> a.getAttributeType().equals(Attributes.AttributeType.GENERIC_ATTACK_SPEED)).findFirst().get().getAmount();
}
这并不工作的Java 8迭代流过滤器NoSuchElementException异常
public static int getDamage(ItemStack item) {
Attributes ab = new Attributes(item);
int dmg = 0;
for(Attributes.Attribute attribute : ab.values())
if (attribute.getAttributeType() == Attributes.AttributeType.GENERIC_ATTACK_DAMAGE)
dmg = (int) attribute.getAmount();
return dmg;
}
这个作品
打招呼。有人可以向我解释为什么后者有效,但首先不是?也许我只是没有得到它,idk。我想也许.equals()在这里不起作用?
如果我使用lambda的第一个,我得到一个异常“NoSuchElementException:没有值存在”。
我们怎么知道?什么是'FlawlordHelper'? 'findFirst()'的代码是什么? – UDKOX
这只是意味着没有值与谓词匹配,并且您试图在缺少的'Optional'上调用'get()'。 –
第二个*“工作”*以什么方式?它是否不会崩溃或它为“速度”产生价值? – luk2302