Java – about map Confusion of containsvalue method
•
Java
I have the following program, I have a HashMap The key of HashMap is a simple integer and the value is an array of integers The plan is as follows:
Map<String,int []> myMap = new HashMap<String,int []>();
myMap.put("EvenNumbers",new int[]{2,4,6,8,10,12,14,16,18,20});
myMap.put("OddNumbers",new int[]{1,3,5,7,9,11,13,15,17,19});
myMap.put("DivisibleByThree",new int[]{3,18});
myMap.put("DivisibleByFive",new int[]{5,20});
int[] array = new int[]{1,19};
System.out.println(myMap.containsKey("EvenNumbers"));
System.out.println(myMap.containsKey("OddNumbers"));
//The following two lines produce a false output. Why ?
System.out.println(myMap.containsValue(new int[]{5,20,20} ));
System.out.println(myMap.containsValue(array));
The following code generates a true value
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1,"tutorials");
newmap.put(2,"point");
newmap.put(3,"is best");
// check existence of value 'point'
System.out.println("Check if value 'point' exists: " +
newmap.containsValue("point"));
Why is that? What's wrong with me? What am I missing? In both cases, I feel like I'm doing the same thing I'm new to the Java environment, so I'm confused Please help me clarify these concepts
Solution
This is because Boolean x = New Int [] {5,20} equals(new int [] {5,20}); Return false One solution is to use Java nio. Intwrapper, try this
map.put("a1",IntBuffer.wrap(new int[]{ 5,20 }));
boolean equals = map.containsValue(IntBuffer.wrap(new int[]{ 5,20 }));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
