簡體   English   中英

如何使用番石榴的MultiMap拆分值列表

[英]How can I use guava's MultiMap to split list of values

Map的類型為Map<String, List<MyClass>>我有100多個與Key關聯的MyClass對象。 如果超過100,則需要拆分。

例如輸入是

Map<String, List<MyClass>> myMap = new HashMap<String, List<MyClass>>();

myMap.put("ABC", [CustomObject1,CustomObject2,CustomObject3....CustomObject100...CustomObject110]);

&輸出應為

myMap.put("ABC", [CustomObject1,CustomObject2,CustomObject3....CustomObject100]);
myMap.put("ABC", [CustomObject100,CustomObject101....CustomObject110]);

我想到了要獲取myMap.containsKey(string)並檢查列表的大小,然后創建新條目或將其添加到相同的條目中。 我嘗試使用番石榴的多圖,但是當我嘗試獲取元素時它返回Collection>,所以不確定如何插入它。 還是對此有更好的選擇?

嘗試使用Guava Lists.partition

Map<String, List<MyClass>> myMap = new HashMap<String, List<MyClass>>();

myMap.put("ABC", [CustomObject1,CustomObject2,CustomObject3....CustomObject100...CustomObject110]);

Map<String, List<List<MyClass>>> output = myMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> Lists.partition(e.getValue(), 100)));

暫無
暫無

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

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