英文:
Can the list returned by a Java streams collector be made unmodifiable?
问题
当使用Java流(streams)时,我们可以使用一个收集器(collector)来生成一个集合,比如一个流。
例如,在这里,我们创建一个Month枚举对象的流,并为每个对象生成包含本地化月份名称的String。我们通过调用Collectors.toList()将结果收集到一个类型为String的List中。
List<String> monthNames =
Arrays
.stream(Month.values())
.map(month -> month.getDisplayName(TextStyle.FULL, Locale.CANADA_FRENCH))
.collect(Collectors.toList());
>monthNames.toString(): [janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
为了使这个列表成为不可修改的,我们可以在Java 10及更高版本中调用List.copyOf。
List<String> monthNamesUnmod = List.copyOf(monthNames);
➥ 是否有一种方法可以使带有收集器的流生成一个不可修改的列表,而不需要我包装调用List.copyOf?
英文:
When working with Java streams, we can use a collector to produce a collection such as a stream.
For example, here we make a stream of the Month enum objects, and for each one generate a String holding the localized name of the month. We collect the results into a List of type String by calling Collectors.toList().
List < String > monthNames =
Arrays
.stream( Month.values() )
.map( month -> month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) )
.collect( Collectors.toList() )
;
>monthNames.toString(): [janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
To make that list unmodifiable, we can call List.copyOf in Java 10 and later.
List < String > monthNamesUnmod = List.copyOf( monthNames );
➥ Is there a way for the stream with collector to produce an unmodifiable list without me needing to wrap a call to List.copyOf?
答案1
得分: 5
Collectors.toUnmodifiableList
是的,有一种方法:Collectors.toUnmodifiableList
与List.copyOf一样,这个特性内置在Java 10及更高版本中。相比之下,Collectors.toList是在Java 8中与Collectors一起首次出现。
在您的示例代码中,只需将最后部分的toList改为toUnmodifiableList。
List<String> monthNames =
Arrays
.stream(Month.values())
.map(month -> month.getDisplayName(TextStyle.FULL, Locale.CANADA_FRENCH))
.collect(Collectors.toUnmodifiableList()); // 🡄 调用 `toUnmodifiableList`。
也适用于Set和Map
Collectors 实用类还提供了将元素收集到不可修改的Set或Map中的选项,就像收集到List中一样。
英文:
Collectors.toUnmodifiableList
Yes, there is a way: Collectors.toUnmodifiableList
Like List.copyOf, this feature is built into Java 10 and later. In contrast, Collectors.toList appeared with the debut of Collectors in Java 8.
In your example code, just change that last part toList to toUnmodifiableList.
List < String > monthNames =
Arrays
.stream( Month.values() )
.map( month -> month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) )
.collect( Collectors.toUnModifiableList() ) // 🡄 Call `toUnModifiableList`.
;
Set and Map too
The Collectors utility class offers options for collecting into an unmodifiable Set or Map as well as List.
答案2
得分: 1
import java.util.stream.Collectors;
List<String> monthNames =
Arrays.stream(Month.values())
.map(month -> month.getDisplayName(TextStyle.FULL, Locale.CANADA_FRENCH))
.collect(
Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList)
);
英文:
In Java 8 we could use Collectors.collectingAndThen.
List < String > monthNames =
Arrays
.stream( Month.values() )
.map( month -> month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) )
.collect(
Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList)
)
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论