HashMap为所有Keys存储相同的值

时间:2017-04-13 19:32:45

标签: java-8 hashmap

我想为唯一坐标存储不同的值。我使用整数数组将HashMap中的值存储到相应的坐标,但每个键映射到最后计算的值。

代码:

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()));



}
}

输入:

3 3

0 1 1 1

1 2 2 4

2 0 1 2

结果:

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 个答案:

答案 0 :(得分:0)

您的ar数组只有一个单一实例,而您只是将其存储在多个键下。您需要每次都创建一个新的数组实例:

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()));

        }
    }
}

输出:

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]

(另请注意n实际上并未在任何地方阅读过。)

为了更好地理解,请考虑以下简化示例:

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]);
    }
}

输出:

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

你在这里看到的是

  1. 密钥1和2都具有相同的数组引用
  2. arr[0]设置为2后,键1的值也在地图中发生了变化