Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ import java.util.Collection; import java.util.List; public class Main { public static <T> T getFirst(Collection<T> collection) { if (isEmpty(collection)) { return null; } if (collection instanceof List) { return ((List<T>) collection).get(0); } return collection.iterator().next(); } public static boolean isEmpty(Collection<?> collection) { return (collection == null) || collection.isEmpty(); } }