linhos
linhos

Reputation: 111

java 8 List<Pair<String, Integer>> to List<String>

There is a List>:

List<Pair<String, Integer>> list =new ArrayList<>();

Pair is javafx.util.Pair which has a key and a value. and a

Integer tmp;

how should I get all String which Pair.getValue() >=tmp by java 8 stream?

Upvotes: 1

Views: 5501

Answers (2)

Mauro Palsgraaf
Mauro Palsgraaf

Reputation: 237

list.stream()
    .filter(p -> p.getValue() >= tmp)
    .map(pair -> pair.getKey())
    .collect(Collectors.toList())

Where map can be changed to static method reference as Pair::getValue

Since this will return a stream and you most likely want to get back a List, you need to transform it to a list with .collect(Collection.asList())

Upvotes: 0

Eugene
Eugene

Reputation: 120848

Something like this, haven't compiled it since I can't tell what a Pair is; but assuming it has two parts left and right and there are getters for it.

list.filter(p -> p.getRight() >= tmp)
    .map(Pair::getLeft)
    .collect(Collectors.toList());

Upvotes: 5

Related Questions