Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Iterator; import com.google.common.collect.AbstractIterator; import com.google.common.collect.Iterators; public class Main { public static <T> Iterable<T> openIntervalIterable(Iterable<T> iterable, T start, T end) { return () -> openIntervalIterator(iterable.iterator(), start, end); } /** * Returns iterator which contains only open interval delimited by the parameters. * * @param iterator source iterator * @param start start element * @param end end element * @return iterator with interval */ public static <T> Iterator<T> openIntervalIterator(Iterator<T> iterator, T start, T end) { class OpenIterator extends AbstractIterator<T> { boolean in = false; @Override protected T computeNext() { if (!in) { T value = Iterators.find(iterator, item -> item == start, null); in = value != null && value != end; } if (in && iterator.hasNext()) { T value = iterator.next(); if (value != end) { return value; } } return endOfData(); } } return new OpenIterator(); } }