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
Collect
p33 Collect the stream values into a list.
/**
* Stream collect ...
*/
package com.minte9.lambdas.common_operations;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class Collect {
public static void main(String[] args) {
List<String> A = Stream.of("a", "b", "c")
.collect(Collectors.toList());
assertEquals(Arrays.asList("a", "b", "c"), A); // pass
}
}
Map
p34 Map each value to uppercase.
/**
* Stream map (to uppercase)
*/
package com.minte9.lambdas.common_operations;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
public class MapApp {
public static void main(String[] args) {
List<String> A, B, C;
A = asList("a", "b", "c");
B = asList("A", "B", "C");
C = new ArrayList<>();
// For
for(String s : A) {
C.add(s.toUpperCase());
}
assertEquals(B, C); // pass
// Stream
C = A.stream()
.map(x -> x.toUpperCase())
.collect(toList());
assertEquals(B, C); // pass
}
}
Filter
p38 Filter only strings that starts with a number.
/**
* Stream filter ...
*
* Strings that starts with a number (example)
*/
package com.minte9.lambdas.common_operations;
import static java.lang.Character.isDigit;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
public class Filter {
public static void main(String[] args) {
List<String> A, B, C;
A = asList("1abc");
B = asList("a", "1abc", "abc1");
C = new ArrayList<>();
// For
for(String s : A) {
if (isDigit(s.charAt(0))) {
C.add(s);
}
}
assertEquals(A, C); // pass
// Filter
C = B.stream()
.filter(x -> isDigit(x.charAt(0)))
.collect(toList());
assertEquals(A, C); // pass
}
}
Reduce
p43 Reduce is used when you want a single result from collection.
/**
* Stream reduce ...
*
* Wwhen you want a single result from collection.
* Calculate the sum (example)
*
* Imperative - using for loop
* Declarative - acc is the "accumulator" (current sum)
*/
package com.minte9.lambdas.common_operations;
import static org.junit.Assert.assertEquals;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
public class Reduce {
public static void main(String[] args) {
int sum = 0;
for(Integer i : asList(1, 2, 3)) {
sum += i;
}
assertEquals(6, sum);
int sum2 = Stream.of(1, 2, 3)
.reduce(0, (acc, x) -> acc + x);
assertEquals(6, sum2);
}
}
Comparing
p41 Comparing to find minimum or maximum of an element.
/**
* Stream Comparator ...
* Comparing to find minimum or maximum of an element (example)
*/
package com.minte9.lambdas.common_operations;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Comparing {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("John", 23),
new Student("Mary", 30),
new Student("Mike", 27)
);
Student youngest = students.stream()
.min(Comparator.comparing(x -> x.getAge()))
.get();
assertEquals(students.get(0), youngest); // pass
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
PartitioningBy
p89 Partitioning a Stream into two collections of values.
/**
* Stream partitionBy ...
*
* Partitioning a Stream into two collections of values (example)
*/
package com.minte9.lambdas.common_operations;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PartitioningBy {
public static void main(String[] args) {
Map<Boolean, List<Integer>> map;
map = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.collect(Collectors.partitioningBy(
x -> x > 3
))
;
System.out.println(map.get(true)); // [4, 5, 6, 7, 8, 9, 10]
System.out.println(map.get(false)); // [1, 2, 3]
}
}
GroupingBy
p91 Groupingby to use whatever keys you want for spliting.
/**
* Stream groupingBy ...
*
* Groupingby to use whatever keys you want for spliting.
* Use first char as key (example)
*/
package com.minte9.lambdas.common_operations;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GroupingBy {
public static void main(String[] args) {
Map<Object, List<String>> map;
map = Stream.of("ab", "bc", "ac", "bd")
.collect(Collectors.groupingBy(
x -> x.charAt(0)
))
;
System.out.println(map); // {a=[ab, ac], b=[bc, bd]}
System.out.println(map.get('a')); // [ab, ac]
System.out.println(map.get('b')); // [bc, bd]
}
}
➥ Questions
Last update: 28 days ago