• Stars
    star
    996
  • Rank 46,003 (Top 1.0 %)
  • Language
    Java
  • License
    Creative Commons ...
  • Created almost 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Curated collection of useful little Java functions that you can understand quickly

30 Seconds Of Java Build Status

Curated collection of useful little Java 8 functions that you can understand quickly.

Table of Contents

Array

View contents

Math

View contents

String

View contents

IO

View contents

Exception

View contents

System

View contents

Class

View contents

Enum

View contents

Array

chunk

Chunks an array into smaller arrays of specified size.

public static int[][] chunk(int[] numbers, int size) {
    return IntStream.iterate(0, i -> i + size)
            .limit((long) Math.ceil((double) numbers.length / size))
            .mapToObj(cur -> Arrays.copyOfRange(numbers, cur, cur + size > numbers.length ? numbers.length : cur + size))
            .toArray(int[][]::new);
}

concat

public static <T> T[] concat(T[] first, T[] second) {
    return Stream.concat(
            Stream.of(first),
            Stream.of(second)
    ).toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}

countOccurrences

Counts the occurrences of a value in an array.

Use Arrays.stream().filter().count() to count total number of values that equals the specified value.

public static long countOccurrences(int[] numbers, int value) {
    return Arrays.stream(numbers)
            .filter(number -> number == value)
            .count();
}

deepFlatten

Deep flattens an array.

Use recursion. Use Arrays.stream().flatMapToInt()

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();
}

difference

Returns the difference between two arrays.

Create a Set from b, then use Arrays.stream().filter() on a to only keep values not contained in b.

public static int[] difference(int[] first, int[] second) {
    Set<Integer> set = Arrays.stream(second).boxed().collect(Collectors.toSet());
    return Arrays.stream(first)
            .filter(v -> !set.contains(v))
            .toArray();
}

differenceWith

Filters out all values from an array for which the comparator function does not return true.

The comparator for int is implemented using IntBinaryOperator function.

Uses Arrays.stream().filter and Arrays.stream().noneMatch() to find the appropriate values.

public static int[] differenceWith(int[] first, int[] second, IntBinaryOperator comparator) {
    return Arrays.stream(first)
            .filter(a ->
                    Arrays.stream(second)
                            .noneMatch(b -> comparator.applyAsInt(a, b) == 0)
            ).toArray();
}

distinctValuesOfArray

Returns all the distinct values of an array.

Uses Arrays.stream().distinct() to discard all duplicated values.

public static int[] distinctValuesOfArray(int[] elements) {
    return Arrays.stream(elements).distinct().toArray();
}

dropElements

Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.

Loop through the array, using Arrays.copyOfRange() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.

public static int[] dropElements(int[] elements, IntPredicate condition) {
    while (elements.length > 0 && !condition.test(elements[0])) {
        elements = Arrays.copyOfRange(elements, 1, elements.length);
    }
    return elements;
}

dropRight

Returns a new array with n elements removed from the right.

Check if n is shorter than the given array and use Array.copyOfRange() to slice it accordingly or return an empty array.

public static int[] dropRight(int[] elements, int n) {
    if (n < 0) {
        throw new IllegalArgumentException("n is less than 0");
    }
    return n < elements.length
            ? Arrays.copyOfRange(elements, 0, elements.length - n)
            : new int[0];
}

everyNth

Returns every nth element in an array.

Use IntStream.range().filter() to create a new array that contains every nth element of a given array.

public static int[] everyNth(int[] elements, int nth) {
     return IntStream.range(0, elements.length)
             .filter(i -> i % nth == nth - 1)
             .map(i -> elements[i])
             .toArray();
 }

indexOf

Find index of element in the array. Return -1 in case element does not exist.

Uses IntStream.range().filter() to find index of the element in the array.

public static int indexOf(int[] elements, int el) {
    return IntStream.range(0, elements.length)
            .filter(idx -> elements[idx] == el)
            .findFirst()
            .orElse(-1);
}

lastIndexOf

Find last index of element in the array. Return -1 in case element does not exist.

Uses IntStream.iterate().limit().filter() to find index of the element in the array.

public static int lastIndexOf(int[] elements, int el) {
    return IntStream.iterate(elements.length - 1, i -> i - 1)
            .limit(elements.length)
            .filter(idx -> elements[idx] == el)
            .findFirst()
            .orElse(-1);
}

filterNonUnique

Filters out the non-unique values in an array.

Use Arrays.stream().filter() for an array containing only the unique values.

public static int[] filterNonUnique(int[] elements) {
    return Arrays.stream(elements)
            .filter(el -> indexOf(elements, el) == lastIndexOf(elements, el))
            .toArray();
}

flatten

Flattens an array.

Use Arrays.stream().flatMapToInt().toArray() to create a new array.

public static int[] flatten(Object[] elements) {
    return Arrays.stream(elements)
            .flatMapToInt(el -> el instanceof int[]
                    ? Arrays.stream((int[]) el)
                    : IntStream.of((int) el)
            ).toArray();
}

flattenDepth

Flattens an array up to the specified depth.

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();


}

groupBy

Groups the elements of an array based on the given function.

Uses Arrays.stream().collect(Collectors.groupingBy()) to group based on the grouping function.

public static <T, R> Map<R, List<T>> groupBy(T[] elements, Function<T, R> func) {
    return Arrays.stream(elements).collect(Collectors.groupingBy(func));
}

initial

Returns all the elements of an array except the last one. Use Arrays.copyOfRange() to return all except the last one

public static <T> T[] initial(T[] elements) {
    return Arrays.copyOfRange(elements, 0, elements.length - 1);
}

initializeArrayWithRange

Initializes an array containing the numbers in the specified range where start and end are inclusive.

public static int[] initializeArrayWithRange(int end, int start) {
    return IntStream.rangeClosed(start, end).toArray();
}

initializeArrayWithValues

Initializes and fills an array with the specified values.

public static int[] initializeArrayWithValues(int n, int value) {
    return IntStream.generate(() -> value).limit(n).toArray();
}

intersection

Returns a list of elements that exist in both arrays.

Create a Set from second, then use Arrays.stream().filter() on a to only keep values contained in b.

public static int[] intersection(int[] first, int[] second) {
    Set<Integer> set = Arrays.stream(second).boxed().collect(Collectors.toSet());
    return Arrays.stream(first)
            .filter(set::contains)
            .toArray();
}

isSorted

Returns 1 if the array is sorted in ascending order, -1 if it is sorted in descending order or 0 if it is not sorted.

Calculate the ordering direction for the first two elements.Use for loop to iterate over array items and compare them in pairs. Return 0 if the direction changes or the direction if the last element is reached.

public static <T extends Comparable<? super T>> int isSorted(T[] arr) {
    final int direction = arr[0].compareTo(arr[1]) < 0 ? 1 : -1;
    for (int i = 0; i < arr.length; i++) {
        T val = arr[i];
        if (i == arr.length - 1) return direction;
        else if ((val.compareTo(arr[i + 1]) * direction > 0)) return 0;
    }
    return direction;
}

join

Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.

Use IntStream.range to zip index with the array item. Then, use Stream.reduce to combine elements into a string.

public static <T> String join(T[] arr, String separator, String end) {
    return IntStream.range(0, arr.length)
            .mapToObj(i -> new SimpleEntry<>(i, arr[i]))
            .reduce("", (acc, val) -> val.getKey() == arr.length - 2
                    ? acc + val.getValue() + end
                    : val.getKey() == arr.length - 1 ? acc + val.getValue() : acc + val.getValue() + separator, (fst, snd) -> fst);
}

nthElement

Returns the nth element of an array.

Use Arrays.copyOfRange() to get an array containing the nth element at the first place.

public static <T> T nthElement(T[] arr, int n) {
    if (n > 0) {
        return Arrays.copyOfRange(arr, n, arr.length)[0];
    }
    return Arrays.copyOfRange(arr, arr.length + n, arr.length)[0];
}

pick

Picks the key-value pairs corresponding to the given keys from an object.

Use Arrays.stream to filter all the keys that are present in the arr. Then, convert all the keys present into a Map using Collectors.toMap.

public static <T, R> Map<T, R> pick(Map<T, R> obj, T[] arr) {
    return Arrays.stream(arr)
            .filter(obj::containsKey)
            .collect(Collectors.toMap(k -> k, obj::get));
}

reducedFilter

Filter an array of objects based on a condition while also filtering out unspecified keys.

Use Arrays.stream().filter() to filter the array based on the predicate fn so that it returns the objects for which the condition is true. For each filtered Map object, create a new Map with keys present in the keys. Finally, collect all the Map object into an array.

public static Map<String, Object>[] reducedFilter(Map<String, Object>[] data, String[] keys, Predicate<Map<String, Object>> fn) {
    return Arrays.stream(data)
            .filter(fn)
            .map(el -> Arrays.stream(keys).filter(el::containsKey)
                    .collect(Collectors.toMap(Function.identity(), el::get)))
            .toArray((IntFunction<Map<String, Object>[]>) Map[]::new);
}

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). This method also works with strings.

public static <T> T sample(T[] arr) {
    return arr[(int) Math.floor(Math.random() * arr.length)];
}

sampleSize

Gets n random elements at unique keys from array up to the size of array.

Shuffle the array using the Fisher-Yates algorithm. Use Array.copyOfRange() to get the first n elements.

public static <T> T[] sampleSize(T[] input, int n) {
    T[] arr = Arrays.copyOf(input, input.length);
    int length = arr.length;
    int m = length;
    while (m > 0) {
        int i = (int) Math.floor(Math.random() * m--);
        T tmp = arr[i];
        arr[i] = arr[m];
        arr[m] = tmp;
    }
    return Arrays.copyOfRange(arr, 0, n > length ? length : n);
}

shuffle

Randomizes the order of the values of an array, returning a new array.

Uses the Fisher-Yates algorithm to reorder the elements of the array.

public static <T> T[] shuffle(T[] input) {
    T[] arr = Arrays.copyOf(input, input.length);
    int length = arr.length;
    int m = length;
    while (m > 0) {
        int i = (int) Math.floor(Math.random() * m--);
        T tmp = arr[i];
        arr[i] = arr[m];
        arr[m] = tmp;
    }
    return arr;
}

similarity

Returns an array of elements that appear in both arrays.

Use Arrays.stream().filter() to remove values that are not part of second, determined using Arrays.stream().anyMatch().

public static <T> T[] similarity(T[] first, T[] second) {
    return Arrays.stream(first)
            .filter(a -> Arrays.stream(second).anyMatch(b -> Objects.equals(a, b)))
            // Make a new array of first's runtime type, but empty content:
            .toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}

sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use IntStream.range().filter() to find the appropriate index where the element should be inserted.

public static <T extends Comparable<? super T>> int sortedIndex(T[] arr, T el) {
    boolean isDescending = arr[0].compareTo(arr[arr.length - 1]) > 0;
    return IntStream.range(0, arr.length)
            .filter(i -> isDescending ? el.compareTo(arr[i]) >= 0 : el.compareTo(arr[i]) <= 0)
            .findFirst()
            .orElse(arr.length);
}

symmetricDifference

Returns the symmetric difference between two arrays.

Create a Set from each array, then use Arrays.stream().filter() on each of them to only keep values not contained in the other. Finally, concatenate both arrays and create a new array and return it.

public static <T> T[] symmetricDifference(T[] first, T[] second) {
    Set<T> sA = new HashSet<>(Arrays.asList(first));
    Set<T> sB = new HashSet<>(Arrays.asList(second));

    return Stream.concat(
            Arrays.stream(first).filter(a -> !sB.contains(a)),
            Arrays.stream(second).filter(b -> !sA.contains(b))
    ).toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}

tail

Returns all elements in an array except for the first one.

Return Arrays.copyOfRange(1) if the array's length is more than 1, otherwise, return the whole array.

public static <T> T[] tail(T[] arr) {
    return arr.length > 1
            ? Arrays.copyOfRange(arr, 1, arr.length)
            : arr;
}

take

Returns an array with n elements removed from the beginning.

public static <T> T[] take(T[] arr, int n) {
    return Arrays.copyOfRange(arr, 0, n);
}

takeRight

Returns an array with n elements removed from the end.

Use Arrays.copyOfRange() to create a slice of the array with n elements taken from the end.

public static <T> T[] takeRight(T[] arr, int n) {
    return Arrays.copyOfRange(arr, arr.length - n, arr.length);
}

union

Returns every element that exists in any of the two arrays once.

Create a Set with all values of a and b and convert to an array.

public static <T> T[] union(T[] first, T[] second) {
    Set<T> set = new HashSet<>(Arrays.asList(first));
    set.addAll(Arrays.asList(second));
    return set.toArray((T[]) Arrays.copyOf(new Object[0], 0, first.getClass()));
}

without

Filters out the elements of an array, that have one of the specified values.

Use Arrays.strean().filter() to create an array excluding(using !Arrays.asList(elements).contains()) all given values.

public static <T> T[] without(T[] arr, T... elements) {
    List<T> excludeElements = Arrays.asList(elements);
    return Arrays.stream(arr)
            .filter(el -> !excludeElements.contains(el))
            .toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, arr.getClass()));
}

zip

Creates an array of elements, grouped based on the position in the original arrays.

public static List<Object[]> zip(Object[]... arrays) {
    OptionalInt max = Arrays.stream(arrays).mapToInt(arr -> arr.length).max();
    return IntStream.range(0, max.getAsInt())
            .mapToObj(i -> Arrays.stream(arrays)
                    .map(arr -> i < arr.length ? arr[i] : null)
                    .toArray())
            .collect(Collectors.toList());
}

zipObject

Given an array of valid property identifiers and an array of values, return an object associating the properties to the values.

public static Map<String, Object> zipObject(String[] props, Object[] values) {
    return IntStream.range(0, props.length)
            .mapToObj(i -> new SimpleEntry<>(props[i], i < values.length ? values[i] : null))
            .collect(
                    HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}

Maths

average

Returns the average of an of two or more numbers.

public static double average(int[] arr) {
    return IntStream.of(arr)
            .average()
            .orElseThrow(() -> new IllegalArgumentException("Array is empty"));
}

gcd

Calculates the greatest common denominator (gcd) of an array of numbers.

Use Arrays.stream().reduce() and the gcd formula (uses recursion) to calculate the greatest common denominator of an array of numbers.

public static OptionalInt gcd(int[] numbers) {
    return Arrays.stream(numbers)
            .reduce((a, b) -> gcd(a, b));
}

private static int gcd(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

lcm

Calculates the lowest common multiple (lcm) of an array of numbers.

Use Arrays.stream().reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an 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));
}

private static int gcd(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

findNextPositivePowerOfTwo

Finds the next power of two greater than or equal to the value.

This method uses left ship operator to shift 1 by the value on the right side. The right side is calculated using the Integer.numberOfLeadingZeros method.

The Integer.numberOfLeadingZeros give the number of zeros leading the value. For example, calling Integer.numberOfLeadingZeros(3) would give value as 30. This is because 3 is represented in binary as 11. As integer has 32 bits, so there are 30 bits with 0. The right side of the left shift operator becomes 32-30 = 2. Left shifting 1 by 2 i.e. 001 << 2 would be 100. 100 in decimal is equal to 4.

public static int findNextPositivePowerOfTwo(int value) {
    return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}

isEven

Check if the number is even.

This method uses bitwise & operator. The 0b1 is the binary representation of 1. Since, Java 7 you can write binary literals by prefixing them with either 0b or 0B. The & operator will return 0 when number is even. For example, IsEven(4) would result in 100 & 001. The result of & will be 000.

public static boolean isEven(final int value) {
    return (value & 0b1) == 0;
}

isPowerOfTwo

Checks if a value is positive power of two.

To understand how it works let's assume we made a call IsPowerOfTwo(4).

As value is greater than 0, so right side of the && operator will be evaluated.

The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.

The result of (value & value) is value. 100 & 100 => 100.

This will value the expression to true as value is equal to value.

public static boolean isPowerOfTwo(final int value) {
    return value > 0 && ((value & (~value + 1)) == value);
}

generateRandomInt

Generate a random integer between Integer.MIN_VALUE and Integer.MAX_VALUE.

public static int generateRandomInt() {
    return ThreadLocalRandom.current().nextInt();
}

String

anagrams

Generates all anagrams of a string (contains duplicates).

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());
}

byteSize

Returns the length of a string in bytes.

public static int byteSize(String input) {
    return input.getBytes().length;
}

capitalize

Capitalizes the first letter of a string.

public static String capitalize(String input, boolean lowerRest) {
    return input.substring(0, 1).toUpperCase() +
            (lowerRest
                    ? input.substring(1, input.length()).toLowerCase()
                    : input.substring(1, input.length()));
}

capitalizeEveryWord

Capitalizes the first letter of every word in a string.

public static String capitalizeEveryWord(final String input) {
    return Pattern.compile("\\b(?=\\w)").splitAsStream(input)
            .map(w -> capitalize(w, false))
            .collect(Collectors.joining());
}

countVowels

Retuns number of vowels in provided string.

public static int countVowels(String input) {
    return input.replaceAll("[^aeiouAEIOU]", "").length();
}

escapeRegExp

Escapes a string to use in a regular expression.

public static String escapeRegExp(String input) {
    return Pattern.quote(input);
}

fromCamelCase

Converts a string from camelcase.

public static String fromCamelCase(String input, String separator) {
    return input
            .replaceAll("([a-z\\d])([A-Z])", "$1" + separator + "$2")
            .toLowerCase();
}

isAbsoluteUrl

Returns true if the given string is an absolute URL, false otherwise.

public static boolean isAbsoluteUrl(String url) {
    return Pattern.compile("^[a-z][a-z0-9+.-]*:").matcher(url).find();
}

isLowerCase

Checks if a string is lower case.

public static boolean isLowerCase(String input) {
    return Objects.equals(input, input.toLowerCase());
}

isUpperCase

Checks if a string is upper case.

public static boolean isUpperCase(String input) {
    return Objects.equals(input, input.toUpperCase());
}

isPalindrome

Checks if a string is palindrome.

public static boolean isPalindrome(String input) {
    String s = input.toLowerCase().replaceAll("[\\W_]", "");
    return Objects.equals(
            s,
            new StringBuilder(s).reverse().toString()
    );
}

isNumeric

Checks if a string is numeric.

public static boolean isNumeric(final String input) {
    return IntStream.range(0, input.length())
            .allMatch(i -> Character.isDigit(input.charAt(i)));
}

mask

Replaces all but the last num of characters with the specified mask character.

public static String mask(String input, int num, String mask) {
    int length = input.length();
    return num > 0
            ?
            input.substring(0, length - num).replaceAll(".", mask)
                    + input.substring(length - num)
            :
            input.substring(0, Math.negateExact(num))
                    + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
}

reverseString

Reverses a string.

public static String reverseString(String input) {
    return new StringBuilder(input).reverse().toString();
}

sortCharactersInString

Alphabetically sorts the characters in a string.

public static String sortCharactersInString(String input) {
    return Arrays.stream(input.split("")).sorted().collect(Collectors.joining());
}

splitLines

Splits a multiline string into an array of lines.

public static String[] splitLines(String input) {
    return input.split("\\r?\\n");
}

toCamelCase

Converts a string to camelcase.

public static String toCamelCase(String input) {
    Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
    List<String> matchedParts = new ArrayList<>();
    while (matcher.find()) {
        matchedParts.add(matcher.group(0));
    }
    String s = matchedParts.stream()
            .map(x -> x.substring(0, 1).toUpperCase() + x.substring(1).toLowerCase())
            .collect(Collectors.joining());
    return s.substring(0, 1).toLowerCase() + s.substring(1);
}

toKebabCase

Converts a string to kebab case.

public static String toKebabCase(String input) {
    Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
    List<String> matchedParts = new ArrayList<>();
    while (matcher.find()) {
        matchedParts.add(matcher.group(0));
    }
    return matchedParts.stream()
            .map(String::toLowerCase)
            .collect(Collectors.joining("-"));
}

match

public static List<String> match(String input, String regex) {
    Matcher matcher = Pattern.compile(regex).matcher(input);
    List<String> matchedParts = new ArrayList<>();
    while (matcher.find()) {
        matchedParts.add(matcher.group(0));
    }
    return matchedParts;
}

toSnakeCase

Converts a string to snake case.

public static String toSnakeCase(String input) {
    Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
    List<String> matchedParts = new ArrayList<>();
    while (matcher.find()) {
        matchedParts.add(matcher.group(0));
    }
    return matchedParts.stream()
            .map(String::toLowerCase)
            .collect(Collectors.joining("_"));
}

truncateString

Truncates a string up to a specified length.

public static String truncateString(String input, int num) {
    return input.length() > num
            ? input.substring(0, num > 3 ? num - 3 : num) + "..."
            : input;
}

words

Converts a given string into an array of words.

public static String[] words(String input) {
    return Arrays.stream(input.split("[^a-zA-Z-]+"))
            .filter(s -> !s.isEmpty())
            .toArray(String[]::new);
}

stringToIntegers

Converts a String of numbers separated by space to an array of ints.

public static int[] stringToIntegers(String numbers) {
        return Arrays.stream(numbers.split(" ")).mapToInt(Integer::parseInt).toArray();
}

IO

convertInputStreamToString

Converts InputStream to a String.

public static String convertInputStreamToString(final InputStream in) throws IOException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = in.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    return result.toString(StandardCharsets.UTF_8.name());
}

readFileAsString

Reads content of a file to a String

public String readFileAsString(Path path) throws IOException {
    return new String(Files.readAllBytes(path));
}

getCurrentWorkingDirectoryPath

public static String getCurrentWorkingDirectoryPath() {
    return FileSystems.getDefault().getPath("").toAbsolutePath().toString();
}

tmpDirName

Returns the value of java.io.tmpdir system property. It appends separator if not present at the end.

public static String tmpDirName() {
    String tmpDirName = System.getProperty("java.io.tmpdir");
    if (!tmpDirName.endsWith(File.separator)) {
        tmpDirName += File.separator;
    }

    return tmpDirName;
}

Exception

stackTraceAsString

Converts exception stack trace to a String.

public static String stackTraceAsString(final Throwable throwable) {
    final StringWriter sw = new StringWriter();
    throwable.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

System

osName

Gets the name of the operating system as a lower case String.

public static String osName() {
    return System.getProperty("os.name").toLowerCase();
}

isDebuggerEnabled

Checks if debugger is attached to the JVM.

public static boolean isDebuggerAttached() {
    final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    return runtimeMXBean.getInputArguments()
            .stream()
            .anyMatch(arg -> arg.contains("-agentlib:jdwp"));

}

Class

getAllInterfaces

This methods returns all the interfaces implemented by the given class and its superclasses.

This method works by concatenating two streams. The first stream is recursively built by creating a stream with the interface and all the interfaces implemented by the the interface. The second stream does the same for the super classes. The result is the concatenation of the two streams after removing the duplicates.

public static List<Class<?>> getAllInterfaces(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());
}

isInnerClass

This method checks if the specified class is an inner class or static nested class

public static boolean isInnerClass(final Class<?> cls) {
    return cls != null && cls.getEnclosingClass() != null;
}

Enum

getEnumMap

Converts to enum to Map where key is the name and value is Enum itself.

public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
    return Arrays.stream(enumClass.getEnumConstants())
            .collect(Collectors.toMap(Enum::name, Function.identity()));
}

In News

  1. Jetbrains Java Annotated Monthly covered little-java-functions in their January newsletter.
  2. JavaChannel podcast covered little-java-functions in their episode 11.
  3. little-java-functions was Github trending repository on 8th January 2018.

Thanks

This project started as a Java fork of 30-seconds-of-code. Thanks to the project collaborators for the effort.

More Repositories

1

52-technologies-in-2016

Let's learn a new technology every week. A new technology blog every Sunday in 2016.
JavaScript
7,159
star
2

99-problems

This is an adaptation of the Ninety-Nine Prolog Problems written by Werner Hett.
Java
3,302
star
3

java8-the-missing-tutorial

Java 8 for all of us
Java
1,572
star
4

strman-java

A Java 8 string manipulation library.
Java
1,354
star
5

building-java-web-apps-checklist

βœ… A checklist for building Java + Angular/React web applications in the correct way
228
star
6

flask-login-example

Flask-Login extension example
Python
154
star
7

spring-boot-maven-angular-starter

Spring Boot 2.x Angular 9 Multi-module Maven Starter Project
HTML
148
star
8

useful-microservices-reading-list

This is a useful reading list on Microservices
101
star
9

day12-face-detection

OpenCV Java example
Java
93
star
10

python-flask-docker-hello-world

Hello world Python Flask application Dockerized
Python
88
star
11

git-the-missing-tutorial

Let's master Git
79
star
12

rx-docker-client

RxJava based Docker REST API client for the JVM
Java
61
star
13

todoapp-spark

Todo app written using Spark web framework http://www.sparkjava.com/
Java
57
star
14

must-read-resources-for-java-developers

55
star
15

project-wiki-template

53
star
16

day20-stanford-sentiment-analysis-demo

Day 20 demo application
CSS
50
star
17

angular-cal-heatmap-directive

AngularJS directive for cal-heatmap https://github.com/kamisama/cal-heatmap
JavaScript
46
star
18

good-docker-resources

A list of good Docker resources on the web
44
star
19

day13-dropwizard-mongodb-demo-app

The Day 13 demo application built using DropWizard and MongoDB
Java
44
star
20

sentiment-analysis-python

sentiment analysis step by step
Python
43
star
21

play-the-missing-tutorial

Play tutorial for Humans
Scala
39
star
22

day25-tornado-demo-app

Day 25 demo app built using Tornado and deployed on OpenShift
JavaScript
39
star
23

youtube-dl-scala

Youtube Video downloader for the JVM
Scala
37
star
24

awesome-multitenancy

33
star
25

day22-spring-angularjs-demo-app

Day 22 demo application
JavaScript
32
star
26

todo-flask-openshift-quickstart

Ported Todo example app https://github.com/mitsuhiko/flask-sqlalchemy/tree/master/examples to OpenShift
CSS
26
star
27

spring-boot-sso

Single sign-on in Spring Boot applications with Spring Security OAuth
Java
26
star
28

wordgame

A simple word game built using WebSockets. The app uses JSR356 API
Java
25
star
29

day30-play-framework-demo

Day 30 demo application
JavaScript
25
star
30

flask-login-openshift-quickstart

Flask Login quickstart for OpenShift
CSS
24
star
31

day23-timelinejs-demo

Day 23 demo application
JavaScript
24
star
32

image-resolver

A Java 8 library to extract main image from a URL or website link
Java
23
star
33

nexus

Sonatype Nexus Repository Manager running in Cloud
HTML
22
star
34

nosql-to-rdbms-stories

This repository contains stories of people moving back to RDBMS from NoSQL datastores
20
star
35

urlcleaner

A Java library that can do URL normalization, unshorten URL, and URL extraction.
Java
17
star
36

software-architecture-document-template

16
star
37

day15-epoll-meteor-demo

The demo application for meteor framework
JavaScript
16
star
38

openshift-tomcat8-quickstart

Get Tomcat 8 running on OpenShift
Shell
15
star
39

day3-instant-flask-web-development-book-app

Sample app built in instant flask web development book
Python
15
star
40

30technologies30days-mobile-app

The mobile application for 30technologiesin30days challenge https://www.openshift.com/blogs/learning-30-technologies-in-30-days-a-developer-challenge
JavaScript
14
star
41

spark-openshift-quickstart

Deploys a simple todo application written using Spark on OpenShift. Application uses Java 8 and MongoDB as well
Java
13
star
42

copy-as-plain-text-chrome-extension

A simple Google Chrome extension that adds "copy as plain text" context menu option
JavaScript
13
star
43

day29-chrome-extension

Chrome extension built using Yeoman
JavaScript
12
star
44

useful-tech-radars

A list of publicly published tech radars
11
star
45

miles2run-java

Miles2Run application
JavaScript
11
star
46

cassandra-openshift-quickstart

A DIY application to get Cassandra running on OpenShift
Shell
11
star
47

localjobshtml5

The LocalJobs application using HTML 5 geo location API.
JavaScript
11
star
48

day19-emberjs-demo

Day 19 demo for EmberJS
JavaScript
10
star
49

localjobs-nodejs

LocalJobs Application written using Node.js and MongoDb
JavaScript
10
star
50

tweet-deduplication

A stream of deduplicated tweets built using RxJava and Twitter4J
Java
10
star
51

gh-stats

A node script that calculates total number of stars and forks for your repositories
JavaScript
10
star
52

git-getting-started

Getting started tutorial for Git
9
star
53

day16-goose-extractor-demo

Day 16 demo
CSS
9
star
54

conduit-on-kubernetes

Source code for "The Kubernetes Guide"
Java
9
star
55

dictionary

Simple Dictionary Application Working on top of Redis. The project is built is Spring Data Redis project
Java
9
star
56

day9-textblob-demo-openshift

Day 9 demo application using TextBlob
CSS
9
star
57

boot-angular-pagination-example-app

Spring Boot 2.x and Angular 9.x Pagination Example Application
TypeScript
9
star
58

day26-togetherjs-demo

Day 26 demo application built using Together
JavaScript
8
star
59

rx-okhttp

Thin RxJava wrapper around OkHttp library
Java
8
star
60

filesave-angular4-example

Sample project that show case how to implement file save functionality in Angular 4
TypeScript
7
star
61

useful-twitter-threads

7
star
62

adventofcode-2016

Advent Of Code 2016 Java 8 Solutions http://adventofcode.com/
Java
7
star
63

vue-docker

Dockerizing a Vue.js application
Vue
7
star
64

the-k8s-internals

Kubernetes Internals (Learning from source)
7
star
65

dailyreads

A simple read it later app powered by Twitter likes. Developed using Python, Flask, and Newspaper
HTML
6
star
66

day24-yeoman-emberjs-demo

Day 24 demo application built using Yeoman and EmberJS
JavaScript
6
star
67

jersey-openshift-quickstart

Jersey file upload example running on OpenShift
Shell
6
star
68

day1-xebiablogmonth-2015-rxjava

Demo code for Xebia's blog month day 1 blog
Java
6
star
69

java8-examples

Contains Java 8 Examples
Java
6
star
70

rxjava-examples

Reactive Java examples
Java
5
star
71

math-java

An extensive Math library for JVM
Java
5
star
72

opentracing-microservices-example

Java
5
star
73

redis-dictionary

A dictionary application built on top of Redis. Spring data is used to interact with Redis
Java
5
star
74

cookiecutter-spring-boot-ms-template

Java
4
star
75

gradle-openshift-quickstart

A quickstart to use Gradle on OpenShift
Java
4
star
76

tdd-training-java7

Java
4
star
77

k8s-workshop

Java
4
star
78

what-will-X-say

Example application of Markov Chains in Java
Java
4
star
79

30-technologies-in-30-days

4
star
80

day11-30technologies30days-mobile-app-with-push-notification

The day 11 demo app. It is a mobile application with push notification using AeroGear
JavaScript
4
star
81

blogger

Sample project to teach TDD with Spring Boot
Java
4
star
82

spring-travel-openshift-tomcat-quickstart

Spring Travel Application on OpenShift
JavaScript
4
star
83

jobfinder

Java
4
star
84

youtrack-openshift

A quickstart to get YouTrack up and running on OpenShift
Shell
4
star
85

glassfish4-openshift-quickstart

GlassFish 4 running on OpenShift
Shell
4
star
86

getbookmarks

A social bookmarking site built using backbone.js , Jax-RS, and MongoDB. The application is deployed on OpenShift https://bookmarks-cix.rhcloud.com/
JavaScript
4
star
87

notebook-part1

The part1 of notebook series
Java
3
star
88

activemq-openshift-quickstart

Quick start for running activemq in cloud
JavaScript
3
star
89

jan2014-javaee-scalability-blog

Demo application for Java EE Scalibility Blog
Java
3
star
90

rx-tweet-stream

A RxJava Observable wrapper over Twitter Streaming API
Java
3
star
91

cobertura-example

An example multi module cobertura project
Java
3
star
92

timeflake-java

Java
3
star
93

day7-gruntjs-livereload-example

GruntJS livereload in action
CSS
3
star
94

schedapp-openshift

Instant Web Development Book Sample app on OpenShift
Python
3
star
95

localjobs-python2.7

LocalJobs Application written using Python 2.7
JavaScript
3
star
96

vagrant-java-box

Configures a Ubuntu VM with Java, Tomcat, and MySQL using chef-apply
Ruby
3
star
97

june-2014-blog-pythonjs

Blog that shows how to use PythonJS to write your Node.js application in Python and deploy to OpenShift
Python
3
star
98

node-openshift-client

A node wrapper around the OpenShift REST API
JavaScript
3
star
99

tdd-demo-application

Demo application for TDD training
Java
2
star
100

profileService

A simple SOAP web service created using Spring WS 2.0
Java
2
star