Search results for: Open API

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

class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 7, 9, 22, 19, 18, 47, 3, 12, 29, 17, 44, 78, 99)); long totalCount = numbers.stream().count(); System.out.println(“Total count of numbers: ” + totalCount); } } Output: Total count of numbers: 14 class Student { private String firstName; private String lastName;…

Read More Streams – count() operation

class Test { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(“Megan”, “John”, “Melissa”, “Tom”, “John”, “Megan”, “Steve”, “Tom”)); List<String> uniqueNames = names.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueNames); } } Output: [Megan, John, Melissa, Tom, Steve] class User { private String name; private String username; private String membershipType; private String address; public User(String name, String…

Read More Streams – distinct() operation

Stream<T> filter(Predicate<? super T> predicate) 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> filteredNumbers = numbers.stream() .filter(number -> number % 2 == 0) .collect(Collectors.toList()); System.out.println(filteredNumbers); } } Output: [2, 4, 6, 8, 10] class Student { private String firstName;…

Read More Streams – filter() operation

class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“New York”, “Berlin”, “Bangkok”, “London”)); List<String> modifiedList = cities.stream() .map(String::toUpperCase) .collect(Collectors.toList()); modifiedList.forEach(System.out::println); } } Output: NEW YORK BERLIN BANGKOK LONDON List<List<String>> cities = new ArrayList<>(); cities.add(new ArrayList<>(Arrays.asList(“Paris”, “London”))); cities.add(new ArrayList<>(Arrays.asList(“New York”, “Berlin”))); class Test { public static void main(String[] args) { List<List<String>>…

Read More Streams – flatMap() operation

class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“New York”, “Paris”, “London”, “Monte Carlo”, “Berlin”)); cities.stream() .map(city -> city.toUpperCase()) .forEach(city -> System.out.println(city)); } } Output: NEW YORK PARIS LONDON MONTE CARLO BERLIN class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“New York”, “Paris”, “London”, “Monte…

Read More Streams – map() operation

@FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { /** * Returns a unary operator that always returns its input argument. * * @param <T> the type of the input and output of the operator * @return a unary operator that always returns its input argument */ static <T> UnaryOperator<T> identity() { return t -> t;…

Read More UnaryOperator Functional Interface in Java

@FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this…

Read More Predicate Functional Interface in Java

@FunctionalInterface public interface BiConsumer<T, U> { /** * Performs this operation on the given arguments. * * @param t the first input argument * @param u the second input argument */ void accept(T t, U u); /** * Returns a composed {@code BiConsumer} that performs, in sequence, this * operation followed by the {@code after}…

Read More BiConsumer Functional Interface in Java

Java has evolved over the years to support functional programming, which is a programming paradigm that emphasizes writing code in terms of functions. In Java, functional programming is made possible with the help of functional interfaces, which are interfaces that define a single abstract method. Functional interfaces can be implemented using lambda expressions or method…

Read More Master functional interfaces in Java for efficient code

Lambda expressions are one of the most significant additions to the Java programming language in recent years. They were introduced in Java 8 and are a way to write more concise and expressive code. Lambda expressions allow you to define and pass around blocks of code, known as functional interfaces, making it easier to write…

Read More Master Lambda Expressions and Enhance Your Java Programming

class Test { public static void main(String[] args) { List<Integer> integers = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10)); List<Integer> resultList = new ArrayList<>(); for (Integer integer : integers) { if (!resultList.contains(integer)) { resultList.add(integer); } } System.out.println(resultList); } } Output: [1, 2, 3, 4, 5, 6, 7, 8,…

Read More Imperative VS Declarative Programming Part 2