Java Mappings and Primitives?

Java Autoboxing allows to create maps on Long, Integer, Double and then operate them using primitive values. For example: java.util. HashMap map = new java.util.HashMap(); map.

Put("one", 1); // 1 is an integer, not an instance of Integer Update If you want to store in one map different primitive types, you can to it by making a Map map = new HashMap(); map. Put("one", 1); map. Put("two", 2.0); map.

Put("three", 1L); for(String k:map.keySet()) { Number v = map. Get(k); System.err. Println(v + " is instance of " + v.getClass().getName() + ": " + v); }.

Java Autoboxing allows to create maps on Long, Integer, Double and then operate them using primitive values. For example: java.util. HashMap map = new java.util.HashMap(); map.

Put("one", 1); // 1 is an integer, not an instance of Integer Update If you want to store in one map different primitive types, you can to it by making a Map. Allows to store values of BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short (and AtomicLong, AtomicInteger). Here is an example: Map map = new HashMap(); map.

Put("one", 1); map. Put("two", 2.0); map. Put("three", 1L); for(String k:map.keySet()) { Number v = map.

Get(k); System.err. Println(v + " is instance of " + v.getClass().getName() + ": " + v); }.

Say for instance – James Andino Dec 9 '10 at 15:31 @Doodle you should make a base class Vehicle and extend it in Boat, Car and then make . – khachik Dec 9 '10 at 15:34 1 Yes, extract a supertype Vehicle and make it a Map or Map (the Generics syntax always confuses me) – ivy Dec 9 '10 at 15:35 I was considering strings being a value that could be useful to pass – James Andino Dec 9 '10 at 15:35 1 There's a supertype of Number and Strings.It's called Object! – ivy Dec 9 '10 at 15:36.

If you need the value to be a primitive for performance reasons, you can use TObjectIntHashMap or similar. E.g. TObjectIntHashMap map = new TObjectIntHashMap(); map.

Put("key", 10); int value = map. Get("key"); One difference with Map is that the values are of type int primitive rather than Integer object.

You can do the following: Map map = new HashMap() Then operations like: map. Put("One", 1); will work. The primitive 1 will get auto-boxed into an Integer.

Likewise: int I = map. Get("One"); will also work because the Integer will get auto-unboxed into an int. Check out some documentation on autoboxing and autounboxing.

1 Map is an interface. – khachik Dec 9 '10 at 15:10 Map is aninterface .. – Jigar Joshi Dec 9 '10 at 15:11 @khacik, fixed. Thanks for pointing that out.

– Justin 'jjnguy' Nelson Dec 9 '10 at 15:11 @org, thanks for pointing out my typo. Fixed. – Justin 'jjnguy' Nelson Dec 9 '10 at 15:11.

Google for "Java Primitive Maps" and you will find some specialised types which avoid the need for autoboxing. An example of this is the "fastutil" library: fastutil.dsi.unimi.it/ However, in general you should do fine with autoboxing as mentioned in other answers.

Thats a bump for showing me Fast util thats something that may be usefull to me thank you. – James Andino Dec 9 '10 at 15:34.

You can't have a primitive as key or value in Map interface. Instead you can use Wrapper classes, like Integer, Character, Boolean and so on. Read more on wiki.

Autoboxing should take care of that if java 1.5 onwards is used so you don't need to wrap the primitives manually – dimitrisli Dec 9 '10 at 15:14.

You would use their boxed counterpart. Map map = new HashMap(); Integer is an immutable boxed type of the primitive int. There are similar Short, Long, Double, Float and Byte boxed types.

Every primitive has a wrapper class, like java.lang. Long for long. So you can map the the wrapper class to Stringand, if you use Java 1.5+, simply put primitives to the map: Map map = new HashMap(); map.

Put("key", 10); int value = map. Get("key"); // value is 10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions