Skip to content

Java ListIterator of Iterators that supports both next and previous

Notifications You must be signed in to change notification settings

gravitate-dev/InterleavingIterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Two Way IterleavingIterator

Infographic

I wanted to solve iterating across multiple iterators with a round-robin fashion.

Features

  • Iterating with next()
  • Iterating with prev()
  • Check prev with hasPrev()
  • Check next with hasNext()

Usage

List<Integer> foo = new ArrayList<>();
foo.add(1);

List<Integer> bar = new ArrayList<>();
bar.add(2);

List<Integer> baz = new ArrayList<>();
baz.add(3);
baz.add(4);
baz.add(5);

List<Iterator<Integer>> myIters = new ArrayList<>();
myIters.add(foo.listIterator());
myIters.add(bar.listIterator());
myIters.add(baz.listIterator());

InterleavingIterator<Integer> ill = new InterleavingIterator(myIters);
while(ill.hasNext()) {
    System.out.println(ill.next());
}
while(ill.hasPrev()) {
    System.out.println(ill.prev());
}

would produce

1
2
3
4
5
5
4
3
2
1

About

Java ListIterator of Iterators that supports both next and previous

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages