org.aksw.commons.collections.CollectionUtils Maven / Gradle / Ivy
package org.aksw.commons.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* User: raven
* Date: 4/17/11
* Time: 12:36 AM
*/
public class CollectionUtils {
/**
* Given an iterable A whose elements are iterables, this method will return the first
* element of A.
* If no such element exist, an empty iterable is returned rather than null.
*
*
* @param iterable
* @param
* @param
* @return
*/
public static > Iterable safeGetFirst(Iterable iterable) {
Iterator it = iterable.iterator();
return it.hasNext() ? it.next() : Collections.emptySet();
}
public static List> chunk(Iterable col, int batchSize)
{
List> result = new ArrayList>();
List chunk = new ArrayList();
Iterator it = col.iterator();
while(it.hasNext()) {
chunk.add(it.next());
if(chunk.size() >= batchSize || !it.hasNext()) {
result.add(chunk);
if(it.hasNext())
chunk = new ArrayList();
}
}
return result;
}
public static Set asSet(Collection c)
{
return (c instanceof Set) ? (Set)c : new HashSet(c);
}
/** Transforms an array into a Hashset. @author Konrad Höffner */
static Set asSet(T[] a)
{
Set s = new HashSet();
for(T e:a) {s.add(e);}
return s;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy