30s of Java 8

Short Java 8 code snippets for all your development needs

Char digit to Integer

Covert character digit to integer

Character.getNumericValue('9') or just '9' - '0'

Copy one collection to another

Copies elements from one collection to another.

Collection.copy(dest,src)

Count Vowels

Returns the number of vowels in the provided string.

int count = input_str.replaceAll("[^aeiou]", "").length()

Distinct values of Array

filters out all the unique/distinct elements from the array

Arrays.stream(elements).distinct().toArray();

Safe compare Double values

Safely compare double values with high precision

Double.compare(1.233333,1.2333333) == 0

Filter

The filter method is used to filter some data from the collection.

List temp = list.stream().filter(e->e%2 == 0).collect(Collectors.toList());

forEach

this operation used to traverse all the elements in the Collection

list.stream().forEach(System.out::println)

GCD Using BigInteger

BigInteger has built in method for the calculating gcd of two numbers

int result = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValueExact();

Get Average

Returns the average of integer array.

IntStream.of(arr).average()

Index of SubList

return the starting position of first occurrences of the specified target list in source list orElse -1

Collections.indexOfSubList(list_src,list_target)

isPalindrome

check if given string is palindrome or not

boolean res = str.equals(new StringBuilder(str).reverse().toString())

Max of Collection

Returns the maximum element of the given collection, according to the natural ordering of its elements.

Collections.max(list)

Binary search in sorted collection

search element in sorted collection O(logn)

int pos = Collection.binarySearch(list,key)

Min of Collection

Returns the minimum element of the given collection, according to the natural ordering of its elements.

Collections.min(list)

Filter non Unique

filters out all the non unique elements from the array

Arrays.stream(elements).filter(el -> indexOf(elements, el) == lastIndexOf(elements, el)).toArray();

Check for Primality Using BigInteger

java.math.BigInteger has built in method isProbablePrime which check for primality

boolean result = BigInteger.valueOf(num).isProbablePrime(100)

Python 'str'*5 in java

There's no built in method for string repeat in java but we can achieve this using streams.

String res = String.join("",Collections.ncopies(ntimes,"string"));

Reverse the order of Elements

reverses the order of all the elements inside the list

Collections.reverse(list)

Rotate

Rotates the elements in the specified list by the specified distance.

Collections.rotate(list,key)

Random Permutation of List

Randomly permutes the specified list using a default source of randomness.

Collections.shuffle(list)

Last Index of SubList

return the starting position of last occurrences of the specified target list in source list orElse -1

Collections.lastIndexOfSubList(list_src,list_target)

Collect

The collect method is used to return the result of the intermediate operations performed on the stream

Set square = list.stream().collect(Collectors.toSet());

Sort characters in String

sort characters in a string using stream

String result = Arrays.stream(input_str.split("")).sorted().collect(Collectors.joining());

Count the frequency

count the occurrences of element in Collection

int a = Collections.frequency(list,key);

Sort the Collection

Sorts the specified list into ascending order, according to the natural ordering of its elements.

Collections.sort(list)

Sort the Collection with custom comparator

Sorts the specified list according to the order induced by the specified comparator.

Collections.sort(list,Comparator.reverseOrder())