我有一個功能,在List
上執行一些流操作(特別是過濾器)。如何將Stream操作參數添加到我的函數中?
public List<String> getAndFilterNames(List<Person> people, Predicate<Person> nameFilter){
List<String> allNames = people.stream()
.map(person -> person.getName())
.filter(nameFilter)
.collect(Collectors.toList());
return allNames;
}
我希望能夠通過中間流操作(filter
,distinct
等)和有我的功能運行的終端操作之前執行該操作。例如:
public List<String> getAndProcessNames(List<Person> people, <intermediate stream operation>){
List<String> allNames = people.stream()
.map(person -> person.getName())
// perform <intermediate stream operation> here
.collect(Collectors.toList());
return allNames;
}
雖然,根據我目前的經驗水平,這似乎是不可能的。有些中間操作有一個參數(如filter
),而另一些則沒有(如distinct
),所以我不能設置一個處理所有情況的單個參數類型......我想我可以創建幾個簽名,使用Function
和Supplier
。
即使這樣,該功能界面與參數的功能需要改變... filter
需要Predicate
,map
需要Function
,並從我的理解,是沒有辦法來表示一個通用的功能接口。那是對的嗎?沒有實際的共同課堂或界面,他們都從中吸取。
那麼,到底,看來我最好的選擇是隻map
和collect
,然後運行我的期望流操作的情況下,逐案的基礎,如:
public List<String> getNames(List<Person> people){
List<String> allNames = people.stream()
.map(person -> person.getName())
.collect(Collectors.toList());
return allNames;
}
List<Person> employees = // a bunch of people
List<String> employeeNames = getNames(employees);
employeeNames = employeeNames.stream(). // desired operations
編輯 :或者,按@Holger:
public Stream<String> getNamesStream(List<Person> people){
Stream<String> namesStream = people.stream()
.map(person -> person.getName());
return namesStream;
}
List<Person> employees = // a bunch of people
Stream<String> employeeNamesStream = getNamesStream(employees);
employeeNamesStream(). // desired operations
或者是否有我遺失的東西?
那麼,唯一的「優勢」是調用者被迫接收一個收集的List,而不是被允許鏈接自己的終端操作。並且該函數不允許映射到除「String」之外的其他內容。我認爲這是一種反收集模式,是以收集爲中心的觀點造成的。另外,應該提到只是返回流以允許任意後續操作。 – Holger
OP想知道是否有任何方法將「流操作」作爲數據傳遞。 「通過一元運算符」的習語一開始並不明顯,有時也很有用。在這種情況下是否值得是無關緊要的(並且挑剔這是否是正確的應用地點似乎是故意忽略了這一點)。這裏的要點是,從「傳遞流操作」到「傳遞修改流的方式」是一個有用的工具,在您的工具箱中有明顯的需求,而且這些工具顯然還沒有出現OP(和其他)。 –
我並不反對說這個解決方案,我只是建議告訴另一個顯然還沒有出現的解決方案,並且可能解決實際問題,因爲這看起來很像xy問題。 – Holger