1

I'm struggling with a java HashMap. I want to return translationList as an array with strings. Example: word : "translated word".

Main class:

public static void main(String[] args) {
 Dictionary dictionary = new Dictionary();
 dictionary.add("apina", "monkey");
 dictionary.add("banaani", "banana");
 dictionary.add("cembalo", "harpsichord");
 ArrayList<String> translations = dictionary.translationList();
 for(String translation: translations) {
 System.out.println(translation);
 }
}

Dictionary class:

private HashMap<String, String> dictionary = new HashMap<String, String>();
public Dictionary(){};
public String translate(String word){
 if(dictionary.containsKey(word)){
 return dictionary.get(word);
 }
 return null;
}
public void add(String word, String translation){
 dictionary.put(word,translation);
}
public int amountOfWords() {
 return dictionary.size();
}
public ArrayList<String> translationList(){
 for ( String key : dictionary.keySet() ) {
 if(translationList().size()<dictionary.size()){
 translationList().add(key+" = "+dictionary.get(key));
 }
 }
 return translationList();
}

Java returns:

Exception in thread "main" java.lang.StackOverflowError
at java.base/java.util.HashMap$KeyIterator.<init>(HashMap.java:1515)
at java.base/java.util.HashMap$KeySet.iterator(HashMap.java:917)
at Dictionary.translationList(Dictionary.java:21)
at Dictionary.translationList(Dictionary.java:22)

Thanks for help :)

galmeriol
4614 silver badges15 bronze badges
asked May 21, 2018 at 12:19
2
  • 2
    You are calling your translationList() method instead of creating an actual list and adding to it. Why? Commented May 21, 2018 at 12:22
  • If next time you include a runnable code, (with imports and class structure) help will be faster :) Commented May 21, 2018 at 12:27

2 Answers 2

5

Your translationList method is calling itself, leading to infinite recursive calls and StackOverflowError.

You probably intended to write something like:

public List<String> translationList(){
 List<String> list = new ArrayList<>();
 for ( String key : dictionary.keySet() ) {
 list.add(key+" = "+dictionary.get(key));
 }
 return list;
}

or

public List<String> translationList(){
 List<String> list = new ArrayList<>();
 for ( Map.Entry<String,String> entry : dictionary.entrySet() ) {
 list.add(entry.getKey()+" = "+entry.getValue());
 }
 return list;
}

You have to first create an ArrayList and then add Strings to it. Besides that, the if (translationList().size()<dictionary.size()) condition is pointless, even without the recursive call.

answered May 21, 2018 at 12:22
1

You're having a recursive call with return translationList();. This is probably accidental. Your code was probably intended to look something like:

public ArrayList<String> translationList(){
 List<String> translationList = new ArrayList<>();
 for ( String key : dictionary.keySet() ) {
 if(translationList().size()<dictionary.size()){
 translationList.add(key+" = "+dictionary.get(key));
 }
 }
 return translationList();
}

But an even simpler implementation can avoid extra lookups:

public List<String> translationList(){
 return dictionary.entrySet()
 .stream()
 .map(entry -> entry.getKey() + " = " + entry.getValue())
 .collect(Collectors.toList());
}
answered May 21, 2018 at 12:29
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.