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
Responsibility
p177 Every class or method should have only one reason to change.
/**
* Primes counting App ...
*
* Multiple responsibilities: counting & checking
*
* A prime number is a natural number greater than 1 ...
* that is not a product of two smaller natural numbers.
*/
package com.minte9.lambdas.single_responsibility;
import static org.junit.Assert.assertEquals;
public class Primes {
public static void main(String[] args) {
assertEquals(4, noOfPrimes(10)); // pass
}
public static long noOfPrimes(int limit) {
int n = 0;
for (int i=2; i<limit; i++) { // counting responsibility
for (int j=2; j<i; j++) { // checking responsibility
if (i%j == 0) { // is prime
n++; break;
}
}
}
return n;
}
}
Encapsulation
According to SR principle this responsability should also be encapsulated.
/**
* A class or method should have only one reason to change.
* Also, this responsability should be encapsulated.
*
* Counting prime numbers - no looping encapsuation!
*/
package com.minte9.lambdas.single_responsibility;
import static org.junit.Assert.assertEquals;
public class Encapsulation {
public static void main(String[] args) {
assertEquals(4, countPrimes(10)); // pass
}
public static long countPrimes(int limit) {
int n = 0;
for (int i=2; i<limit; i++) { // counting loop - no encapsulation!
if (isPrime(i)) n++;
}
return n;
}
private static boolean isPrime(int n) { // check
for (int i=2; i<n; i++) {
if (n%i == 0) return false;
}
return true;
}
}
Lambdas
p179 Lambda expressions make it a lot more easier to implement SR principle.
/**
* With lambdas you can easily make methods that are inline with SRP ...
* one responsability and encapsulation.
*
* Later, we can speeed up the time by using more CPU resources ...
* with parallel method, without changing any other code.
*/
package com.minte9.lambdas.single_responsibility;
import static org.junit.Assert.assertEquals;
import java.util.stream.IntStream;
public class Lambdas {
public static void main(String[] args) {
assertEquals(25, MyNumbers.countPrimes(100)); // pass
System.out.println(
MyNumbers.countBigPrimes(200000) // 17984
);
}
}
class MyNumbers {
public static long countPrimes(int limit) {
return IntStream.range(2, limit)
.filter(MyNumbers::isPrime)
.count()
;
}
private static boolean isPrime(int number) {
return IntStream.range(2, number)
.allMatch(x -> number % x != 0)
;
}
public static long countBigPrimes(int limit) {
return IntStream.range(2, limit)
.parallel() // Look Here
.filter(MyNumbers::isPrime)
.count()
;
}
}
➥ Questions