Codota Logo
snippets
Code IndexAdd Codota to your IDE (free)

How to use snippets

Best Java code snippets using snippets (Showing top 20 results out of 315)

  • Add the Codota plugin to your IDE and get smart completions
private void myMethod () {
FileOutputStream f =
  • Codota IconFile file;new FileOutputStream(file)
  • Codota IconString name;new FileOutputStream(name)
  • Codota IconFile file;new FileOutputStream(file, true)
  • Smart code suggestions by Codota
}
origin: biezhi/30-seconds-of-java8

private static int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
origin: biezhi/30-seconds-of-java8

public static <T> String join(T[] arr) {
  return join(arr, ",");
}
origin: biezhi/30-seconds-of-java8

/**
 * 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();
}
origin: biezhi/30-seconds-of-java8

/**
 * 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();
}
origin: biezhi/30-seconds-of-java8

public static String capitalizeEveryWord(final String input) {
  return Pattern.compile("\\b(?=\\w)").splitAsStream(input)
      .map(w -> capitalize(w, false))
      .collect(Collectors.joining());
}
origin: biezhi/30-seconds-of-java8

/**
 * 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();
}
origin: biezhi/30-seconds-of-java8

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());
}
origin: biezhi/30-seconds-of-java8

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());
}
origin: shekhargulati/30-seconds-of-java

/**
 * 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();
}
origin: biezhi/30-seconds-of-java8

/**
 * 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));
}
origin: biezhi/30-seconds-of-java8

public static <T> String join(T[] arr, String separator) {
  return join(arr, separator, separator);
}
origin: shekhargulati/30-seconds-of-java

/**
 * 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();
}
origin: shekhargulati/30-seconds-of-java

public static String capitalizeEveryWord(final String input) {
  return Pattern.compile("\\b(?=\\w)").splitAsStream(input)
      .map(w -> capitalize(w, false))
      .collect(Collectors.joining());
}
origin: shekhargulati/30-seconds-of-java

/**
 * 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();
}
origin: biezhi/30-seconds-of-java8

/**
 * 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));
}
origin: shekhargulati/30-seconds-of-java

public static <T> String join(T[] arr) {
  return join(arr, ",");
}
origin: shekhargulati/30-seconds-of-java

private static int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
origin: shekhargulati/30-seconds-of-java

public static <T> String join(T[] arr, String separator) {
  return join(arr, separator, separator);
}
origin: shekhargulati/30-seconds-of-java

/**
 * 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));
}
origin: shekhargulati/30-seconds-of-java

/**
 * 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));
}
snippets

Most used classes

  • Snippets
  • Rational
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now