public static <T extends Annotation>List<T> filterToTopLevelAnnotations(final Collection<T> annotations){
  final List<T> topLevel=new LinkedList<>();
  for (  final T a : annotations) {
    boolean covered=false;
    for (    final T b : annotations) {
      if (!a.equals(b) && b.getBegin() <= a.getBegin() && a.getEnd() <= b.getEnd()) {
        covered=true;
        break;
      }
    }
    if (!covered) {
      topLevel.add(a);
    }
  }
  return topLevel;
}

public static < T extends Annotation > List < T > filterToTopLevelAnnotations ( final Collection < T > annotations ) { final List < T > topLevel = new LinkedList < > ( ) ; for ( final T a : annotations ) { boolean covered = false ; for ( final T b : annotations ) { if ( ! a . equals ( b ) && b . getBegin ( ) <= a . getBegin ( ) && a . getEnd ( ) <= b . getEnd ( ) ) { covered = true ; break ; } } if ( ! covered ) { topLevel . add ( a ) ; } } return topLevel ; }
filter only the top level annotations ( that is remove all the covered annotations ) . this is not too efficient , but is should work with any ordering .
