HashMap storing same values for all Keys

2019-09-20 06:26发布

I want to store different values for unique coordinates.I am using integer array to store those values in HashMap to corresponding coordinates but every key maps to last calculated value.

Code :

import java.util.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Solution
{

@SuppressWarnings("empty-statement")
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int m = in.nextInt();

    Integer[] ar = new Integer[3];

    Map<Point, Integer[]> map = new HashMap<>();

    for (int a0 = 0; a0 < m; a0++) {

        Point p = new Point(in.nextInt(), in.nextInt());
        int a = in.nextInt();
        int b = in.nextInt();

        if (map.containsKey(p)) {

            if (map.get(p)[2] < (a - b)) {
                ar[0] = a;
                ar[1] = b;
                ar[2] = a - b;
                map.put(p, ar);
            }
        } else {
            ar[0] = a;
            ar[1] = b;
            ar[2] = a - b;
            map.put(p, ar);
        }

    }
    Set<Entry<Point, Integer[]>> set = map.entrySet();
    List<Entry<Point, Integer[]>> list = new ArrayList<>(set);


    for (Map.Entry<Point, Integer[]> entry : list)
        System.out.println(entry.getKey() + " ==== " + Arrays.toString(entry.getValue()));



}
}

Input :

3 3

0 1 1 1

1 2 2 4

2 0 1 2

Result :

java.awt.Point[x=0,y=1] ==== [1, 2, -1]

java.awt.Point[x=1,y=2] ==== [1, 2, -1]

java.awt.Point[x=2,y=0] ==== [1, 2, -1]

1条回答
手持菜刀,她持情操
2楼-- · 2019-09-20 06:58

You only have one single instance of your ar array and you are just storing it under multiple keys. You need to create a new array instance each time:

import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class Solution {

    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            int n = in.nextInt();
            int m = in.nextInt();

            Map<Point, Integer[]> map = new HashMap<>();

            for (int a0 = 0; a0 < m; a0++) {

                Point p = new Point(in.nextInt(), in.nextInt());
                int a = in.nextInt();
                int b = in.nextInt();
                // New array for each key
                Integer[] ar = new Integer[3];

                if (map.containsKey(p)) {

                    if (map.get(p)[2] < (a - b)) {
                        ar[0] = a;
                        ar[1] = b;
                        ar[2] = a - b;
                        map.put(p, ar);
                    }
                } else {
                    ar[0] = a;
                    ar[1] = b;
                    ar[2] = a - b;
                    map.put(p, ar);
                }

            }
            Set<Entry<Point, Integer[]>> set = map.entrySet();
            List<Entry<Point, Integer[]>> list = new ArrayList<>(set);

            for (Map.Entry<Point, Integer[]> entry : list)
                System.out.println(entry.getKey() + " ==== " + Arrays.toString(entry.getValue()));

        }
    }
}

Output:

java.awt.Point[x=0,y=1] ==== [1, 1, 0]
java.awt.Point[x=1,y=2] ==== [2, 4, -2]
java.awt.Point[x=2,y=0] ==== [1, 2, -1]

(Also note that n isn't actually read anywhere.)

For a better understanding consider this simplified example:

import java.util.HashMap;
import java.util.Map;

public class SimpleSolution {

    public static void main(String[] args) {
        Map<Integer, Integer[]> map = new HashMap<>();
        Integer[] arr = new Integer[1];

        arr[0] = 1;
        map.put(1, arr);
        System.out.println(map);
        System.out.println(map.get(1)[0]);
        arr[0] = 2;
        map.put(2, arr);
        System.out.println(map);
        System.out.println(map.get(1)[0]);
    }
}

Output:

{1=[Ljava.lang.Integer;@2a139a55}
1
{1=[Ljava.lang.Integer;@2a139a55, 2=[Ljava.lang.Integer;@2a139a55}
2

What you can see here is

  1. Key 1 and 2 both have the same array reference
  2. After setting arr[0] to 2, the value for key 1 has also changed in the map
查看更多
登录 后发表回答