HashMap为所有Keys存储相同的值 [英] HashMap storing same values for all Keys

查看:160
本文介绍了HashMap为所有Keys存储相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为唯一坐标存储不同的值。我使用整数数组将这些值存储在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)
{
扫描仪输入=新扫描仪(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
Point p = new Point(in.nextInt(),in.nextInt());
int a = in.nextInt();
int b = in.nextInt(); (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); (Map.Entry< Point,Integer []>条目:list)


$ b System.out.println(entry.getKey()+==== + Arrays.toString(entry.getValue()));


$ b}
}

输入:

<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]

解决方案

您只有 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
Point p = new Point(in.nextInt(),in.nextInt());
int a = in.nextInt();
int b = in.nextInt();
//每个键的新数组
Integer [] ar = new Integer [3]; (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); (Map.Entry< Point,Integer []>条目:列表)

b
System.out.println(entry.getKey()+====+ Arrays。的toString(entry.getValue()));



$ b code
$ b

输出:

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


$ / code $ / pre
$ b $输出:

  {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的值在地图中也发生了变化


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]

解决方案

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

这篇关于HashMap为所有Keys存储相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆