Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively Create Hasmaps in Java

I'm trying to create a new HashMap for each document I have as input. In pseudeocode I can think of something like:

For(eachInputDoc)
{
    Map<String, String> mapInputNumber = new HashMap<String, String>;
}

So that for 4 documents you would have:

mapInput1
mapInput2
mapInput3
mapInput4

How can I accomplish this?

like image 910
chrstahl89 Avatar asked Nov 25 '25 09:11

chrstahl89


2 Answers

It looks like you're trying to declare variables dynamically. You can't do that in Java - the variables themselves are determined at compile time. However, you could create a list:

List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
for (Document doc : docs)
{
    Map<String, String> map = new HashMap<String, String>();
    // Populate map from doc
    maps.add(map);
}
like image 138
Jon Skeet Avatar answered Nov 26 '25 21:11

Jon Skeet


I suggest you make an ArrayList of HashMaps.

like image 33
S.L. Barth Avatar answered Nov 26 '25 22:11

S.L. Barth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!