public static List<String> anagrams(String input) { if (input.length() <= 2) { return input.length() == 2 ? Arrays.asList(input, input.substring(1) + input.substring(0, 1)) : Collections.singletonList(input); } return IntStream.range(0, input.length()) .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1))) .flatMap(entry -> anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1)) .stream() .map(s -> entry.getValue() + s)) .collect(Collectors.toList()); }
public static String capitalizeEveryWord(final String input) { return Pattern.compile("\\b(?=\\w)").splitAsStream(input) .map(w -> capitalize(w, false)) .collect(Collectors.joining()); }
/** * Deep flattens an array. * * @param input A nested array containing integers * @return flattened array */ public static int[] deepFlatten(Object[] input) { return Arrays.stream(input) .flatMapToInt(o -> { if (o instanceof Object[]) { return Arrays.stream(deepFlatten((Object[]) o)); } return IntStream.of((Integer) o); }).toArray(); }
/** * Filters out the non-unique values in an array. * <p> * Use Array.stream().filter() for an array containing only the unique values. * * @param elements input array * @return unique values in the array */ public static int[] filterNonUnique(int[] elements) { return Arrays.stream(elements) .filter(el -> indexOf(elements, el) == lastIndexOf(elements, el)) .toArray(); }
private static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); }
public static <T> String join(T[] arr) { return join(arr, ","); }
public static List<Class<?>> getAllInterfaces(final Class<?> cls) { return Stream.concat( Arrays.stream(cls.getInterfaces()).flatMap(intf -> Stream.concat(Stream.of(intf), getAllInterfaces(intf).stream())), cls.getSuperclass() == null ? Stream.empty() : getAllInterfaces(cls.getSuperclass()).stream() ).distinct().collect(Collectors.toList()); }
/** * Flattens an array up to the specified depth. * * @param elements input array * @param depth depth to which to flatten array * @return flattened array */ public static Object[] flattenDepth(Object[] elements, int depth) { if (depth == 0) { return elements; } return Arrays.stream(elements) .flatMap(el -> el instanceof Object[] ? Arrays.stream(flattenDepth((Object[]) el, depth - 1)) : Arrays.stream(new Object[]{el}) ).toArray(); }
/** * Filters out the non-unique values in an array. * <p> * Use Array.stream().filter() for an array containing only the unique values. * * @param elements input array * @return unique values in the array */ public static int[] filterNonUnique(int[] elements) { return Arrays.stream(elements) .filter(el -> indexOf(elements, el) == lastIndexOf(elements, el)) .toArray(); }
/** * Calculates the greatest common denominator (gcd) of an array of numbers * * @param numbers Array of numbers * @return gcd of array of numbers */ public static OptionalInt gcd(int[] numbers) { return Arrays.stream(numbers) .reduce((a, b) -> gcd(a, b)); }
public static <T> String join(T[] arr, String separator) { return join(arr, separator, separator); }
public static List<Class<?>> getAllInterfaces(final Class<?> cls) { return Stream.concat( Arrays.stream(cls.getInterfaces()).flatMap(intf -> Stream.concat(Stream.of(intf), getAllInterfaces(intf).stream())), cls.getSuperclass() == null ? Stream.empty() : getAllInterfaces(cls.getSuperclass()).stream() ).distinct().collect(Collectors.toList()); }
/** * Flattens an array up to the specified depth. * * @param elements input array * @param depth depth to which to flatten array * @return flattened array */ public static Object[] flattenDepth(Object[] elements, int depth) { if (depth == 0) { return elements; } return Arrays.stream(elements) .flatMap(el -> el instanceof Object[] ? Arrays.stream(flattenDepth((Object[]) el, depth - 1)) : Arrays.stream(new Object[]{el}) ).toArray(); }
/** * Calculates the lowest common multiple (lcm) of an array of numbers. * * @param numbers Array of numbers * @return lcm of array of numbers */ public static OptionalInt lcm(int[] numbers) { IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y); return Arrays.stream(numbers) .reduce((a, b) -> lcm.applyAsInt(a, b)); }
public static <T> String join(T[] arr) { return join(arr, ","); }
public static String capitalizeEveryWord(final String input) { return Pattern.compile("\\b(?=\\w)").splitAsStream(input) .map(w -> capitalize(w, false)) .collect(Collectors.joining()); }
public static List<String> anagrams(String input) { if (input.length() <= 2) { return input.length() == 2 ? Arrays.asList(input, input.substring(1) + input.substring(0, 1)) : Collections.singletonList(input); } return IntStream.range(0, input.length()) .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1))) .flatMap(entry -> anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1)) .stream() .map(s -> entry.getValue() + s)) .collect(Collectors.toList()); }
/** * Deep flattens an array. * * @param input A nested array containing integers * @return flattened array */ public static int[] deepFlatten(Object[] input) { return Arrays.stream(input) .flatMapToInt(o -> { if (o instanceof Object[]) { return Arrays.stream(deepFlatten((Object[]) o)); } return IntStream.of((Integer) o); }).toArray(); }
private static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); }
public static <T> String join(T[] arr, String separator) { return join(arr, separator, separator); }