Andrej
Andrej

Reputation: 1736

Pascal's set types analogue for Java

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))
  ...

Upvotes: 3

Views: 285

Answers (2)

Brad
Brad

Reputation: 15879

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

Upvotes: 1

yatskevich
yatskevich

Reputation: 2093

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

Upvotes: 1

Related Questions