Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pascal's set types analogue for Java

Tags:

java

pascal

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))
  ...
like image 702
Andrej Avatar asked Apr 09 '26 19:04

Andrej


2 Answers

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

like image 79
Brad Avatar answered Apr 12 '26 08:04

Brad


Unfortunately, there are no such beautiful construct in Java. But apache-commons provides a Range class which may suite your needs

like image 26
yatskevich Avatar answered Apr 12 '26 07:04

yatskevich