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.Collection;
import java.util.Collections;

import java.util.List;

public class Main {

    public static int binarySearchIndex(Collection<?> collection, Object value) {
        if ((value instanceof Comparable)) {
            List valueList;
            if ((collection instanceof List)) {
                valueList = (List) collection;
            } else {
                valueList = new ArrayList();
                valueList.addAll(collection);
            }
            return Collections.binarySearch(valueList, value);
        }
        return searchIndex(collection, value);
    }

    public static int searchIndex(Collection<? extends Object> collection, Object value) {
        List<? extends Object> valueList = new ArrayList<Object>(collection);

        int i = 0;
        for (Object o : valueList) {
            if ((o == value) || ((o != null) && (o.equals(value)))) {
                return i;
            }
            i++;
        }
        return -1;
    }
}