public static <T>List<List<T>> overlap(List<List<T>> lists,int before,int after){
  if (before < 0) {
    throw new IllegalArgumentException("Value of before cannot be negative");
  }
  if (after < 0) {
    throw new IllegalArgumentException("Value of after cannot be negative");
  }
  ListIterator<List<T>> iter=lists.listIterator();
  List<List<T>> result=new ArrayList<List<T>>();
  for (; iter.hasNext(); ) {
    List<T> current=new ArrayList<T>(iter.next());
    List<T> prev=before > 0 ? findPrevious(iter) : null;
    List<T> next=after > 0 ? findNext(iter) : null;
    if (prev != null) {
      List<T> overlap=prev.subList(prev.size() - before,prev.size());
      current.addAll(0,overlap);
    }
    if (next != null) {
      List<T> overlap=next.subList(0,after);
      current.addAll(overlap);
    }
    result.add(current);
  }
  return result;
}

public static < T > List < List < T > > overlap ( List < List < T > > lists , int before , int after ) { if ( before < 0 ) { throw new IllegalArgumentException ( "Value of before cannot be negative" ) ; } if ( after < 0 ) { throw new IllegalArgumentException ( "Value of after cannot be negative" ) ; } ListIterator < List < T > > iter = lists . listIterator ( ) ; List < List < T > > result = new ArrayList < List < T > > ( ) ; for ( ; iter . hasNext ( ) ; ) { List < T > current = new ArrayList < T > ( iter . next ( ) ) ; List < T > prev = before > 0 ? findPrevious ( iter ) : null ; List < T > next = after > 0 ? findNext ( iter ) : null ; if ( prev != null ) { List < T > overlap = prev . subList ( prev . size ( ) - before , prev . size ( ) ) ; current . addAll ( 0 , overlap ) ; } if ( next != null ) { List < T > overlap = next . subList ( 0 , after ) ; current . addAll ( overlap ) ; } result . add ( current ) ; } return result ; }
introduces overlap into a series of lists .
