为什么`List`在有`forEach`时没有`map`默认方法? [英] Why does `List` not have a `map` default method when it has `forEach`?

查看:115
本文介绍了为什么`List`在有`forEach`时没有`map`默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究过在Java 8中编写基于流的代码,并注意到了一种模式,即我经常有一个列表,但需要通过对每个元素应用一个简单的映射将其转换为另一个列表。写完 .stream()。map(...)。collect(Collections.toList())还有一次我记得我们有 List。 forEach 所以我找了 List.map 但显然这个默认方法还没有添加。

I have looked into writing streams-based code in Java 8, and have noticed a pattern, namely that I frequently have a list but have the need to transform it to another list by applying a trivial mapping to each element. After writing .stream().map(...).collect(Collections.toList()) yet another time I remembered we have List.forEach so I looked for List.map but apparently this default method has not been added.

为什么 List.map()(编辑:或 List.transform() List.mumble()
没有添加(这是一个历史问题),是否有一个简单的速记使用默认运行时库中的其他方法做同样的事情我有只是没有注意到?

Why was List.map()( or List.transform() or List.mumble()) not added (this is a history question) and is there a simple shorthand using other methods in the default runtime library that does the same thing that I have just not noticed?

推荐答案

当然,我无法深入了解Java设计师的头脑,但我能想到有许多理由不在集合中包含 map (或其他流方法)。

Of course, I can't look into the head of the Java designers, but I can think of a number of reasons not to include a map (or other stream methods) on collections.


  1. 这是API膨胀。同样的事情可以用一种更通用的方式来完成,使用流进行小的打字开销。

  1. It's API bloat. The same thing can be done, in a more general way, with minor typing overhead using streams.

这会导致代码膨胀。如果我在列表上调用 map ,我希望结果与我调用它的列表具有相同的运行时类型(或至少与运行时属性相同)。因此对于 ArrayList.map 将返回 ArrayList LinkedList.map a LinkedList 等。这意味着需要在所有 List 实现中实现相同的功能(接口中合适的默认实现,因此旧代码不会被破坏。

It leads to code bloat. If I called map on a list, I would expect the result to have the same runtime type (or at least with the runtime properties) as the list I called it on. So for a ArrayList.map would return an ArrayList, LinkedList.map a LinkedList etc. That means that the same functionality would need to be implemented in all List implementations (with a suitable default implementation in the interface so old code will not be broken).

它会鼓励像 list.map(function1)这样的代码).map(function2),效率远低于 list.stream()。map(function1).map(function2).collect(Collectors.toList()) 因为前者构造了一个立即丢弃的辅助列表,而后者将这两个函数应用于列表元素,然后才构造结果列表。

It would encourage code like list.map(function1).map(function2), which is considerably less efficient than list.stream().map(function1).map(function2).collect(Collectors.toList()) because the former constructs an auxiliary list which is immediately thrown away, while the latter applies both functions to the list elements and only then constructs the result list.

对于像Scala这样的函数式语言,优缺点之间的平衡可能会有所不同。

For a functional language like Scala the balance between advantages and disadvantages might be different.

我不知道Java标准库中的快捷方式,但您当然可以实现自己的n:

I do not know of shortcuts in the Java standard library, but you can of course implement your own:

public static <S,T> List<T> mapList(List<S> list, Function<S,T> function) {
    return list.stream().map(function).collect(Collectors.toList());
}

这篇关于为什么`List`在有`forEach`时没有`map`默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆