Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static <T> List range(List<? extends T> list, T min, T max) { List<? super T> copyList = new ArrayList<>(list); Collections.sort(copyList, (o1, o2) -> Integer.compare(o1.hashCode(), o2.hashCode())); if (copyList.indexOf(min) < copyList.indexOf(max)) { return copyList.subList(copyList.indexOf(min), copyList.indexOf(max)); } else { return newArrayList(); } } public static <T> List range(List<? extends T> list, T min, T max, Comparator<? super T> comparator) { List<? super T> copyList = new ArrayList<T>(list); Collections.sort(list, comparator); if (copyList.indexOf(min) < copyList.indexOf(max)) { return copyList.subList(copyList.indexOf(min), copyList.indexOf(max)); } else { return newArrayList(); } } public static <T> int indexOf(List<? extends T> source, Object o) { return source.indexOf(o); } public static <T> List<T> newArrayList() { return new ArrayList(); } }