简体   繁体   中英

Replace values in Hashmap that contains multiple values per key

I need some help replacing values in a HashMap. The HashMap contains multiple values per key, the values are added from an ArrayList. What I want to do is to replace an element within a key. For example, the user inputs key 1 and index 2, this would be the values of that key [1] = [A,B,C,D], index 2 would be the letter "C" and I want to replace it with an "X" so that the output would be [1] = [A,B,X,D], this would be inside a while loop.

 HashMap<String, List<String>> seatMap = new HashMap<String, List<String>>();

 seats.add("A");
 seats.add("B");
 seats.add("C");
 seats.add("D");

String k = null;     
for(int i=1; i<8;i++){
     k = Integer.toString(i);
     seatMap.put(k, seats);
}

 for(Map.Entry<String, List<String>> entry : seatMap.entrySet()){
     String key= entry.getKey();
     List<String> values = entry.getValue();
     System.out.println(key+"  "+values.get(0)+values.get(1)+"  "+values.get(2)+values.get(3));
 }

Output:

  • 1 AB CD
  • 2 AB CD
  • 3 AB CD
  • 4 AB CD
  • 5 AB CD
  • 6 AB CD
  • 7 AB CD

I have tried to put all values from the selected key into an ArrayList, then replace the value in the ArrayList, then put the ArrayList in the selected key and it should replace the values of the key.

 ArrayList temp = new ArrayList();
 temp.addAll(seatMap.get(row));
 temp.set(inputKey, "X");
 seatMap.put(row, temp);

     for(Map.Entry<String, List<String>> entry : seatMap.entrySet()){
         String key= entry.getKey();
         List<String> values = entry.getValue();

            System.out.println(values);

     }

If the user selects key: 1 and value A, this is the result.

  • [X, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]

But if the user then selects key: 2 and value B, the values of key 1 become the values of key 2

  • [X, X, C, D, A, B, C, D]
  • [X, X, C, D, A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]
  • [A, B, C, D]

I want an output like this

  • [X, B, C, X]
  • [A, X, C, D]
  • [A, B, C, D]
  • [A, B, X, X]
  • [A, X, C, D]
  • [A, B, C, D]
  • [A, B, C, X]

It would put and X depending on what the user selects. I have also tried to clear the ArrayList after is added into the HashMap, but it remove its values and it doesn't add anything to the HashMap.

First of all, as @earcam said in the comments, all values in seatMap are shared as you use the same instance of list . If you replace string in one list, it will replace in all of the lists.

So, move the List<String> seatMap ... inside the for loop

    for(int i=1; i<8;i++){
        List<String> seats = new ArrayList<String>();
        seats.add("A");
        seats.add("B");
        seats.add("C");
        seats.add("D");

        k = Integer.toString(i);
        seatMap.put(k, seats);
    }

To replace the element, you can use set . From the doc about set,

Replaces the element at the specified position in this list with the specified element.

int key = 1, index = 2;
seatMap.get(Integer.toString(key)).set(2, "X");

For the sake of the example, I am hardcoding, key and index. You can tweak the code based on your requirements.

Output

1 AB XD

2 AB CD

3 AB CD

4 AB CD

5 AB CD

6 AB CD

7 AB CD

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM