Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static <T extends Comparable> List<T> range(List<? extends T> list, T min, T max) {
        return range(list, min, max, Comparable::compareTo);
    }

    public static <T> List<T> range(List<? extends T> list, T min, T max, Comparator<? super T> comparator) {
        List<T> tList = newArrayList();
        addAll(list, tList);
        tList.sort(comparator);
        return tList.subList(indexOf(list, min), indexOf(list, max) + 1);
        /*
            return list.stream()
                .sorted(comparator)
                .filter(t -> comparator.compare(t,min)>=0&&comparator.compare(t,max)<=0)
                .collect(Collectors.toList());
         */
    }

    public static <T> List<T> newArrayList() {
        return new ArrayList<>();
    }

    public static <T> void addAll(List<? extends T> source, List<? super T> destination) {
        destination.addAll(source);
    }

    public static <T> int indexOf(List<? extends T> source, T t) {
        return source.indexOf(t);
    }
}