不确定这里的语法,lambda表达式和排序的新功能.
itemStream.parallel().filter(Objects::nonNull).forEach(
(item) -> randomMethod(item));
Run Code Online (Sandbox Code Playgroud)
我之前在for循环中所做的是检查null,然后在遇到null项时发出警告
log.warn("Skipping a null item!");
Run Code Online (Sandbox Code Playgroud)
如何使用基于流的方法在遇到空对象(同时仍在过滤时)时进行记录?
让我们说,我有一个数组列表:
catalog id--->901
Catalog name --->series277
catalog id--->901
Catalog name --->series277
catalog id--->545
Catalog name --->series285
catalog id--->545
Catalog name --->series285
catalog id--->545
Catalog name --->series285
catalog id--->546
Catalog name --->series685
catalog id--->546
Catalog name --->series685
catalog id--->40962
Catalog name --->series19348
catalog id--->40962
Catalog name --->series19348
catalog id--->40962
Catalog name --->series19348
catalog id--->40962
Run Code Online (Sandbox Code Playgroud)
从这个列表中,我想找到如下所示的出现次数.
catalog id--->901 -- 2
catalog id--->545 -- 3
catalog id--->546 -- 2
catalog id--->40962 -- 4
Run Code Online (Sandbox Code Playgroud)
我的目标是,基于id我想要创建循环以进一步迭代的出现次数.
请找到我的Arraylist创建的java代码
List<Catalog> catalogList2 = new ArrayList<Catalog>();
for …Run Code Online (Sandbox Code Playgroud) 我从这里阅读有关java 8默认方法时遇到了以下段落:
如果层次结构中的任何类具有具有相同签名的方法,则默认方法变得无关紧要.默认方法不能覆盖java.lang.Object中的方法.原因很简单,因为Object是所有java类的基类.因此,即使我们将Object类方法定义为接口中的默认方法,它也将是无用的,因为将始终使用Object类方法.这就是为什么要避免混淆,我们不能有重写Object类方法的默认方法.
我很快尝试使用代码来确认行为
public class DefaultMethodClass {
public void defaultMethod()
{
System.out.println("DefaultMethodClass.defaultMethod()");
}
}
public interface DefaultMethodInterface {
public default void defaultMethod()
{
System.out.println("DefaultMethodInterface.defaultMethod()");
}
}
public class DefaultMethodClassInterfaceChild extends DefaultMethodClass implements DefaultMethodInterface
{
public static void main(String[] args) {
(new DefaultMethodClassInterfaceChild()).defaultMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
这打印
DefaultMethodClass.defaultMethod()
Run Code Online (Sandbox Code Playgroud)
我无法看到为什么语言设计师选择这种特定行为背后的任何具体原因.他们的任何重要原因是我失踪了吗?或者它只是接口默认方法在逻辑上比超类提供的具体实现重要性低?
我正在使用Java8实现以下目的,
Map<String, String> m0 = new HashMap<>();
m0.put("x", "123");
m0.put("y", "456");
m0.put("z", "789");
Map<String, String> m1 = new HashMap<>();
m1.put("x", "000");
m1.put("y", "111");
m1.put("z", "222");
List<Map<String, String>> l = new ArrayList<>(Arrays.asList(m0, m1));
List<String> desiredKeys = Lists.newArrayList("x");
List<Map<String, String>> transformed = l.stream().map(map -> map.entrySet().stream()
.filter(e -> desiredKeys.stream().anyMatch(k -> k.equals(e.getKey())))
.collect(Collectors.toMap(e -> e.getKey(), p -> p.getValue())))
.filter(m -> !m.isEmpty())
.collect(Collectors.toList());
System.err.println(l);
System.err.println(transformed);
List<String> values = new ArrayList<>();
for (Map<String,String> map : transformed) {
values.add(map.values().toString());
System.out.println("Values inside map::"+map.values());
}
System.out.println("values::"+values); //values::[[123], [000]]
Run Code Online (Sandbox Code Playgroud)
在这里,我只想从列表中获取x值。我已经实现了,但是格式不正确。 …
我读了为什么Java 8的Optional不能用在参数中.但是Optional(Int|Long|Double),有什么理由反对使用它们作为输入参数?
正如我所理解的那样,它们不能是空的,而是null(Optional<X>输入参数中的主要参数参数),但可能还有其他的珊瑚礁?我应该使用OptionalInt或Integer使用null而不是null重视?
我有一个类似于此的数据结构:
[{option : {some object A},
feature : "some string value",
score : 0.9
},
{option : {some object B},
feature : "some other string value",
score : 0.9
},
{option : {some object C},
feature : "some string value",
score : 0.6
},{option : {some object D},
feature : "some string value",
score : 1.0
}]
Run Code Online (Sandbox Code Playgroud)
我想过滤具有独特功能的选项。如果功能相同,我想选一个得分最高的。对于上面的示例,预期结果将是:
[{option : {some object B},
feature : "some other string value",
score : 0.9
},{option : {some object D},
feature : "some …Run Code Online (Sandbox Code Playgroud) 考虑我有一个列表,其中包含两种类型的数据,一种是有效的,另一种是无效的.
如果我开始过滤此列表,我可以在最后收集两个列表吗?
我想根据过滤器收集物品.但如果未找到匹配项,则不应初始化结果列表.我更喜欢null而不是空列表.
List<String> match = list
.stream()
.filter(item -> item.getProperty == "match")
.collect(Collectors.toList());
if (match != null && !match.isEmpty()) {
//handle seldom match
}
Run Code Online (Sandbox Code Playgroud)
问题:大多数时候我没有匹配,导致空集合.这意味着即使我不需要,列表也会在大多数情况下实例化.
这是一个非常简单的例子:
private boolean f(List x) {
return x != null && !x.isEmpty();
}
private boolean f(Map x) {
return x != null && !x.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
两个函数内部的代码相同,它们只对不同的对象进行操作.我想将它们合并到一个函数中以避免代码重复.
我尝试过类似的东西:
private <T> boolean f(T x) {
return x != null && !x.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
但它给出了错误 x.isEmpty()
我有一个Optional并且想要调用一个带有'内容的函数(如果存在),如果没有则抛出.问题是map不会采用无效方法.
File file;
//...
Optional maybeFile = Optional.ofNullable(file);
//..
maybeFile
.map(f -> writeTo(f, "stuff")) //Compile error: writeTo() is void
.orElseThrow(() -> new IllegalStateException("File not set"));
Run Code Online (Sandbox Code Playgroud)
如何在保持writeTo无效的同时实现这一点?