Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert array into hashtable w/o initializing in Java?

I have a hashmap initialized as follows:

Hashmap<String[][], Boolean> tests = new Hashmap<String [][], Boolean>();

I would like to insert into tests without having to initialize the key:

tests.put({{"a"}, {"a"}}, true);

However, Java doesn't seem to let me do this. It works if I do it like this:

String[][] hi = {{"a"}, {"a"}};
tests.put(hi, true);

Is there any way to avoid the latter and get the former working?

Can someone also explain the reasoning behind this error?

Thanks

like image 920
Popcorn Avatar asked Nov 29 '25 09:11

Popcorn


1 Answers

Yes, you can write like this:

tests.put(new String[][] {{"a"}, {"a"}}, true);

This is often referred to as an anonymous array or a just-in-time array.

like image 92
Keppil Avatar answered Dec 02 '25 00:12

Keppil