1

I am trying to understand a programming question where I am given a list of words for example, (apple, orange, car, can, fuzzy) I would get a hashmap with the length for the key and linkedlist of words for value. For example,

(3, {car, can})
(5, {apple, fuzzy})
(6, {orange})

How could I build this? I am relatively new to Java and only knows how to read string input and get the length of each string. But very unfamiliar with hashmaps. Could someone guide me in the right direction?

0

2 Answers 2

1

As Tim Biegeleisen said, here's an approach if you're using Java 8:

import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.*;

public class CollectByLength {
    public static void main(String[] args) {
        Map<Integer, List<String>> map = Stream.of("apple", "orange", "car", "can", "fuzzy")
                                        .collect(groupingBy(String::length));

        System.out.println(map); //prints {3=[car, can], 5=[apple, fuzzy], 6=[orange]}
    }
}

If you care about the List implementation for some reason, the above solution doesn't provide any guarantee on the list implementation. From the doc:

There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List objects returned.

But it's also possible to specify the List implementation you need (LinkedList here)

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.*;

public class CollectByLength {
    public static void main(String[] args) {
        Map<Integer, List<String>> map = Stream.of("apple", "orange", "car", "can", "fuzzy")
                                        .collect(groupingBy(String::length, toCollection(LinkedList::new)));

        System.out.println(map); //prints {3=[car, can], 5=[apple, fuzzy], 6=[orange]}
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a fairly concise Java 7 approach:

List<String> words = Arrays.asList("apple", "orange", "car", "can", "fuzzy");
Map<Integer, List<String>> map = new HashMap<>();

for (String word : words) {
    List<String> wordList = map.get(word.length());
    if (wordList == null) {
        wordList = new ArrayList<String>();
        wordList.add(word);
        map.put(word.length(), wordList);
    }
    else {
        wordList.add(word);
    }
}

I will leave it up the experts to give an even leaner Java 8 solution.

5 Comments

This is what I tried but the hashmap I am looking for needs to be Map<Integer, ArrayList<String>> where the arraylist is a bunch of words of length integer
The list returned by singletonList() is immutable, so that code won't work.
@JonathanBishop In question you say "linkedlist" by now you say ArrayList. Which is it?
The list returned by Arrays.asList() is fixed-length, so that code won't work.
No need to be an expert to give a Java 8 solution, or ?

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.