Home > Software design >  How to create a matrix with key and value in java
How to create a matrix with key and value in java

Time:05-07

I have this table with these values

enter image description here

I have this code in java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestHashes {

    public static void main(String args[]){
        Map<String,String> myMap1 = new HashMap<>();

        List<Map<String , String>> myMap  = new ArrayList<>();

        myMap1.put("URL", "URL1");
        myMap1.put("CRC", "CRC1");
        myMap1.put("SIZE", "Size1");
        myMap1.put("PROGRESS", "Progress1");

        myMap.add(0,myMap1);

        myMap1.put("URL", "URL2");
        myMap1.put("CRC", "CRC2");
        myMap1.put("SIZE", "Size2");
        myMap1.put("PROGRESS", "Progress2");

        myMap.add(1,myMap1);

        for (Map<String, String> map : myMap) {
            System.out.println(map.get("URL"));
            //System.out.println(map.get("CRC"));
            //System.out.println(map.get("SIZE"));
            //System.out.println(map.get("PROGRESS"));
        }
        System.out.println(myMap);
    }
}

Result

URL2
URL2
[{CRC=CRC2, SIZE=Size2, PROGRESS=Progress2, URL=URL2}, {CRC=CRC2, SIZE=Size2, PROGRESS=Progress2, URL=URL2}]

The first record is lost and the last record is saved

How can I save all the records?

and it is possible to implement access to a table cell

similar to row and column for example myMap[0]["SIZE"]?

CodePudding user response:

Looks like you're saving the same Map into index 0 and index 1:

myMap.add(0,myMap1);
...
myMap.add(1,myMap1);

So when you're trying to input for the second row, since they are the same keys as the first row keys ("URL", "CRC", etc.), it overwrites the values for those keys of the first row.

What you could do is use Map.of() to create a new immutable map for each row.

For example:

List<Map<String , String>> myMap  = new ArrayList<>();
myMap.add(0, Map.of("URL", "URL1","CRC", "CRC1",... ));
myMap.add(1, Map.of("URL", "URL2","CRC", "CRC2",... ));
...

and it is possible to implement access to a table cell similar to row and column for example myMap[0]["SIZE"]?

Yes. Just call it similarly: ArrayList.get(0).get("SIZE")

CodePudding user response:

What you are doing is a common mistake for juniors (I also saw it from seniors as well).

Take a look what references are and how they work (and maybe in addition to method calls, the topic call by reference, call by value): https://www.geeksforgeeks.org/types-references-java/

So what went wrong? You were trapped in how references and maps work.

You've created a map-object, then you set values with keys:

innerMap['foo'] = "bar";

This means that the map holds a value 'bar' on index 'foo'. You add this object (per reference, thus per memory address to the outer map):

outerMap.put(0, innerMap);

so outerMap holds a referents (points) to innerMap Object which has value 'bar' on index 'foo'.

Now you manipulating the exact same object innerMap on the exact same index 'foo':

innerMap['foo'] = "bumsti";

What here happens is that you've overwritten the old value 'bar' with 'bumsti'.

After that you're adding the exact same reference as before (innerMap) to outerMap on index 1.

outerMap.put(1, innerMap);

What you've got is an outerMap[0]->innerMap[foo]: "bumsti" and outerMap[1]->innerMap[foo]: "bumsti".

To avoid that, don't reuse your object since you will always write to the same memory.

Do:

Map<Integer, String> outerMap = new HashMap<>();
Map<String, String> innerMap1 = new HashMap<>();
innerMap1["foo"] = "bar";
outerMap.put(0, innerMap1);
Map<String, String> innerMap2 = new HashMap<>();
innerMap1["foo"] = "bumsti";
outerMap.put(1, innerMap2);

This is what you wanted. Try it out!

  • Related