Java

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 interface MyInterface { Message send(String message); } class Message { public Message(String message) { System.out.println(message); } } class Test { public static void main(String[] args) { MyInterface myInterface = Message::new; myInterface.send(“Hello!”); } } Output: Hello! class Message { public Message(String message) { System.out.println(message); } public Message(String message1, String message2) { System.out.println(message1 + message2); }…

Read More Constructor Reference in Java

@FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing circle…”); } public static void main(String[] args) { Test test = new Test(); Drawable drawable = () -> test.drawCircle(); drawable.draw(); } } Output: Drawing circle… @FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing…

Read More Method Reference in Java

@FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } class Test { public static void main(String[] args) { Supplier<String> supplier = () -> “Java”; System.out.println(supplier.get()); } } Output: Java class User { private String name; private String username; private String membershipType; private String address; public…

Read More Supplier Functional Interface in Java

@FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T,T,T> { /** * Returns a {@link BinaryOperator} which returns the lesser of two elements * according to the specified {@code Comparator}. * * @param <T> the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * @return a {@code…

Read More BinaryOperator Functional Interface in Java

@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 BiPredicate<T, U> { /** * Evaluates this predicate on the given arguments. * * @param t the first input argument * @param u the second input argument * @return {@code true} if the input arguments match the predicate, * otherwise {@code false} */ boolean test(T t, U u); /** * Returns a…

Read More BiPredicate Functional Interface in Java