简体   繁体   English

结合两个地图java 8列表

[英]Combine two list of map java 8

I have two lists of maps.我有两个地图列表。

List<Map<String,Object>> list1 = new ArrayList<>();
List<Map<String,Object>> list2 = new ArrayList<>();

Now I want to combine these two list but limit the combined list to 10 elements but all the elements from list1 should be there(example if list1 size is 7 and list2 size is 8, my combined list should have 7 elements from list1 and 3 elements from list2).现在我想组合这两个列表,但将组合列表限制为 10 个元素,但 list1 中的所有元素都应该在那里(例如,如果 list1 大小为 7,list2 大小为 8,我的组合列表应该有来自 list1 的 7 个元素和 3 个元素来自列表 2)。

Will the below code guarantee下面的代码是否保证

List<String> newList = Stream.concat(list1.stream(), list2.stream()).limit(10).
                             .collect(Collectors.toList());

Your idea of the code is going to work fine.你对代码的想法会很好地工作。

This is a generic version of the same logic, which you can use for Lists of any type, that is List<T> .这是相同逻辑的通用版本,您可以将其用于任何类型的List<T> ,即List<T>

public static <T> List<T> contatLists(List<T> list1, List<T> list2) {
    return Stream.concat(list1.stream(), list2.stream())
            .limit(10)
            .collect(Collectors.toList());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM