1

I have a case in which I need to store huge numbers into key and value. I tried:

HashMap<String, double> map = new HashMap<String, double>();
map.put("Type1", 1267650600228229401496703205376);

I get syntax error. Is there a way to store key and value with huge number?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

1

You can try this:

HashMap<String, Double> map1 = new HashMap<String, Double>();
map1.put("Type1", 1267650600228229401496703205376D);

or

HashMap<String, BigDecimal> map2 = new HashMap<String, BigDecimal>();
map2.put("Type1", new BigDecimal("1267650600228229401496703205376"));
insan-e
  • 3,883
  • 3
  • 18
  • 43
  • I tried with Double but I need to cast the numbers every time The method put(String, Double) in the type HashMap is not applicable for the arguments (String, int) – Peter Penzov Aug 02 '18 at 19:29
  • 1
    Using a `Double` to hold really big integers is a really bad idea. If you find yourself switching to double because long long was too small, you're doing something wrong – Fureeish Aug 02 '18 at 19:30
  • @Fureeish OP said huge numbers, didn't specify they were integers. Also, his example shows doubles... I agree, of course, if those are integers use `Long` and/or `BigInteger`. – insan-e Aug 02 '18 at 19:32
0

Please read about BigDecimal It works with big double values and supports needed for you accuracy. Also you can use BigInteger for integer values.

There are so many method for working with big values, also compareTo() for comparing.

lord_hokage
  • 189
  • 7
0

Use BitInteger if your numbers won't be insanely large like your example, or just store it as a String.

HashMap<String, BitInteger> map = new HashMap<>();
map.put("Type1", BigInteger.valueOf(12676506));
zbys
  • 453
  • 2
  • 8