Java
/
Lambdas
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
One Method
p18 A functional interface has only one abstract method (used as type for lambdas).
/**
* A functional interface has only one abstract method ...
* used as type for lambdas.
*/
package com.minte9.lambdas.functional_interfaces;
import javax.swing.JButton;
import javax.swing.JFrame;
/*
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
*/
public class OneMethod {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
/*
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
}
);
*/
button.addActionListener(event -> { // Look Here
System.out.println("Button clicked");
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
/*
abstract interface ActionListener extends EventListener {
public abstract void actionPerformed(ActionEvent arg0);
}
*/
Predicate
Functional interfaces can come in many forms.
/**
* Examples of functional interfaces, using Predicate interface.
*/
package com.minte9.lambdas.functional_interfaces;
import java.util.function.Function;
import java.util.function.Predicate;
public class Predicates {
public static void main(String[] args) {
Predicate<Integer> op1 = (x) -> (x % 2 == 0);
System.out.println(op1.test(36));
// true
Predicate<String> op2 = new Predicate<>() {
@Override public boolean test(String t) {
return t.length() > 3;
}
};
System.out.println(op2.test("abcd"));
// true
Predicate<String> op3 = t -> (t.length() > 3);
System.out.println(op3.test("abcd"));
// true
Function<Long, Long> op = x -> x + 1;
System.out.println(op.apply(20L));
// 21
}
}
/*
package java.util.function;
public abstract interface Predicate<T> {
public abstract boolean test(T arg0);
...
*/
Custom
You can create custom functional interface.
/**
* You can create custom functional interface.
*/
package com.minte9.lambdas.functional_interfaces;
public class Custom {
public static void main(String[] args) {
MyFuncInterface s1 = s -> s + "!";
MyFuncInterface s2 = s -> s + "?";
System.out.println(s1.run("Hello"));
// Hello!
System.out.println(s2.run("Hello"));
// Hello?
}
}
abstract interface MyFuncInterface {
String run(String s);
}
➥ Questions