Java
/
Collections
- 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
Streams
With Java 8 Streams we can make bulk operations.
/**
* With Java 8 Streams we can make bulk operations ...
* sequentiallly or paralel.
*
* The source for streams can be arrays, files, regex, etc.
* We don't have to create a collection in order to work with streams.
*/
package com.minte9.collections.streams;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Streams {
public static void main(String[] args) {
List<String> myList = Arrays.asList("aa", "ba", "bc");
myList
.stream()
.filter(s -> s.startsWith("b"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// BA BC
Stream.of("aa", "ba", "bc") // no collection
.filter(s -> s.startsWith("b"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// BA BC
Arrays.stream(new int[] {1, 2, 3}) // primitives
.map(n -> n * 2)
.average()
.ifPresent(System.out::println);
// 4.0
}
}
Declarative
Using declarative approach we can create code that is reusable and testable.
/**
* The Problem:
*
* Given a list of integers,
* find the square of all the even numbers
* and return them in a new list.
*/
package com.minte9.collections.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Declarative {
static List<Integer> mySquareList = new ArrayList<>();
static List<Integer> mylist = Arrays.asList(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
);
public static void main(String[] args) {
// Imperative programming
for(Integer n : mylist) {
if (n % 2 == 0) {
mySquareList.add(n * n);
}
}
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
// Declarative - no lambdas
mySquareList = mylist.stream()
.filter(
new Predicate<Integer>() {
@Override public boolean test(Integer n) {
return n % 2 == 0;
}
})
.map(
new Function<Integer, Integer>() {
@Override public Integer apply(Integer n) {
return n * n;
}
}
)
.collect(Collectors.toList());
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
// Declarative - lambdas
mySquareList = mylist.stream()
.filter(x -> x % 2 == 0)
.map(y -> y * y)
.collect(Collectors.toList());
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
}
}
Anonimous
Lambdas removed the need of writing anonymous functions.
/**
* Streams are collection of objects on which you can apply ...
* various methods while they are in the pipeline of execution.
*
* Lambdas are language constructs that remove the need of ...
* writing anonymous functions.
*/
package com.minte9.collections.streams;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Anonimous {
public static void main(String[] args) {
/**
* Filter:
* ----------------
* Only elements that meet the condition ...
* will be passed to the next function in the pipeline
*/
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(x -> x % 2 == 0)
.forEach(System.out::println);
// 2 4
/**
* Map:
* ---------------
* Used to transform an element into other element
*/
List<String> letters = Arrays.asList("a", "b", "c");
letters.stream()
.map(x -> x.toUpperCase())
.forEach(System.out::println);
// A B C
/**
* ForEach:
* ---------------
* Runs for every element in the stream
*/
List<Integer> myNumbers = Arrays.asList(1, 2, 3, 4, 5);
myNumbers.stream()
.filter(x -> x % 2 == 0)
.map(x -> x * x)
.forEach(System.out::println);
// 4 16
/**
* Collect:
* ----------------
* Collects every element in the stream to another collection
*/
List<String> myList = Arrays.asList("London", "Paris", "London");
Set<String> mySet = myList.stream()
.filter(x -> x.length() % 2 == 0)
.collect(Collectors.toSet()); // no duplicates (Set)
System.out.println(mySet);
// [London]
}
}
➥ Questions