Java 8 - Predefined Functional Interface - Supplier
Predefined Functional Interface - Supplier
* I wont provide any input, But I need some output based on some operation.
* Eg. Provide some random value, Get student details etc.,
Syntax:
interface Supplier<R>
{
public R get();
}
Eg: Supplier to supply System Date
class Test
{
public static void main(String[] args)
{
Supplier<Date> s=()->new Date();
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
Output:
Sun Aug 05 21:34:06 IST 2018
Sun Aug 05 21:34:06 IST 2018
Sun Aug 05 21:34:06 IST 2018
Eg: Supplier to supply Random Passwords
Rules:
1. length should be 8 characters
2. 2,4,6,8 places only digits
3. 1,3,5,7 only Capital Uppercase characters,@,#,$
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 pwd="";
for(int i =1;i<=8;i++)
{
if(i%2==0)
{
pwd=pwd+d.get();
}
else
{
pwd=pwd+c.get();
}
}
return pwd;
};
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
Output:
G2W3Y8W5
W7M8$0L3
T5T5N2F3
O9N2L0V2
A4I1$1P6
* I wont provide any input, But I need some output based on some operation.
* Eg. Provide some random value, Get student details etc.,
Syntax:
interface Supplier<R>
{
public R get();
}
Eg: Supplier to supply System Date
class Test
{
public static void main(String[] args)
{
Supplier<Date> s=()->new Date();
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
Output:
Sun Aug 05 21:34:06 IST 2018
Sun Aug 05 21:34:06 IST 2018
Sun Aug 05 21:34:06 IST 2018
Eg: Supplier to supply Random Passwords
Rules:
1. length should be 8 characters
2. 2,4,6,8 places only digits
3. 1,3,5,7 only Capital Uppercase characters,@,#,$
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 pwd="";
for(int i =1;i<=8;i++)
{
if(i%2==0)
{
pwd=pwd+d.get();
}
else
{
pwd=pwd+c.get();
}
}
return pwd;
};
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
}
}
Output:
G2W3Y8W5
W7M8$0L3
T5T5N2F3
O9N2L0V2
A4I1$1P6
Comments
Post a Comment