[英]How to group different lists one inside another?
我有包含三個級別的列表的列表。 我正在使用JPA從三個不同的數據庫表中獲取此數據。
更詳細地講,我有三個不同的對象(組,類型,項目):一個包含類型列表的組,該類型列表包含一個項目列表。
我只想從不同的類型和組中獲取一些項目,然后像列表一樣返回這些項目。
問題是:是否有一種優雅而簡單的方法來做到這一點? 還是我真的需要獲取所有項目並逐級分組?
番石榴可能提供一個不錯的解決方案:
Predicate<Group> matchingGroup = ...;
Predicate<Type> matchingType = ...;
Function<Group, Type> getType = new Function<Group, Type>(){};
Function<Type, Item> getItem = new Function...;
// starting point
List<Group> myGroups;
Iterable<Item> filteredItems =
Iterables.transform(
Iterables.filter(
Iterables.transform(
Iterables.filter(myGroups, matchingGroup),
getType),
matchingType),
getItem);
或擴展:
// starting point
List<Group> myGroups;
Iterable<Type> types = Iterables.transform(
Iterables.filter(myGroups, matchingGroup),
getType);
Iterable<Item> items = Iterables.transform(
Iterables.filter(types, matchingType),
getItem);
可能有一種使用謂詞/函數組合方法的方法來縮短此時間。
番石榴在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.