I\xc2\xb4m 使用 Stream 并findFirst()返回Optional,但也有 \xc2\xb4s 有可能我的StreamMaybe 不会发出任何东西,所以我添加了orElse在底部添加了运算符。
问题是orElse不返回可选值,而是返回可选值的类类型findFirst。
def optional = categories.categories.stream()\n .filter { category -> category.name == selCategory }\n .map { category -> loadUniqueIds.call(category) }\n .map { UUIDs -> new JsonArray(UUIDs) }\n .findFirst().orElse{Optional.of(new JsonArray())}\n optional.get()\nRun Code Online (Sandbox Code Playgroud)\n所以我最终无法使用findFirst并且orElse。
我\xc2\xb4m在这里做错了什么?
\n谢谢
\n更新:
\n我最终删除了orElse之后findFirst并稍后检查可选的
optional.isPresent() ? optional.get(): new JsonArray()\nRun Code Online (Sandbox Code Playgroud)\n无论如何,如果有人有更好/优雅的解决方案,请告诉我。
\n问候。
\n我有一个 Fruit 对象列表,其中每个 Fruit 都有一个'name'和'desc'。此水果列表将包含'name'不同的重复项'desc'。i.e.
{"apple","its red"},{"banana","its yellow"},{"apple", "its hard"}
Run Code Online (Sandbox Code Playgroud)
现在,我想使用 Java 8 Streams API 迭代此 Fruits 列表,并将它们映射到 MAP 中,使得键为“name”且不得包含重复项。
输出应该是:
key - "apple", value - List of desc i.e. {"its red","its hard"}
key - "banana", value - {"its yellow"}
Run Code Online (Sandbox Code Playgroud)
请指导。
我想确定“行”中的“列”或更好:构建地图列表的总和,例如 List> rows
是否可以以某种方式对每个不同列的所有值求和?该函数应返回一个 Map,其中列作为键,所有值的总和作为值。
summMap.get("columname")
Run Code Online (Sandbox Code Playgroud)
假设我有以下地图列表:
List<Map<String, Long>> mapList = new ArrayList();
Map<String, Object> map1 = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
map1.put("col1", 90);
map1.put("col2", 50);
map1.put("col3", 10);
map2.put("col1", 90);
map2.put("col2", 50);
map2.put("col3", 10);
map3.put("col1", 90);
map3.put("col2", 50);
map3.put("col3", 10);
mapList.add(map1);
mapList.add(map2);
mapList.add(map3);
Map<String, Long> sum = mapList.stream().distinct().sum() // Example
// result i'm awaiting/expecting
Long sumVal1 = sum.get("col1"); // 270
Long sumVal2 = sum.get("col2"); // 150
Long sumVal3 = sum.get("col3"); // …Run Code Online (Sandbox Code Playgroud) 我试图使用 Java1.8 使用 help Stream 函数将下面的 for 循环转换为 forEach 方法,但我很困惑,也很困惑这样做。
List<A> valueList = new ArrayList<>();
List<B> responseList = getResponses();
List<A> value = new ArrayList<>();
for (B getResponse: responseList) {
valueList = getValues(getResponse);
value.addAll(valueList);
}
Run Code Online (Sandbox Code Playgroud) 我有Map1000 个项目,我想List从它的前 500 个项目中生成一个,Map并删除从Map.
换句话说,我想过滤、收集到 aList并从 a 中删除项目Map。
我正在尝试这样的事情:
final int i = 0;
int max = 5;
Map<String, Object> map = new HashMap<>();
map.put("ads", "123");
map.put("qwe", "123");
map.put("cvb", "123");
map.put("asd", "123");
map.put("iop", "123");
map.put("jkl", "123");
map.put("yui", "123");
List list = map.entrySet().stream().filter(y -> i++ < max).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
预期输出:
具有 2 个值的映射
包含 5 个值的列表
如何使用流优化它?
List<String> prepared = new ArrayList<>();
availableFieldsFromImage.forEach(field -> {
if(field.contains(".")){
prepared.add(field.split("\\.")[0]);
} else {
prepared.add(field);
}
});
Run Code Online (Sandbox Code Playgroud) 我有两个数组如下:
k[] = {"K1","K2","K3"}
v[] = {"V1","V2","V3"}
Run Code Online (Sandbox Code Playgroud)
我想以这样的方式使用 Stream API 迭代这两个数组,以便我将它们作为 Map 收集
[K1=V1,K2=V2,K3=V3]
Run Code Online (Sandbox Code Playgroud) 我有一个对象列表。我需要从对象列表中找到最小数量。但如果有平局,则需要找到年龄最小的那个。如何使用java 8完成它?
class TestObject{
private Double amount;
private int age;
}
List<TestObject> objectList = getAllItems();
TestObject obj = objectList.stream()
.min(Comparator
.comparing(TestObject::getAmount))
.get();
Run Code Online (Sandbox Code Playgroud) 我正在尝试改进我已经可以运行的代码,所以这就是事情,我有一个返回 a 的方法Map:
public Map<String, String> extractPartitionsValues(java.nio.file.Path marketFile) {
Map<String, String> map = new HashMap<>();
for (String p : this.partitions) {
map.put(p, partitionValueFromFilePath(p, marketFile.toString()));
}
return map;
}
Run Code Online (Sandbox Code Playgroud)
我想使用stream和collectorsToMap在一行中重新运行它,但我真的很菜,所以我阅读了文档和我发现的所有内容,但它仍然不起作用,有人可以帮我解决这个问题吗?
我有一个这样的对象:
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
private String name;
private String lastName;
}
Run Code Online (Sandbox Code Playgroud)
还有一个,我们称之为:
@Data
@AllArgsConstructor
@NoArgsConstructor
class DBPerson {
private String name;
private String lastName;
}
Run Code Online (Sandbox Code Playgroud)
假设两个对象都有一个带有name和的构造函数lastName以及它们各自的 getter 和 setter,我正在使用lombok
List<DBPerson> dbPersons = new ArrayList();
dbPersons.add(new DBPerson("Harry", "Potter"));
dbPersons.add(new DBPerson("Hermione", "Granger"));
dbPersons.add(new DBPerson("Ronald", "Weasley"));
Run Code Online (Sandbox Code Playgroud)
我需要从 a 转换List<DBPerson>为List<Person>,所以我尝试了这种方式:
dbPersons.stream()
.map(DBPerson::getName)
.map(Person::new)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这显然给了我一个错误,new因为没有带有单个StringonPerson类的构造函数
我怎么能映射两个属性DBPerson来创建一个新的Person,然后将所有这些都收集到一个List<Person>?
我已经尝试过.forEach,但是我不确定它是否是最佳解决方案,所以如果有另一种使用 Stream …
java-stream ×10
java ×8
java-8 ×4
collections ×1
foreach ×1
groovy ×1
hashmap ×1
inline ×1
merge ×1