简体   繁体   English

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

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

Map is of type Map<String, List<MyClass>> I have more than 100 objects of MyClass associated with Key. Map的类型为Map<String, List<MyClass>>我有100多个与Key关联的MyClass对象。 I need to split it if it's more than 100. 如果超过100,则需要拆分。

For eg Input is 例如输入是

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

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

& Output should be &输出应为

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

I thought of getting myMap.containsKey(string) and check size of list and then create new entry or add it in same. 我想到了要获取myMap.containsKey(string)并检查列表的大小,然后创建新条目或将其添加到相同的条目中。 I tried using guava's multimap but it returns Collection> when I try to get elements so not sure how to insert it. 我尝试使用番石榴的多图,但是当我尝试获取元素时它返回Collection>,所以不确定如何插入它。 Or if there is any better option for this? 还是对此有更好的选择?

Try using Guava Lists.partition 尝试使用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