简体   繁体   中英

Get a list of map keys from a deque in Java

I have a deque

Deque<Map<int, String> cars = new LinkedList();

I want to use Java stream to collect all the keys from the deque map into a

List<int>. 

Is there any way to do this?

I tried something like

cars.stream().map(car -> car.keySet()).collect(Collectors.toList()

This question is different from the previous question. I want to collect all the keys, as opposed to removing a map from the deque.

Use flatMap() :

cars.stream()
    .map(Map::keySet)
    .flatMap(Set::stream)
    .collect(Collectors.toList())

You could flatMap the maps' keys:

List<Integer> keys =
    cars.stream().flatMap(car -> car.keySet().stream()).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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