繁体   English   中英

Java 8 流:myList.stream().map(Foo::bar).collect(Collectors.toList()) 的简写

[英]Java 8 streams: Shorthand for myList.stream().map(Foo::bar).collect(Collectors.toList())

以下是否有通用的简写? 欢迎像番石榴这样的外部依赖项。

myList.stream().map(Foo::bar).collect(Collectors.toList());

如果我必须实现它,它会是这样的:

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

有没有适用于任何 Iterable 的? 如果没有,我该怎么写? 我开始这样思考:

static <T, U, V extends Iterable> V<U> mapApply(V<T> iterable, Function<T, U> function) {
    return iterable.stream().map(function).collect(???);
}

Foo::bar再次返回Foo实例的情况下,即。 您需要再次将T转换为T ,然后您可以使用使用UnaryOperator<T> List::replaceAll ,因此每个项目都被相同类型的一个替换。 此解决方案改变了原始列表。

List<String> list = Arrays.asList("John", "Mark", "Pepe");
list.replaceAll(s -> "hello " + s);

如果您想将T转换为R ,您所能做的就是将当前的解决方案与一系列stream() -> map() -> collect()方法调用或简单迭代一起使用。

包装 this 的静态方法也可以这样做。 请注意,您不能使用相同的方式从CollectionIterable创建Stream 也可以随意传递您的自定义Collector

  • T是输入CollectionIterable的泛型类型。
  • R是映射函数结果的泛型类型(从TR映射)

来自Collection<T>

List<Bar> listBarFromCollection = mapApply(collectionFoo, Foo::bar, Collectors.toList());
static <T, R> List<R> mapApply(Collection<T> collection, Function<T, R> function) {
    return collection.stream()
        .map(function)
        .collect(Collectors.toList());
}

来自Iterable<T>

List<Bar> listBarFromIterable = mapApply(iterableFoo, Foo::bar, Collectors.toList());
static <T, R> List<R> mapApply(Iterable<T> iterable, Function<T, R> function) {
    return StreamSupport.stream(iterable.spliterator(), false)
        .map(function)
        .collect(Collectors.toList());
}

...与Collector

如果要传递自定义Collector ,它将是Collector<R, ?, U> collector和方法U的返回类型而不是List<R> 正如@Holger 指出的那样,将Collector传递给方法与调用实际的stream() -> map() -> collect()没有太大区别。

你可以实现这样的方法:

public class StreamShorthandUtil {
    
    public static <T, U, V extends Collection<T>, W> W mapApply(V in, Function<T, U> function, Collector<U, ?, W> collector) {
        return in.stream().map(function).collect(collector);
    }

    // main class for testing
    public static void main(String[] args) {
        List<String> numbersAsString = Arrays.asList("1", "2", "3", "4", "5");
        List<Integer> numbers = mapApply(numbersAsString, Integer::parseInt, Collectors.toList());
    }
}

除了输入和映射器函数之外,此方法还有一个Collector参数,用于定义返回的类型。

您可以使用Eclipse Collections ,它直接在集合上使用具有丰富 API 的集合,并为collect (又名map )等方法提供协变返回类型。

例如:

MutableList<Foo> fooList;
MutableList<Bar> barList = fooList.collect(Foo::bar);

MutableSet<Foo> fooSet;
MutableSet<Bar> barSet = fooSet.collect(Foo::bar);

MutableBag<Foo> fooBag;
MutableBag<Bar> barBag = fooBag.collect(Foo::bar);

Iterate实用程序类也适用于任何Iterable并提供丰富的协议集。

Iterable<Foo> fooIterable;
List<Bar> barList = Iterate.collect(fooIterable, Foo::bar, new ArrayList<>());
Set<Bar> barSet = Iterate.collect(fooIterable, Foo::bar, new HashSet<>());

注意:我是 Eclipse Collections 的提交者。

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string>

[英]How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .collect(Collectors.toList())和Java方法上的Streams Java,Streams:如何使用.collect(Collectors.toList())转换表达式 Java 16的Stream.toList()和Stream.collect(Collectors.toList()的区别? Java Stream Collectors.toList()不会编译 java中Stream Collectors.toList()中的不兼容类型 努力理解“清单 <String> list = people.stream()。map(Person :: getName).collect(Collectors.toList());” 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> 如何在Kotlin中的Java 8 Stream上调用collect(Collectors.toList())? Java .forEach(list :: add)与.collect(Collectors.toList()) Java Stream:如何避免在 Collectors.toList() 中添加 null 值?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM