Skip to content

Instantly share code, notes, and snippets.

@milingo
Last active October 18, 2019 16:07
Show Gist options
  • Save milingo/9061630 to your computer and use it in GitHub Desktop.
Save milingo/9061630 to your computer and use it in GitHub Desktop.
Java
// Init
new String[]{"a", "b"}
List<String> list = new ArrayList<String>(
Arrays.asList("String A", "String B", "String C")
);
Map<String, Owner> owners = new HashMap<String, Owner>() {{
put("no-money", createOwnerWithoutMoney());
}};
// convert
new HashSet<>(Arrays.asList(new String[]{"a", "b"}));
// utils
public static <T> boolean any(Collection<T> collection, ConditionalFunction<T> f) {
if (collection != null) {
for (T e : collection) {
if (f.fulfills(e)) {
return true;
}
}
}
return false;
}
public static <T> boolean isEmpty(Collection<T> list) {
return list == null || list.isEmpty();
}
// utils tests
public void testAny(List<Integer> numbers, boolean expected) throws Exception {
ConditionalFunction<Integer> isEven = new ConditionalFunction<Integer>() {
public boolean fulfills(Integer number) {
return number % 2 == 0;
}
};
assertTrue(CollectionUtils.any(numbers, isEven) == expected);
}
public class CollectionUtils {
/**
* Check the intersection between list1 and list2 is not empty.
*
* @param list1
* @param list2
* @param f the function to
* @return whether the intersection is empty.
*/
public static Boolean emptyIntersection(Collection<? extends Object> list1, Collection<? extends Object> list2, Predicate<? super Object> f) {
return !list1.parallelStream().anyMatch(f);
}
/**
* Check the intersection between list1 and list2 is not empty.
*
* @param list1
* @param list2
* @return whether the intersection is empty.
*/
public static Boolean emptyIntersection(Collection<? extends Object> list1, Collection<? extends Object> list2) {
return emptyIntersection(list1, list2, list2::contains);
}
/**
* Check if the first collection contains at least one element which is not in the second collection.
*
* @param list1
* @param list2
* @return
*/
public static Boolean hasDifferentElements(Collection<? extends Object> list1, Collection<? extends Object> list2, Predicate<? super Object> f) {
return list1.parallelStream().anyMatch(f);
}
/**
* Check if the first collection contains at least one element which is not in the second collection.
*
* @param list1
* @param list2
* @return
*/
public static Boolean hasDifferentElements(Collection<? extends Object> list1, Collection<? extends Object> list2) {
return hasDifferentElements(list1, list2, item -> !list2.contains(item));
}
public static <T> boolean isEmpty(Collection<T> list) {
return list == null || list.isEmpty();
}
public static void addIfNotNull(List list, Object item) {
if (item != null) {
list.add(item);
}
}
public static <T> Optional<T> findFirstNotNull(List<T> list) {
return list.stream().filter(x -> x != null).findFirst();
}
public static Optional<String> findFirstNotNullOrEmpty(List<String> list) {
return list.stream().filter(x -> !StringUtils.isNullOrEmpty(x)).findFirst();
}
}
public <T extends Shape> void addIfPretty(List<T> shapes, T shape) {
if (shape.isPretty()) {
shapes.add(shape);
}
}
import java.text.MessageFormat;
String pattern = "I have {0} apples";
String message = MessageFormat.format(pattern, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment