Java 8 — Supplier | Code Factory

  • For this type of requirements we should go for Supplier
  • Supplier can be used to supply items(objects)
  • Supplier won’t take any input and it will always supply objects
  • Supplier Functional Interface contains only one method get()
@FunctionalInterface
public interface Supplier<T> {
T get();
}

Program to generate random name :

package com.codeFactory.supplier;import java.util.function.Supplier;public class Test {
public static void main(String... args) {
Supplier<String> s = () -> {
String[] s1 = {"Narendra", "Amit", "Yogi", "Rahul"};
int x = (int) (Math.random() * 4);
return s1[x];
};

System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
Narendra
Amit
Rahul

Program to get System Date by using Supplier :

package com.codeFactory.supplier;import java.util.Date;
import java.util.function.Supplier;
public class Test {
public static void main(String... args) {
Supplier<Date> s = () -> new Date();
System.out.println(s.get());
}
}
Wed Apr 29 13:27:58 IST 2020

Program to generate 5 digit random OTP :

package com.codeFactory.supplier;import java.util.function.Supplier;public class Test {
public static void main(String... args) {
Supplier<String> otp = () -> {
String s = "";
for(int i=0; i<5; i++) {
s = s + (int) (Math.random() * 10);
}
return s;
};

System.out.println(otp.get());
System.out.println(otp.get());
System.out.println(otp.get());
}
}
33325
89449
53473

Program to generate random password :

  • length should be 8 characters
  • 2, 4, 6, 8 places only digits
  • 1, 3, 5, 7 only Capital Uppercase characters, @, #, $
package com.codeFactory.supplier;import java.util.function.Supplier;public class Test {
public static void main(String... args) {
Supplier<String> s = () -> {
String symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#$@";
Supplier<Integer> d = () -> (int)(Math.random() * 10);
Supplier<Character> c = () -> symbols.charAt((int)(Math.random() * 29));
String psw = "";
for(int i=0; i<8; i++) {
if(i%2 == 0) {
psw += d.get();
} else {
psw += c.get();
}
}
return psw;
};
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
9V3#4I7R
7I9Z2Q5$
8D8N0Z3A

Comparison Table of Predicate, Function, Consumer and Supplier :

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How we used No-code platform to create the largest booking site for Nutritionist

A Simple (But Comprehensive) Guide on Updating Your System Bios

Setup a Flutter CI/CD with GitLab CI — Part 3

Reduce your docker image size with .dockerignore

How to Serve Responsive Images for Mobile, Tablet, and Desktop

New QuickSwap Vaults, Feed The Cat Contest, and Moar! 🙀

Header Bidding: A Rapid Evolution With Its Own Downsides

BOMB Shell: Community vote, Beefy and more!

BOMB Shell: May 2 Update

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Singleton Design Pattern in Java

JAVA: Exception Handling

Functional Interfaces in Java

The Java Library