1

cash range ------------> tax

5,000 - 30,000--------> 700

30,001- 60,000-------- > 1,000

60,001 - 125,000 -------> 1,600

125,001 - 250,000 ---------> 3,000

250,001 - 500,000 -------- > 5,000

500,001 - 1,000,000---------> 9,000

How I can implement this in a efficient way? What if I don't want to use IF/Else for that.

Waqas
  • 4,411
  • 6
  • 27
  • 31
  • 1
    If you are using Java 6 or above use java.util.NavigableMap – Bharat Sinha Jul 25 '12 at 09:57
  • Possible duplicate of [In Java,Using switch statement with a range of value in each case?](https://stackoverflow.com/questions/10873590/in-java-using-switch-statement-with-a-range-of-value-in-each-case) – gnat Aug 16 '19 at 10:10

1 Answers1

6

This is an excellent case for a NavigableMap implementation. You can use its ceilKey method to directly get what you need. Your keys will be the upper bounds of the ranges and the values will be the tax amounts.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436