Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;
import java.util.Comparator;

import java.util.Iterator;
import java.util.List;

import java.util.Map;
import java.util.NoSuchElementException;
import java.util.RandomAccess;

public class Main {
    public static <E extends Comparable<E>> E getSmallest(final Collection<? extends E> c) {
        if (c.isEmpty()) {
            throw new NoSuchElementException();
        }

        if ((c instanceof List) && (c instanceof RandomAccess)) {
            return getSmallest((List<? extends E>) c);
        }

        final Iterator<? extends E> iterator = c.iterator();
        E result = iterator.next();
        E element;

        while (iterator.hasNext()) {
            element = iterator.next();
            if (element.compareTo(result) < 0) {
                result = element;
            }
        }

        return result;
    }

    private static <E extends Comparable<E>> E getSmallest(final List<? extends E> randomAccessList) {
        E result = randomAccessList.get(0);

        for (int i = 1; i < randomAccessList.size(); i++) {
            final E element = randomAccessList.get(i);
            if (element.compareTo(result) < 0) {
                result = element;
            }
        }

        return result;
    }

    public static <E> E getSmallest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
        if (c.isEmpty()) {
            throw new NoSuchElementException();
        }

        if ((c instanceof List) && (c instanceof RandomAccess)) {
            return getSmallest(comparator, (List<? extends E>) c);
        }

        final Iterator<? extends E> iterator = c.iterator();
        E result = iterator.next();
        E element;

        while (iterator.hasNext()) {
            element = iterator.next();
            if (comparator.compare(element, result) < 0) {
                result = element;
            }
        }

        return result;
    }

    private static <E> E getSmallest(final Comparator<? super E> comparator,
            final List<? extends E> randomAccessList) {
        E result = randomAccessList.get(0);

        for (int i = 1; i < randomAccessList.size(); i++) {
            final E element = randomAccessList.get(i);
            if (comparator.compare(element, result) < 0) {
                result = element;
            }
        }

        return result;
    }

    public static <V> void get(final Map<?, ? extends V> map, final Collection<? extends Object> keys,
            final Collection<? super V> values) {
        for (final Object key : keys) {
            if (!map.containsKey(key)) {
                continue;
            }

            final V value = map.get(key);
            values.add(value);
        }
    }

    public static <V> void get(final Map<?, ? extends V> map, final Collection<? super V> values,
            final Object... keys) {
        for (final Object key2 : keys) {
            final Object key = key2;
            if (!map.containsKey(key)) {
                continue;
            }

            final V value = map.get(key);
            values.add(value);
        }
    }
}