views:

126

answers:

2

Hello there.

Is there something like C#/.NET's

IEnumerable<int> range = Enumerable.Range(0, 100); //.NET

in Java?

A: 

You could subclass an Arraylist to achieve the same:

public class Enumerable extends ArrayList<Integer> {   
   public Enumerable(int min, int max) {
     for (int i=min; i<=max; i++) {
       add(i);
     }
   }    
}

Then use the iterator to get a sequence of Integers from min to max (both including)

EDIT

As sepp2k mentioned - the solution above is quick, dirty and practical but has some serious withdraws (not only O(n) in space while it should have O(1)). For a more serious emulation of the C# class I'd rather write a custom Enumerable class that implements Iterable and a custom iterator (but not here and now ;) ).

Andreas_D
Thanks Andreas. So there isn't?
Simon
Not in the basic Java API, afaik, but there maybe something in one of the numerous apache commons libraries. That's where I would look first.
Andreas_D
Ok... add your comment to your answer, so I can upvote it and mark it as answer. :)
Simon
Note that this solution will actually take up space proportional to the length of the range (unlike Enumerable.Range which only stores the start and the end value), which might not be acceptable for large ranges.
sepp2k
There are much more withdraws (and I wouldn't use that code in own projects) but the OP didn't tell why he wants to use/emulate the Enumerable type from C#. Better requirements, better solutions.
Andreas_D
@sepp2k: No, the memory usage is only deferred. As soon as you call `GetEnumerator` or use it in a `foreach`, the `IEnumerable` is populated with actual values. MSDN docs: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx
R. Bemrose
@R.Bemrose: I've just run `foreach(int i in Enumerable.Range(0, 1000000000)) {/*...*/}` without any visible increase in memory consumption, so I'm convinced that it doesn't store all the values between 0 and 1000000000 in memory.
sepp2k
+2  A: 

There is no built in support for this in Java, however it is very easy to build it yourself. By and large the Java APIs provide all the bits you need for this kind of functionality but do not combine them out of the box.

Java takes the approach that there is an infinite number of ways to combine things so why privilege a few combinations over others. With the right set of building blocks everything else can be easily built (this is also the Unix philosophy).

Other language API's (C# and Python for example) take a more measured view, they do pick a few things to make really easy, but still allow more esoteric combinations.

A typical example of the problem with the Java approach can be seen in the Java IO library. The canonical way to create a text file for output is:

BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));

The Java IO library uses the Decorator Pattern which is a really good idea for flexibility, but surely more often than not you need a buffered file? Compare that to the equivalent in Python, which makes the typical use case really simple:

out = file("out.txt","w")
Tendayi Mawushe
Yeah, you're right. Just wanted to know if there's something built-in.
Simon