Pascal has a feature of set types. It allows nice constructs like this:
if i in [5..10] then
...
Are there any similar things in Java?
I came up only with this ugly construction that doesn't accept intervals:
if ((new HashSet<Integer>(Arrays.asList(new Integer[]{5,6,7,8,9,10}))).contains(i))
...
Yes you're right. You need an implementation of a Set in Java and have to populate it yourself with a loop if you want a non-sequential list of numbers.
Also, Java does not support the contruct of a Range. Other JVM laguages like Groovy and Scala however do.
This post may add some more colour
Unfortunately, there are no such beautiful construct in Java. But apache-commons provides a Range class which may suite your needs
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