Java Collection Find findCommonElementType(Collection collection)

Here you can find the source of findCommonElementType(Collection collection)

Description

find Common Element Type

License

Open Source License

Declaration

public static Class<?> findCommonElementType(Collection<?> collection) 

Method Source Code


//package com.java2s;

import java.util.Collection;

public class Main {

    public static Class<?> findCommonElementType(Collection<?> collection) {
        if (isEmpty(collection)) {
            return null;
        }//from ww  w.  j  ava 2 s .c  om
        Class<?> candidate = null;
        for (Object val : collection) {
            if (val != null) {
                if (candidate == null) {
                    candidate = val.getClass();
                } else if (candidate != val.getClass()) {
                    return null;
                }
            }
        }
        return candidate;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null || collection.isEmpty());
    }
}

Related

  1. findClass(String name, Collection availableImports, ClassLoader cl)
  2. findCommonElementType(Collection collection)
  3. findCommonElementType(Collection collection)
  4. findCommonElementType(Collection collection)
  5. findMax(Collection vals)
  6. findMax(Collection vals)
  7. findMax(Iterable collection)
  8. findValueOfType(Collection collection, Class type)