Iterators
Iterators framework is the implementation of lazy collections functional based on Iterator and Iterable interfaces. Requires Java 1.5 +
How do I use it?
You can download the latest build at: https://github.com/Megaprog/Iterators/releases
Or use it as a maven dependency:
<dependency>
<groupId>org.jmmo</groupId>
<artifactId>iterators</artifactId>
<version>2.0</version>
</dependency>
What can I do with Iterators?
- Create Empty, Singleton or Optional Iterable
- Merge two or more iterators into one
- Filter iterator values
- Map iterator values
- Reduce iterator values to produce a single result
- Repeat a value by creation iterator contains this value some number of times
- Produce ranges contains the sequence of integer or double values
- Create Java Collection, List or Set from any Iterable
Examples
Obtain empty Iterable:
Iterable<String> empty = Iterators.<String>empty();
Obtain Iterable contains a single element:
Iterable<String> singleton = Iterators.singleton("jmmo");
Create iterator contains repeated value some number of times (will contains 1, 1, 1, 1, 1):
Iterable<Integer> repeat = Iterators.repeat(1, 5);
Create iterator contains the range of integer or double values (will contains 1, 2, 3):
Iterable<Integer> range = Iterators.range(1, 3);
Merge two Iterable's in one (will contains 1, 2, 4, 5):
Iterable<Integer> merged = Iterators.merge(Arrays.asList(1, 2), Arrays.asList(4, 5));
Make the flat Iterable from an Iterable of Iterable's (will contains 1, 2, 0, 3, 4):
Iterable<Integer> flat = Iterators.flat(Arrays.asList(Iterators.range(1, 2), Iterators.singleton(0), Iterators.range(3, 4)));
Filter the Iterable (will contains even numbers from 2 to 100):
Iterable<Integer> even = Iterators.filter(Iterators.range(1, 100), new Filter<Integer>() {
@Override
public boolean test(Integer integer) {
return integer % 2 == 0;
}
});
Map the Iterable (will contains "1", "2", "3"):
Iterable<String> int2String = Iterators.map(Iterators.range(1, 3), new Mapper<Integer, String>() {
@Override
public String mapper(Integer integer) {
return Integer.toString(integer);
}
});
Reduce Iterable to produce a single result (will be 6):
Reducer<Integer> reducer = new Reducer<Integer>() {
@Override
public Integer accumulate(Integer previous, Integer current) {
return previous + current;
}
};
Mapper<String, Integer> mapper = new Mapper<String, Integer>() {
@Override
public Integer mapper(String s) {
return s.length();
}
};
int length = reducer.reduce(0, mapper.map("AB", "C", "DEF", ""));
Create Collection, List or Set from any Iterable:
Collection<Integer> rangeCollection = Iterators.toCollection(Iterators.range(1, 3), new Vector<Integer>());
List<Integer> rangeList = Iterators.toList(Iterators.range(1, 3));
Set<Integer> rangeSet = Iterators.toSet(Iterators.range(1, 3));