TransWikia.com

Combine two list of map java 8

Stack Overflow Asked by Jeeppp on January 25, 2021

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).

Will the below code guarantee

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

One Answer

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>.

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

Answered by Giorgi Tsiklauri on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP