Skip to content

Instantly share code, notes, and snippets.

@puke3615
Created July 14, 2021 07:42
Show Gist options
  • Save puke3615/3deff4315febd32bfc257801dbf64f7e to your computer and use it in GitHub Desktop.
Save puke3615/3deff4315febd32bfc257801dbf64f7e to your computer and use it in GitHub Desktop.
CollectionUtil
import java.util.Collection;
import java.util.Iterator;
/**
* @author puke
* @version 2021/5/10
*/
public class CollectionUtil {
public static <T> T getFirst(Collection<T> collection) {
if (collection == null) {
return null;
} else {
Iterator<T> iterator = collection.iterator();
return iterator.hasNext() ? iterator.next() : null;
}
}
public static <T> boolean isNotEmpty(Collection<T> collection) {
return !isEmpty(collection);
}
public static <T> boolean isEmpty(Collection<T> collection) {
return collection == null || collection.isEmpty();
}
public static <T> int size(Collection<T> collection) {
return collection == null ? 0 : collection.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment