Java 8 List V into Map K V

0 votes

I want to translate a List of objects into a Map using Java 8's streams and lambdas.

This is how I would write it in Java 7 and below.

private Map<String, Choice> nameMap(List<Choice> choices) {
        final Map<String, Choice> hashMap = new HashMap<>();
        for (final Choice choice : choices) {
            hashMap.put(choice.getName(), choice);
        }
        return hashMap;
}

I can accomplish this easily using Java 8 and Guava but I would like to know how to do this without Guava.

In Guava:

private Map<String, Choice> nameMap(List<Choice> choices) {
    return Maps.uniqueIndex(choices, new Function<Choice, String>() {

        @Override
        public String apply(final Choice input) {
            return input.getName();
        }
    });
}

And Guava with Java 8 lambdas.

private Map<String, Choice> nameMap(List<Choice> choices) {
    return Maps.uniqueIndex(choices, Choice::getName);
}
Nov 29, 2018 in Java by Sushmita
• 6,910 points
1,360 views

1 answer to this question.

0 votes

Based on Collectors documentation it's as simple as:

Map<String, Choice> result =
    choices.stream().collect(Collectors.toMap(Choice::getName,
                                              Function.identity()));
answered Nov 29, 2018 by Daisy
• 8,120 points

Related Questions In Java

0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
659 views
0 votes
1 answer

Convert data in List<JsonNode> to Map <String,String> in Java

Hi, @Jyra, Basically, you can use the object mapper to ...READ MORE

answered Apr 7, 2020 in Java by Gitika
• 65,910 points
20,752 views
0 votes
2 answers

I want list of date of every week based on pass dynamically "Mon ,Tue, Wed, Thu, Fri, Sat, Sun" between startDate and endDate using Java 8 LocaleDate

public List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, ...READ MORE

answered Sep 14, 2020 in Java by anonymous
• 190 points
1,085 views
0 votes
2 answers

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,190 points
62,682 views
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
3,350 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 15, 2018 in Java by samarth295
• 2,220 points
1,929 views
0 votes
1 answer

Convert String type to byte[] in Java

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,620 points
577 views
+2 votes
10 answers

Convert Java Byte Array - String - Byte Array

Character Stream Vs Byte Stream Character oriented are ...READ MORE

answered Sep 25, 2018 in Java by carldivin
• 180 points
9,808 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,190 points
872 views
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,910 points
5,408 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP