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
Default
p70 Java 8 changes required the introduction of default methods in interfaces.
/**
* One of the biggest changes in Java 8 is to collections library.
*
* It required the introduction of a new concept, ...
* default methods in interfaces.
*
* A dfault methods allows body implementations.
* Classes implementations win over default methods.
*/
package com.minte9.lambdas.default_methods;
public class DefaultMethod {
public static void main(String[] args) {
new A().hello(); // Hello default
new B().hello(); // Hello B
}
}
interface MyInterface {
public default void hello() {
System.out.println("Hello default");
}
}
class A implements MyInterface { // Look Here
// no implementation
}
class B implements MyInterface {
@Override public void hello() {
System.out.println("Hello B");
}
}
Functional
Default methods can be used in lambda expresions.
/**
* Default methods can be used in lambda expresions.
* Functional interface must contain only one abstract method.
*/
package com.minte9.lambdas.default_methods;
import java.util.Arrays;
import java.util.List;
public class Functional {
public static void main(String[] args) {
List<MyValue> myList = Arrays.asList(
() -> 1,
() -> 3,
() -> 2 // lambda: get()
);
int maxDoubled = myList.stream()
.mapToInt(x -> x.getDouble()) // lambda: default method
.max()
.orElse(0)
;
System.out.println(maxDoubled); // 6
}
}
@FunctionalInterface interface MyValue {
int get(); // abstract
default int getDouble() {
return get() * 2;
}
}
Object
Default methods can be accessed only with object references!
/**
* Calling a default method in lambas without object reference ...
* doesn't work!
*/
package com.minte9.lambdas.default_methods;
import static org.junit.Assert.assertEquals;
public class Object {
public static void main(String[] args) {
Formula a = x -> x * 2;
assertEquals(4, a.calculate(2));
// pass
Formula b = x -> a.sqrt(x); // Look Here
assertEquals(4, b.calculate(16));
// pass
//Formula c = x -> sqrt(x); // Not working
}
}
@FunctionalInterface interface Formula {
abstract int calculate(int x);
default int sqrt(int x) {
return (int) Math.sqrt(x);
}
}
Super
p74 Be careful when using multiple implementations and default method.
/**
* Interfaces are subject to multiple inheritance.
* Java compiler might not know which method to choose.
*
* In this case you must override the defaul method ...
* and provide the choosen interface method
*/
package com.minte9.lambdas.default_methods;
public class Super {
public static void main(String[] args) {
String msg = new myClass().hello();
System.out.println(msg);
// Hello A
}
}
interface AI {
public default String hello() { return "Hello A"; }
}
interface BI {
public default String hello() { return "Hello B"; }
}
class myClass implements AI, BI {
@Override public String hello() {
return AI.super.hello(); // Look Here
}
}
➥ Questions
Last update: 28 days ago