英文:
How do you call a method from within a Collector in Java 8 Streaming?
问题
以下是您提供的代码的中文翻译部分:
原始代码:
private Map<Florist, EnumSet<Flower>> localMethod(List<FlowerSellers> branchList) {
Map<Florist, EnumSet<Flower>> availableFlowers = new EnumMap<>(Florist.class);
branchList.stream()
.filter(f -> f instanceof FlorestFranchised)
.forEach(f -> availableFlowers.put(
FlorestHelperClass.florestTypeForKey(f.id()),
((FlorestFranchised) f).getFlowers()));
return availableFlowers;
}
重写使用收集器的代码:
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
FlorestFranchised::getId,
FlorestFranchised::getFlowers));
在您的重写中,似乎您想要调用FlorestHelperClass.florestTypeForKey
方法来获取Florest
,但它不适用于Collectors.toMap
方法。要实现这一点,您可以使用Collectors.toMap
的另一重载版本,将一个函数用于键映射:
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
p -> FlorestHelperClass.florestTypeForKey(p.getId()),
FlorestFranchised::getFlowers));
这个版本中,FlorestHelperClass.florestTypeForKey(p.getId())
将用作键的映射函数,以获取正确的Florest
对象。
英文:
I have a bit of code that is working functionally as I want it to:
private Map<Florist, EnumSet<Flower>> localMethod(List<FlowerSellers> branchList) {
Map<Florist, EnumSet<Flower>> availableFlowers = new EnumMap<>(Florist.class);
branchList.stream()
.filter(f -> f instanceof FlorestFranchised)
.forEach(f -> availableFlowers.put(
FlorestHelperClass.florestTypeForKey(f.id()),
((FlorestFranchised) f).getFlowers()));
return availableFlowers;
}
For sake of argument the helper class method is:
public static Florist FlorestTypeForKey(String id) {
return FLOREST_MAP.get(id);
}
I would like to do this with a collector instead so I wanted to do this ...
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
FlorestFranchised::getId,
FlorestFranchised::getFlowers));
But obviously this fails because it is not doing the helper class lookup and so rather than returning a map of <Florest, Flowers> it is returning <String, Flowers>
This fails though :
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
FlorestHelperClass.florestTypeForKey(FlorestFranchised::getId),
FlorestFranchised::getFlowers));
Can anyone tell me how I should call the lookup methods (FlorestHelperClass.florestTypeForKey)
in the collector? Is it possible?
答案1
得分: 3
Collectors.toMap
需要两个函数来分别从传递给collect
方法的值中创建键和值。因此,您可以这样做:
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
p -> FlorestHelperClass.florestTypeForKey(p.getId()), // 键
p -> p.getFlowers() // 值
));
最后一个lambda表达式可以替换为FlorestFranchised::getFlowers
。
英文:
Collectors.toMap
requires two functions creating a key and value respectively from the value passed into the collect
method. Therefore you can do this:
return branchList.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.collect(Collectors.toMap(
p -> FlorestHelperClass.florestTypeForKey(p.getId()), // key
p -> p.getFlowers() // value
));
The last lambda expression can be replaced with FlorestFranchised::getFlowers
.
答案2
得分: 2
你可以将首个映射为 AbstractMap.SimpleEntry
对,然后使用 Collectors.toMap
return branchList
.stream()
.filter(p -> p instanceof FlorestFranchised)
.map(p -> (FlorestFranchised) p)
.map(p -> new AbstractMap.SimpleEntry<>(
FlorestHelperClass.florestTypeForKey(p.getId()), p.getFlowers()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
英文:
You can map first as AbstractMap.SimpleEntry
pair then use Collectors.toMap
return branchList
.stream()
.filter(p -> p instanceof FlorestFranchised )
.map(p -> (FlorestFranchised) p )
.map(p -> new AbstractMap.SimpleEntry<>(
FlorestHelperClass.florestTypeForKey(p.getId()), p.getFlowers()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论