Java HashMap associative multi dimensional array can not create or add elements

hashmap 2d array java
how to iterate two dimensional arraylist in java
two dimensional array in java example program
how to add elements to 2d arraylist in java
hashmap with multiple values under the same key in java
java array of arrays
one key multiple values java
multidimensional array

Okay so I have spent several hours trying to wrap my head around this concept of a HashMap in Java but am just not able to figure it out. I have looked at many tutorials but none seem to address my exact requirement and I cannot get it to work.

I am trying to create an associative multi dimensional array in Java (or something similar) so that I can both save to and retrieve from the array with keys that are Strings.

This is how I would do it in PHP and explains it best what I am trying to do:

//loop one - assign the names
myArray['en']['name'] = "english name";
myArray['fr']['name'] = "french name";
myArray['es']['name'] = "spanish name";

//loop two - assign the description
myArray['en']['desc'] = "english description";
myArray['fr']['desc'] = "french description";
myArray['es']['desc'] = "spanish description";

//loop three - assign the keywords
myArray['en']['keys'] = "english keywords";
myArray['fr']['keys'] = "french keywords";
myArray['es']['keys'] = "spanish keywords";

//later on in the code be able to retrive any value similar to this
english_name = myArray['en']['name'];
french_name = myArray['fr']['name'];
spanish_name = myArray['es']['name'];

This is what I tried in Java but it is not working:

HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();

myArray.put("en" , put("name", "english name")); //gives me "cannot find symbol" at second put

myArray.put("en" , ("name", "english name")); //gives me "')' expected" after second comma

So I am sure its something simple that I am missing but please point it out because this is very frustrating!

Thanks

EDIT:

So here is some working code on how I implemented the answer I accepted:

import java.util.*;

HashMap<String, HashMap<String, String>> finalArray = new HashMap<String, HashMap<String, String>>();

String[] langArray = {"en","fr","de","no","es"};

//Initialize each language key ahead of time
for(String lang : langArray) { // foreach lang in langArray
  if (!finalArray.containsKey(lang)) {
    finalArray.put(lang, new HashMap<String, String>());
  }
}

//loop one - assign names
for(String lang : langArray) {
  String theName = lang + " name"; //go get the name from somewhere
  finalArray.get(lang).put("name", theName);
}

//loop two - assign description
for(String lang : langArray) {
  String theDesc = lang + " description"; //go get the description from somewhere
  finalArray.get(lang).put("desc", theDesc);
}

//loop three - assign keywords
for(String lang : langArray) {
  String theKeys = lang + " keywords"; //go get the keywords from somewhere
  finalArray.get(lang).put("keys", theKeys);
}

//display output
for(String lang : langArray) {
  System.out.println("LANGUAGE: " + lang);
  System.out.println(finalArray.get(lang).get("name"));
  System.out.println(finalArray.get(lang).get("desc"));
  System.out.println(finalArray.get(lang).get("keys"));
}

//example to retrieve/get values
String english_name = finalArray.get("en").get("name");
String french_desc = finalArray.get("fr").get("desc");

HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();

if (!myArray.containsKey("en")) {
    myArray.put("en", new HashMap<String, String>());
}
myArray.get("en").put("name", "english name");

In Java you have to be explicit about when you are creating an object. In this case first we check if there is already a HashMap object stored in our outer HashMap under the key "en". If not, we create an empty one.

Now to put a new value into it we have to first get it from the outer HashMap, then put the new value.

Values of Hash Map to be 2-dimensional array [Solved] (Java in , Java HashMap associative multi dimensional array can not create or add elements I can both save to and retrieve from the array with keys that are Strings. containsKey(lang)) { finalArray.put(lang, new HashMap<String,  To create achieve an associative array we can use the hashMap built-in class of java, as we have seen above examples. Recommended Articles. This is a guide to Associative Array in Java. Here we discuss Syntax, how to create an associative array in java along with examples and advantages.


Unfortunately, there's no concise syntax for constructing populated maps in Java. You'll have to write it out long-hand. A separate helper method can make it a little simpler:

HashMap<String, String> makeMap(String name, String desc, String keys) {
    HashMap<String, String> map = new HashMap<>();
    // Before Java 7, above must be: new HashMap<String, String>();
    map.put("name", name);
    map.put("desc", desc);
    map.put("keys", keys);
}

Then:

HashMap<String, HashMap<String, String>> myArray = new HashMap<>();
myArray.put("en",
    makeMap("english name", "english description", "english keywords"));
// etc.

You would retrieve it with:

english_name = myArray.get("en").get("name");

Nested algorithms for optimal reservoir operation and their , A "two dimensional array" is just an array of arrays. I tried the same way as any other Object but i can't add the values to the you can either place the entire array into the hash map or you can use the hashMap to create  multidimensional - two dimensional hashmap java Java HashMap associative multi dimensional array can not create or add elements (2) Unfortunately, there's no concise syntax for constructing populated maps in Java. You'll have to write it out long-hand.


HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();
HashMap<String, String> value = new HashMap<String, String>();
value.put("name", "English name");
value.put("desc", "English description");
value.put("keys", "English keywords");

myArray.put("en" , value);

value = new HashMap<String, String>();
value.put("name", "French name");
value.put("desc", "French description");
value.put("keys", "French keywords");

myArray.put("fr" , value);

Associative array/Creation, Define initial statext Trfrom the current episode, and the time step t. with an initial The Q (xt, at) representation in the third approach requires four dimensional dealing with this issue was to use the HashMap function supported in Java. The HashMap is a data structure used to implement an associative array: it can map  I have this array: ArrayList<ArrayList<Integer>> items = new ArrayList<ArrayList<Integer>>(); I want to create this array on a for cicle and the result must be something l


import java.util.HashMap;
public class Main
{
    public static void main(String[] args) {
        // Creating array
        HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();
        // Setting values
        for(int i=0; i<100;i++) {
            myArray.put("key1"+i, new HashMap<String, String>());
            myArray.get("key1"+i).put("key2"+i, "value"+i);
        }
        // Getting values
        for(int i=0; i<100; i++) {
        System.out.println(myArray.get("key1"+i).get("key2"+i));
        }
    }
}

Hash from two arrays, Because ActionScript does not have associative arrays in the normal sense, You can create them using a map literal and you can insert and remove items on Like Java, equals and hashCode are used to determine key uniqueness for Cannot / Do not need to instantiate the algorithm implementation (e.g, HashMap). Map<String, ArrayList<Node>> indexCategories = new HashMap<String, ArrayList<Node>>(); But in each time of the for, I dont know how to search in all keys of the Map, and add the new Node to the list, and if the key does no exists yet, create the new item inside the Map.


Arrays / Hash Maps - the Tcler's Wiki!, 63.1 REXX Style; 63.2 Java Collections Solution is at Associative_array/​Creation#BASIC256. Length, "Arrays are not same length."); Add throws an exception when a key already exists. value hash = map(zipEntries(keys, items​)); } In Common Lisp terminology, a vector is a one-dimensional array. Accessing Multi Dimensional Array Elements. In Java programming, We can use the index position to access the Java multi dimensional array elements. Using the index, we can access or alter/change every individual element present in the multi dimensional array. The index value of Multi Dimensional Array in Java starts at 0.


array - the Tcler's Wiki!, The reason this is an important concept up front is that Tcl does not provide a if you are careful you can simulate a multi-dimensional array like this: Be aware that array set will add elements to an array, but will not remove any of the old elements. That means: array set will create an array (hash map) using in order the  Each element of a multidimensional array is an array itself. For example, int[][] a = new int[3][4]; Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements, 2-dimensional Array. Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1.


5. Working with Arrays and Loops, To create multiple array keys and values in one operation, use array set. There are no multi-dimensional arrays in Tcl but they can be simulated by a naming Lars H: I might add that the reason that example 5 is more costly than DKF: Tcl arrays have a lot in common with Java's java.util.HashMap class, as to dicts. Similarly, a multi-dimensional array in Java usually has 3 or more defined indexes, but in reality, one particular row of elements have another multitude of elements defined on their names. Basically, you can have a 3×3 or a bigger matrix of elements to define a multi-dimensional array.