Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.springside.modules.utils.collection; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import com.google.common.annotations.Beta; import com.google.common.collect.Iterables; import com.google.common.collect.Iterators; /** * Collection * * List, Map, Queue, Set??Utils. * * ?JDKComparableComparatorNullComparatorJDK8? * * guavaOrdering,fluentableAPI??nullFirstnullLast,reverse * * @see com.google.common.collect.Ordering */ @Beta public abstract class CollectionUtil { /** * ?. */ public static boolean isEmpty(Collection<?> collection) { return (collection == null) || collection.isEmpty(); } /** * ??. */ public static boolean isNotEmpty(Collection<?> collection) { return (collection != null) && !(collection.isEmpty()); } /** * ?Collectioncollectionnull. */ 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(); } /** * ?Collection?collectionnull. */ public static <T> T getLast(Collection<T> collection) { if (isEmpty(collection)) { return null; } // List??. if (collection instanceof List) { List<T> list = (List<T>) collection; return list.get(list.size() - 1); } return Iterators.getLast(collection.iterator()); } /** * ??. */ public static boolean elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2) { return Iterables.elementsEqual(iterable1, iterable2); } /** * ???? */ public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) { return Collections.min(coll); } /** * ??? */ public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) { return Collections.min(coll, comp); } /** * ??? */ public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) { return Collections.max(coll); } /** * ?? */ public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) { return Collections.max(coll, comp); } /** * ???? */ public static <T extends Object & Comparable<? super T>> Pair<T, T> minAndMax(Collection<? extends T> coll) { Iterator<? extends T> i = coll.iterator(); T minCandidate = i.next(); T maxCandidate = minCandidate; while (i.hasNext()) { T next = i.next(); if (next.compareTo(minCandidate) < 0) { minCandidate = next; } else if (next.compareTo(maxCandidate) > 0) { maxCandidate = next; } } return new ImmutablePair<T, T>(minCandidate, maxCandidate); } /** * ??? */ public static <T> Pair<T, T> minAndMax(Collection<? extends T> coll, Comparator<? super T> comp) { Iterator<? extends T> i = coll.iterator(); T minCandidate = i.next(); T maxCandidate = minCandidate; while (i.hasNext()) { T next = i.next(); if (comp.compare(next, minCandidate) < 0) { minCandidate = next; } else if (comp.compare(next, maxCandidate) > 0) { maxCandidate = next; } } return new ImmutablePair<T, T>(minCandidate, maxCandidate); } }