Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
How to convert the values of a Map of List to a single List

How is the best (efficient and simple) way to convert the values of a Map<Object1, List<Object2>> to a List<Object2>. Should I simply iterate over all entries and adAll the items to a list?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Here's a third way so you don't need to create an ArrayList, which is what you actually asked. This is the proper way to use streams because you're not changing the state of any object.

List<Object2> values = myMap.values()
                            .stream()
                            .flatMap(Collection::stream)
                            .collect(Collectors.toList());
over 4 years ago · Santiago Trujillo Report

0

Just found it:

 List<Object2> list = myMap.values()
                           .stream()
                           .flatMap(item -> item.stream())
                           .collect(Collectors.toList()));
over 4 years ago · Santiago Trujillo Report

0

One option using addAll.

List<Object2> list = new ArrayList<>();
myMap.values().forEach(list::addAll);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!