Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Maps Java Maps Java Maps Quiz

how would I initialize a hashmap with a string array and a string.

how would I initialize a hashmap in java with a string array and a string?

1 Answer

Martin Klestil
Martin Klestil
5,520 Points

I hope this helps you

import java.util.HashMap;

public class MyHashMap {
    private static HashMap<String, String[]> subjects;

    public static void main(String[] args) {
        subjects = new HashMap<String, String[]>();
        subjects.put("calculus", new String[] { "math", "logic" });
        subjects.put("chemisty", new String[] { "ions", "electrons" });
        subjects.put("biology", new String[] { "life", "bacteria" });
        for (String s : subjects.get("biology")) {
            System.out.println(s);
        }

    }
}

https://stackoverflow.com/questions/9898749/using-string-array-in-hashmap-java