List of usage examples for java.util Collection size
int size();
From source file:Main.java
public static String collectionAsString(Collection<?> collection) { if (collection == null) return "[null]"; if (collection.size() == 0) return "[empty]"; StringBuilder builder = new StringBuilder(); for (Object obj : collection) { builder.append(obj).append(", "); }//from w w w . java 2s . co m builder.deleteCharAt(builder.length() - 1); return builder.toString(); }
From source file:Main.java
/** * Converts a typed {@link Collection} to a typed array. * // w ww .j a v a 2 s . c o m * @param type * the type class * @param collection * the collection to convert to an array. * @return the array. */ @SuppressWarnings("unchecked") public static <T> T[] toArray(Class<T> type, Collection<T> collection) { return collection.toArray((T[]) Array.newInstance(type, collection.size())); }
From source file:Main.java
public static <T> T get(final Collection<T> collection, final int index) { if (collection.size() < index + 1) { return null; }//from w ww . j ava 2s . c o m int i = 0; for (T item : collection) { if (i == index) { return item; } i++; } return null; }
From source file:Main.java
/** * Nullsafe method returning the number of elements the incoming collection contains. * * @param c Collection of Objects to count the number of elements from * @return int containing the number of the elements in the collection, or -1 if the collection is null *//* w ww .j a va 2 s . c om*/ public static int size(Collection c) { int size = -1; if (c != null) { size = c.size(); } return size; }
From source file:Main.java
/** * Checks whether the COLLECTION is not NULL and has at least one element. * //from w w w . j av a2s. c om * @param <T> * the generic type * @param collection * the collection * @return true, if successful */ public static <T> boolean hasElements(Collection<T> collection) { return (null != collection && collection.size() > 0); }
From source file:com.teradata.benchto.service.model.AggregatedMeasurement.java
public static AggregatedMeasurement aggregate(MeasurementUnit unit, Collection<Double> values) { if (values.size() < 2) { Double value = Iterables.getOnlyElement(values); return new AggregatedMeasurement(unit, value, value, value, 0.0, 0.0); }//from ww w. j ava 2 s .c o m DescriptiveStatistics statistics = new DescriptiveStatistics( values.stream().mapToDouble(Double::doubleValue).toArray()); double stdDevPercent = 0.0; if (statistics.getStandardDeviation() > 0.0) { stdDevPercent = (statistics.getStandardDeviation() / statistics.getMean()) * 100; } return new AggregatedMeasurement(unit, statistics.getMin(), statistics.getMax(), statistics.getMean(), statistics.getStandardDeviation(), stdDevPercent); }
From source file:Main.java
public static boolean equalsIgnoreOrder(Collection<?> c1, Collection<?> c2) { if (c1 == c2) { return true; }//ww w. j av a 2s. c om if (c1.size() != c2.size()) { return false; } return c1.containsAll(c2) && c2.containsAll(c1); }
From source file:Main.java
/** * *// www . ja v a 2s . co m * Get size of Collection and NullPointerException safe * * @param collection * @return -1 if collction is null then else return collection's size */ public static int getSize(Collection collection) { if (collection == null) { return -1; } return collection.size(); }
From source file:Main.java
public static <ELEMENT> HashSet<ELEMENT> newHashSet(Collection<ELEMENT> elements) { final HashSet<ELEMENT> set = newHashSetSized(elements.size()); set.addAll(elements);//ww w . j av a 2 s.c om return set; }
From source file:Main.java
public static Object findOne(Collection coll) { if (coll.isEmpty()) { return null; } else if (coll.size() > 1) { throw new RuntimeException("Expected only one member in collection, found many: " + coll.toString()); } else {//from w w w . j a v a 2 s . c o m return coll.iterator().next(); } }