1
Map<String,String> persons = new HashMap<>();
persons.put("aaaa@testing","123456789");
persons.put("bbbb@testing","987654321");

Map<String,UsersDTO> users = new HashMap<>();
users.put("aaaa@testing", UsersDTO1);
users.put("bbbb@testing",UsersDTO2);

//Below one is the my required final map by using above two maps by using java 8 Lambdas
Map<String,UsersDTO> finalMap = new HashMap<>();
finalMap.put("123456789",UsersDTO1);
finalMap.put("987654321",UsersDTO2);

How to make finalMap by using the two maps above? This type of question might be there but I want to give special focus on this so that's why I am posting it. How to make by using the lambda expressions?

2
  • 2
    You want your final map to be <String,String> but then you are puting UserDtos to it? Commented Sep 12, 2019 at 19:29
  • Sorry it's typo mistake, finally i want <String,UserDto> generic type map Commented Sep 12, 2019 at 19:41

2 Answers 2

4

You could do that but note that you will get a Map<String,UserDto>:

Map<String,UsersDTO> finalMap =
        persons.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getValue, e-> users.get(e.getKey())));

As suggested by Andreas, if the email doesn't have a match between the two maps, you could handle that case. For example by ignoring the entry :

Map<String, UsersDTO> finalMap =
        persons.entrySet().stream()
                .filter(e -> users.containsKey(e.getKey()))
                .collect(Collectors.toMap(Map.Entry::getValue, e -> users.get(e.getKey())));
Sign up to request clarification or add additional context in comments.

2 Comments

You're missing a filter() call to exclude persons entries without matching users entry.
Indeed.But not sure how the op would handle that (filtering, default value, exception) but added anyway an example with filtering.
1

I think you mean something like so :

Map<String, UsersDTO> finalMap = users.entrySet().stream()
        .collect(Collectors.toMap(user -> persons.get(user.getKey()), Map.Entry::getValue));

Here is a simple ideone demo to the expected result.


As commented by Andreas, you need another filter to make sure that each entry has a matching :

Map<String, UsersDTO> finalMap = users.entrySet().stream()
        .filter(user -> persons.containsKey(user.getKey()))
        .collect(Collectors.toMap(user -> persons.get(user.getKey()), Map.Entry::getValue));

5 Comments

You're missing a filter() call to exclude users entries without matching persons entry.
@Andreas you are correct, without the filter it can cause some issues. fix it ;)
Can Please suggest where i can learn this complex lamdas, suggest me any resource for learing
@SrikanthJanapati by practice and learning books for example amazon.fr/Java-Lambdas-Pragmatic-Functional-Programming-ebook/…
@YCF_L Thank you

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.