簡體   English   中英

Java 8 Lambda-多次使用流

[英]java 8 lambda - use stream multiple times

是否有可能避免從同一集合中的當前流內部創建流,如以下示例中那樣收集一些數據(listOfA被使用兩次來創建流)?

List<A> listOfA = Arrays.asList(new A(1L, "A1", "V1"), new A(2L, "A2", "V1"), new A(1L, "A1", "V2"));

List<B> listOfB = listOfA.stream().map(r -> new B(r.getId(), r.getName(),
            listOfA.stream().filter(r2 -> r.getId().equals(r2.getId())).map(A::getVal).collect(toSet())
    )).distinct().collect(toList());

class A {
     private final Long id;
     private final String name;
     private final String val;

     A(Long id, String name, String val) //constructor
     //getters

}

class B {
     private final Long id;
     private final String name;
     private final Set<String> values;

     B(Long id, String name, Set<String> values) //constructor
     //getters


     @Override
     public boolean equals(Object o) {
          ...
          return id.equals(a.id);
     }
     //hashCode
 }

最終結果應該是2個對象的列表:

B {id = 1,名稱=“ A1”,值= [V1,V2]}

B {id = 2,名稱='A2',值= [V1]

提前致謝!

我不確定您要問的目標是什么。 如果問題是為了避免重新創建流而需要進行最小的更改,那么我必須回答:我不知道。 但是,您的方法似乎過於復雜。 mapcollectfilterdistinctcollect是內置有實在令人費解。

經過短暫的咆哮...

也許我擔心將來的Java程序都會看起來像這樣,因此變得完全無法維護是沒有道理的。 也許只是“習慣”了這種編程風格。 可能會有一個短暫的時期,人們過於渴望使用新的語言功能,並且遲早會恢復為“正常”風格(以及功能元素的“健康”水平)。 但是我個人認為,像createBsByMergingTheValuesOfAs()這樣的方法在這里就足夠了。

...我想建議使用一個專用的Collector ,它已經提供了許多可變可變的基礎結構,您似乎正在使用此操作鏈進行模擬:

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamCollectTest
{
    public static void main(String[] args)
    {
        List<A> listOfA = Arrays.asList(
            new A(1L, "A1", "V1"), 
            new A(2L, "A2", "V1"), 
            new A(1L, "A1", "V2"));

        Map<Long, B> result = listOfA.stream().collect(
            Collectors.toConcurrentMap(

                // The "id" will be the key of the map
                a -> a.getId(), 

                // The initial value stored for each key will be a "B"
                // whose set of values contains only the element of 
                // the corresponding "A"
                a -> new B(a.getId(), a.getName(), 
                    new LinkedHashSet<String>(Collections.singleton(a.getVal()))),

                // Two "B"s with the same key will be merged by adding
                // all values from the second "B" to that of the first
                (b0,b1) -> { b0.values.addAll(b1.values); return b0; }));

        System.out.println(result);
    }

    static class A
    {
        private final Long id;
        private final String name;
        private final String val;

        A(Long id, String name, String val)
        {
            this.id = id;
            this.name = name;
            this.val = val;
        }

        public Long getId()
        {
            return id;
        }

        public String getName()
        {
            return name;
        }

        public String getVal()
        {
            return val;
        }
    }

    static class B
    {
        private final Long id;
        private final String name;
        private final Set<String> values;

        B(Long id, String name, Set<String> values)
        {
            this.id = id;
            this.name = name;
            this.values = values;
        }

        @Override
        public String toString()
        {
            return id+","+name+","+values;
        }
    }
}

它打印

{1=1,A1,[V1, V2], 2=2,A2,[V1]}

因此,結果圖的values()應該正是您要尋找的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM