Java tutorial
//package com.java2s; import java.util.*; public class Main { public static <T extends Comparable<? super T>> List<T> range(List<? extends T> list, T min, T max) { List<T> resultList = new ArrayList<T>(); for (T elem : list) { if (elem.compareTo(min) >= 0 && elem.compareTo(max) <= 0) { resultList.add(elem); } } Collections.sort(resultList); return resultList; } public static <T> List<T> range(List<? extends T> list, T min, T max, Comparator<T> comparator) { List<T> resultList = new ArrayList<T>(); for (T elem : list) { if (comparator.compare(elem, min) >= 0 && comparator.compare(elem, max) <= 0) { resultList.add(elem); } } Collections.sort(resultList, comparator); return resultList; } public static <T> void add(List<? super T> source, T o) { source.add(o); } }