I encountered a problem when I use TreeMap.
Map<Integer, Integer> a = new TreeMap<Integer, Integer>(); a.put(5,1); a.put(3,2); a.put(4,3); int target = 7; System.out.println(target - a.get(5)); //line 6 for(Map.Entry b : a.entrySet()){ System.out.println(target - b.getValue()); //line 8 }
The code above gave me an compile error. However, when I change the line 8 to this:
Map<Integer, Integer> a = new TreeMap<Integer, Integer>(); a.put(5,1); a.put(3,2); a.put(4,3); int target = 7; System.out.println(target - a.get(5)); //line 6 for(Map.Entry b : a.entrySet()){ System.out.println(target - (int) b.getValue()); //line 8 }
Then it works. Could anyone give me some ideas why I don't need any change in line 6 but need convert an Integer to int in line 8?
In java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting. In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value.
Convert Int to Integer Using the Integer. valueOf() Method in Java. This is another that we can use to convert an int to an Integer in Java. Here, we used valueOf() method of the Integer class.
We can convert long to int in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype).
In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
You ignored the "raw type" warning in the for
statement. It should be:
for(Map.Entry<Integer,Integer> b : a.entrySet()) { ...
The raw type would cause getValue()
to return Object
. If you provide the type parameters then the compiler knows it will return Integer
, and this will get unboxed automatically.
There are more than one operations that are underneath (int) b.getValue()
. First getValue()
returns Object
and then that is casted to Integer
which is then unboxed to int
. a.get()
in it's own immediately returns Integer
since you declared a with Integer in <> (see https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object) it returns the V type).
The target - b.getValue()
didn't compile because it was int - Object
operation which is not defined for operator -
. That is why you have to do cast to (int)
.
Following wont work even though b is referring to the object that is Integer
.
Integer a = 1; Object b = a; System.out.println(3 - b); // compile time error "bad operand types for binary operator '-'"
Following works
Integer a = 1; Object b = a; System.out.println(3 - a);
Also works
Integer a = 1; Object b = a; System.out.println(3 - (int) b); //this is when you say to compiler not to worry since you are sure that object reference refers to the object that is Integer.
Though if at runtime b doesn't refer to int the cast will fail. Even if it did compile in the first place.
Integer a = 1; String s = "shouldn't work at runtime"; Object b = s; System.out.println(3 - (int) b); //this will compile but fail at runtime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With