0

I have

List<String, Person> generalList

as a list. there is Customer object Under Person, 1 more list under Customer named Id

I want to filter this nested IdList under object but it is not working.

I tried to use flatMap but this code is not working

String s = generalList.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.filter(b -> b.getValue().equals("1"))
.findFirst()
.orElse(null);

I expect the output as String or Customer object

Edit: My original container is a map, I am filtering Map to List

Explanation.

Map<String, List<Person> container;

List<Person> list = container.get("A");

String s = list.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.filter(b -> b.getValue().equals("1"))
.findFirst()
.orElse(null);

Here is the Person

public class Person
{
private Customer customer;

public Customer getCustomer ()
{
    return customer;
}
}

And Customer

public class Customer {
private Id[] idList;
/*getter setter*/
}

And Id

public class Id {
private String value;
/*getter setter*/
}
7
  • 13
    There is no such thing as List<String, Object> do you mean a Map? Commented Feb 5, 2019 at 6:34
  • list or map??b what you ,mean? Commented Feb 5, 2019 at 6:42
  • i guess you meant List<Person> where Person is POJO class Commented Feb 5, 2019 at 6:46
  • 3
    Instead of explaining in the text that it is a Map and not a List, why not put in that extra little bit of effort and update the code instead so it is correct and easier to understand for all of us who reads your question? Commented Feb 5, 2019 at 6:54
  • @a_horse_with_no_name yes it is my mistake. i explained it into edit Commented Feb 5, 2019 at 7:38

2 Answers 2

2

You're possibly looking for a map operation as:

String s = list.stream()
        .flatMap(a -> a.getCustomer().getIdList().stream())
        .filter(b -> b.getValue().equals("1"))
        .findFirst()
        .map(Id::getValue) // map to the value of filtered Id
        .orElse(null);

which is equivalent of(just to clarify)

String valueToMatch = "1";
String s = list.stream()
        .flatMap(a -> a.getCustomer().getIdList().stream())
        .anyMatch(b -> b.getValue().equals(valueToMatch))
        ? valueToMatch : null;
Sign up to request clarification or add additional context in comments.

2 Comments

.flatMap(a -> Arrays.stream(a.getCustomer().getIdList())) is not working, the error is The method stream(T[]) in the type Arrays is not applicable for the arguments (List<Id>). getIdList returns List<Id>
@sakirow updated to adapt to List<Id> as a return type from getIdList
1

Update 2 This solution works directly on a list of Person objects:

String key = "1";
List<Person> list = container.get("A");
String filteredValue = list.stream()
    .flatMap(person -> Arrays.stream(person.getCustomer().getId())
    .filter(id -> id.getValue().equals(key)))
    .findFirst().get().getValue();

Old answer working with map Since you are only interested in the values of your map you should stream on them and in the flatMap I not only got a stream on the getId() list but also filtered on them directly. So if I understood your code structure correctly this should work

String key = "1";

 String filteredValue =  map.values().stream()
     .flatMap(list -> list.stream()
     .flatMap(person -> Arrays.stream(person.getCustomer().getId())
     .filter(id -> id.getValue().equals("1"))))
     .findFirst().get().getValue();

Updated to adjust for edited question

2 Comments

I am converting map to List<Person>
@sakirow fixed so that I use list in my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.