Java

We have the isPresent() and ifPresent() methods to help us detect if Optional contains some value or not. Optional isPresent() method The isPresent() method returns true if there is a value present in the Optional, otherwise false.Using it, we can avoid getting an exception when trying to get the value from an empty Optional. So…

Read More Optional – ifPresent() and isPresent() methods

class Test { public static void main(String[] args) { Optional optional = Optional.empty(); // creates an empty Optional if (optional.isEmpty()) { System.out.println(“It is an empty Optional.”); } else { System.out.println(“Optional is not empty.”); } } } Output: It is an empty Optional. 2. Optional.of(T value) method class Test { public static void main(String[] args) {…

Read More Introduction to Optional class in Java

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“Paris”, “Bern”, “London”, “Tokyo”, “Boston”)); Map<Integer, List<String>> resultMap = cities.stream() .collect(Collectors.groupingBy(String::length)); System.out.println(resultMap); } } Output: {4=[Bern], 5=[Paris, Tokyo], 6=[London, Boston]} class Test { public static void main(String[]…

Read More Streams – groupingBy() operation

class Test { public static void main(String[] args) { List<String> words = new ArrayList<>(Arrays.asList(“Hello”, “World”, “of”, “Java!”)); String result = words.stream().collect(Collectors.joining()); System.out.println(result); String resultWithDelimiters = words.stream().collect(Collectors.joining(“-“)); // delimiter System.out.println(resultWithDelimiters); String resultWithDelimitersAndPrefixAndSuffix = words.stream().collect(Collectors.joining(“-“, “(“, “)”)); // delimiter, prefix, suffix System.out.println(resultWithDelimitersAndPrefixAndSuffix); } } Output: HelloWorldofJava! Hello-World-of-Java! (Hello-World-of-Java!) class Test { public static void main(String[] args) {…

Read More Streams – collect() operation

DoubleStream.of(2.4); DoubleStream.of(7.1, 10.5, 12.7); DoubleStream.iterate(0, i -> i + 2).limit(10); DoubleStream.generate(() -> Math.random() * 5).limit(10); class Test { public static void main(String[] args) { DoubleStream doubleStream = DoubleStream.of(1.2, 3.1, 7.2, 5.3, 9.8); doubleStream.forEach(item -> System.out.print(item + ” “)); } } Output: 1.2 3.1 7.2 5.3 9.8 class Test { public static void main(String[] args) {…

Read More Streams – DoubleStream class

LongStream.of(7); LongStream.of(7, 8, 9); LongStream.range(1, 3); LongStream.rangeClosed(1, 3); LongStream.iterate(0, i -> i + 2).limit(10); LongStream.generate(() -> (long) (Math.random() * 5)).limit(10); class Test { public static void main(String[] args) { LongStream longStream = LongStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); longStream.forEach(item -> System.out.print(item + ” “)); } } Output: 1 2 3 4…

Read More Streams – LongStream class

IntStream.of(5); IntStream.of(1, 2, 3); IntStream.range(1, 3); IntStream.rangeClosed(1, 3); IntStream.iterate(0, i -> i + 2).limit(10); IntStream.generate(() -> (int) (Math.random() * 5)).limit(10); class Test { public static void main(String[] args) { IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); intStream.forEach(item -> System.out.print(item + ” “)); } } Output: 1 2 3 4…

Read More Streams – IntStream class

Optional<T> findAny() class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 7, 9, 15, 17)); Optional<Integer> resultOptional = numbers.stream().findAny(); if (resultOptional.isPresent()) { // checking if optional contains any value System.out.println(“The element returned from the stream: ” + resultOptional.get()); // getting the value from Optional } else { System.out.println(“The stream is…

Read More Streams – findAny() operation

T reduce(T identity, BinaryOperator<T> accumulator); class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 5, 7, 9)); int result = numbers.stream() .reduce(1, (num1, num2) -> num1 * num2); System.out.println(“The result is: ” + result); } } Output: The result is: 945 class Test { public static void main(String[] args) {…

Read More Streams – reduce() operation

boolean noneMatch(Predicate<? super T> predicate) class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(12, 17, 20, 8, 92, 15, 9, 31, 7)); System.out.println(numbers.stream() .noneMatch(number -> number < 5)); } } Output: true class Test { public static void main(String[] args) { System.out.println(getStudents().stream() .noneMatch(student -> student.getGrade() < 7)); } private static…

Read More Streams – noneMatch() operation

boolean anyMatch(Predicate<? super T> predicate) class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(20, 12, 12, 8, 9, 5, 32, 19)); System.out.println(numbers.stream() .anyMatch(number -> number > 25)); } } Output: true class Test { public static void main(String[] args) { System.out.println(getStudents().stream() .anyMatch(student -> student.getGrade() > 8)); } private static List<Student>…

Read More Streams – anyMatch() operation

boolean allMatch(Predicate<? super T> predicate) class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)); System.out.println(numbers.stream() .allMatch(number -> number % 2 == 0)); } } Output: true class Test { public static void main(String[] args) { boolean areAllPremiumMembers = getAllUsers().stream() .allMatch(user ->…

Read More Streams – allMatch() operation

Stream<T> limit(long maxSize) Stream<T> skip(long n) class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); List<Integer> resultList = numbers.stream() .limit(5) .map(num -> num * 2) .collect(Collectors.toList()); System.out.println(resultList); } } Output: [2, 4, 6, 8, 10] class Test { public static void…

Read More Streams – limit() and skip() operations

Optional<T> min(Comparator<? super T> comparator); class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 98, 72, 48, 3, 10, 14, 42, 97, 24)); int minNumber = numbers.stream() .min(Comparator.comparing(Integer::valueOf)) .get(); System.out.println(“Minimum number is: ” + minNumber); } } Output: Minimum number is: 3 class Student { private String firstName; private…

Read More Streams – max() and min() operations

class Test { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(“John”, “Alex”, “Megan”, “Bobby”, “Xavier”)); List<String> orderedNames = names.stream() .sorted() .collect(Collectors.toList()); System.out.println(orderedNames); } } Output: [Alex, Bobby, John, Megan, Xavier] class Student implements Comparable<Student> { private String firstName; private String lastName; private int grade; public Student(String firstName, String lastName, int grade) {…

Read More Streams – sorted() operation