Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.*;

public class Main {
    /**
     * Returns the last position in 'list' where 'key' could be inserted without violating ordering according to 'comparator'.
     * (Collections.binarySearch makes no guarantee about which index will be returned if the list contains multiple elements equal
     * to the key.)
     * 
     * As with Collections.binarySearch, results are undefined if 'list' is not sorted into ascending order according to
     * 'comparator'.
     */
    public static <T> int upperBound(List<? extends T> list, T key, Comparator<? super T> comparator) {
        int index = Collections.binarySearch(list, key, comparator);
        if (index < 0) {
            return -index - 1;
        }
        // FIXME: O(n) is distressing on a sorted list.
        while (index + 1 < list.size() && comparator.compare(list.get(index + 1), key) == 0) {
            ++index;
        }
        // We return the first index *past* the [run of] value[s].
        return index + 1;
    }
}