
org.aksw.commons.collections.SetUtils Maven / Gradle / Ivy
package org.aksw.commons.collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import com.google.common.collect.ForwardingSet;
public class SetUtils {
// public static Set asSet(Iterable c) {
// return (c instanceof Set) ? (Set) c : Sets.newLinkedHashSet(c);
// }
/**
* If the argument is already an instance of a set then cast it to a set and return it;
* otherwise create a new LinkedHashSet initialized with the given items
*/
public static Set asSet(Iterable c)
{
return (c instanceof Set) ? (Set)c : CollectionUtils.newCollection(LinkedHashSet::new, c);
}
public static Set newLinkedHashSet(Iterator extends T> c)
{
return CollectionUtils.newCollection(LinkedHashSet::new, c);
}
/**
* Short hand for
* Set result = source.stream().map(fn).collect(Collectors.toSet())
*
* Maps a set of keys to a corresponding set of values via a given map
* TODO Probably this method can be replaced by something from guava
*
* @param set
* @param map
* @return
*/
public static Set mapSet(Set set, Map map) {
Set result = new HashSet();
for(K item : set) {
V v = map.get(item);
result.add(v);
}
return result;
}
public static Set newForwardingSet(Supplier extends Set> setSupplier) {
return new ForwardingSet() {
@Override
protected Set delegate() {
return setSupplier.get();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy