Short Java 8 code snippets for all your development needs
Covert character digit to integer
Character.getNumericValue('9') or just '9' - '0'
Copies elements from one collection to another.
Collection.copy(dest,src)
Returns the number of vowels in the provided string.
int count = input_str.replaceAll("[^aeiou]", "").length()
filters out all the unique/distinct elements from the array
Arrays.stream(elements).distinct().toArray();
Safely compare double values with high precision
Double.compare(1.233333,1.2333333) == 0
The filter method is used to filter some data from the collection.
List
this operation used to traverse all the elements in the Collection
list.stream().forEach(System.out::println)
BigInteger has built in method for the calculating gcd of two numbers
int result = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValueExact();
Returns the average of integer array.
IntStream.of(arr).average()
return the starting position of first occurrences of the specified target list in source list orElse -1
Collections.indexOfSubList(list_src,list_target)
check if given string is palindrome or not
boolean res = str.equals(new StringBuilder(str).reverse().toString())
Returns the maximum element of the given collection, according to the natural ordering of its elements.
Collections.max(list)
search element in sorted collection O(logn)
int pos = Collection.binarySearch(list,key)
Returns the minimum element of the given collection, according to the natural ordering of its elements.
Collections.min(list)
filters out all the non unique elements from the array
Arrays.stream(elements).filter(el -> indexOf(elements, el) == lastIndexOf(elements, el)).toArray();
java.math.BigInteger has built in method isProbablePrime which check for primality
boolean result = BigInteger.valueOf(num).isProbablePrime(100)
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"));
reverses the order of all the elements inside the list
Collections.reverse(list)
Rotates the elements in the specified list by the specified distance.
Collections.rotate(list,key)
Randomly permutes the specified list using a default source of randomness.
Collections.shuffle(list)
return the starting position of last occurrences of the specified target list in source list orElse -1
Collections.lastIndexOfSubList(list_src,list_target)
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 a string using stream
String result = Arrays.stream(input_str.split("")).sorted().collect(Collectors.joining());
count the occurrences of element in Collection
int a = Collections.frequency(list,key);
Sorts the specified list into ascending order, according to the natural ordering of its elements.
Collections.sort(list)
Sorts the specified list according to the order induced by the specified comparator.
Collections.sort(list,Comparator.reverseOrder())