Java tutorial
//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 getSmallestNotNull(final Collection<? extends E> c) { if ((c instanceof List) && (c instanceof RandomAccess)) { return getSmallestNotNull((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 == null) { continue; } if ((result == null) || (element.compareTo(result) < 0)) { result = element; } } if (result == null) { throw new NoSuchElementException(); } return result; } private static <E extends Comparable<E>> E getSmallestNotNull(final List<? extends E> randomAccessList) { E result = null; E element = randomAccessList.get(0); for (int i = 1; i < randomAccessList.size(); i++) { element = randomAccessList.get(i); if (element == null) { continue; } if ((result == null) || (element.compareTo(result) < 0)) { result = element; } } if (result == null) { throw new NoSuchElementException(); } return result; } public static <E> E getSmallestNotNull(final Comparator<? super E> comparator, final Collection<? extends E> c) { if ((c instanceof List) && (c instanceof RandomAccess)) { return getSmallestNotNull(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 (element == null) { continue; } if ((result == null) || (comparator.compare(element, result) < 0)) { result = element; } } if (result == null) { throw new NoSuchElementException(); } return result; } private static <E> E getSmallestNotNull(final Comparator<? super E> comparator, final List<? extends E> randomAccessList) { E result = randomAccessList.get(0); E element; for (int i = 1; i < randomAccessList.size(); i++) { element = randomAccessList.get(i); if (element == null) { continue; } if ((result == null) || (comparator.compare(element, result) < 0)) { result = element; } } if (result == null) { throw new NoSuchElementException(); } 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); } } }