+++ title = "utils" draft = false tags = [ "code", "java", "utils" ] date = "2013-03-12" +++ # Utility methods ## SetUtils ```java public class SetUtil { public static Set asSet(T... elements) { return new HashSet(Arrays.asList(elements)); } public static boolean bevat(Set someSet, Predicate predicate) { return !Sets.filter(someSet, predicate).isEmpty(); } public static Set cast(Set someSet, Class classToCastTo) { Set newSet = new LinkedHashSet(); for (Object object : someSet) { newSet.add(classToCastTo.cast(object)); } return newSet; } } ``` ## ListUtils ```java public class ListUtil { public static List sort(Collection collection, Comparator comparator) { List toSort = new ArrayList(collection); Collections.sort(toSort, comparator); return toSort; } public static boolean bevatDubbels(List aList) { return Sets.newHashSet(aList).size() != aList.size(); } public static List cast(List aList, Class clazz) { List result = new ArrayList(); for (Object obj : aList) { result.add(clazz.cast(obj)); } return result; } } ```