Java Functional Interfaces: Lambda Expressions and Streams

What is a Functional Interface in Java? ☕️ A functional interface is an interface that has exactly one abstract method. This single-method rule is what allows Java to support lambda expressions and makes modern Java code more expressive and readable. If you’ve used lambdas, Streams, or Comparators - you’ve already used functional interfaces. Most used functional interfaces (almost daily 👇) 🔹 Predicate<T> Used when a condition is involved. Common in filter() and validations. Predicate<Student> isTopper = s -> s.getMarks() > 80; 🔹 Function<T, R> Used for transforming one object into another. Very common with map(). Function<Student, String> getName = Student::getName; 🔹 Consumer<T> Used when you just need to perform an action. Logging, printing, saving, etc. Consumer<Student> print = System.out::println; 🔹 Supplier<T> Used when you need to provide values lazily. Helpful for defaults and object creation. Supplier<UUID> uuidSupplier = UUID::randomUUID; Other useful ones you’ll see often 👇 🔸 BiPredicate<T, U> - condition with two inputs 🔸 BiFunction<T, U, R> - two inputs, one result 🔸 UnaryOperator<T> - modify and return same type 🔸 BinaryOperator<T> - combine two values of same type 🔸 Comparator<T> - sorting and ordering logic Small interfaces, but they quietly power most modern Java applications. 💬 Comment below if you’ve used any of these in your code - even once counts 😄 #Java #Java8 #FunctionalInterfaces #Lambdas #Streams #BackendDevelopment #SpringBoot #Learning

To view or add a comment, sign in

Explore content categories