Skip to content

Commit 1cea722

Browse files
Upload local code
1 parent 6f98679 commit 1cea722

14 files changed

+878
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
target/
3+
*.iml

pom.xml

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.manueldeveloper</groupId>
7+
<artifactId>streams</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<name>streams</name>
11+
12+
<url>https://stackify.com/streams-guide-java-8/</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
19+
<junit.version>4.11</junit.version>
20+
<validation.version>1.1.0.Final</validation.version>
21+
<hamcrest.version>1.3</hamcrest.version>
22+
<assertj.version>3.6.1</assertj.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>junit</groupId>
28+
<artifactId>junit</artifactId>
29+
<version>${junit.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.hamcrest</groupId>
34+
<artifactId>hamcrest-core</artifactId>
35+
<version>${hamcrest.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.hamcrest</groupId>
40+
<artifactId>hamcrest-library</artifactId>
41+
<version>${hamcrest.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.hamcrest</groupId>
46+
<artifactId>hamcrest-all</artifactId>
47+
<version>${hamcrest.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.assertj</groupId>
52+
<artifactId>assertj-core</artifactId>
53+
<version>${assertj.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>javax.validation</groupId>
58+
<artifactId>validation-api</artifactId>
59+
<version>${validation.version}</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
66+
<plugins>
67+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
68+
<plugin>
69+
<artifactId>maven-clean-plugin</artifactId>
70+
<version>3.1.0</version>
71+
</plugin>
72+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
73+
<plugin>
74+
<artifactId>maven-resources-plugin</artifactId>
75+
<version>3.0.2</version>
76+
</plugin>
77+
<plugin>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<version>3.8.0</version>
80+
</plugin>
81+
<plugin>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>2.22.1</version>
84+
</plugin>
85+
<plugin>
86+
<artifactId>maven-jar-plugin</artifactId>
87+
<version>3.0.2</version>
88+
</plugin>
89+
<plugin>
90+
<artifactId>maven-install-plugin</artifactId>
91+
<version>2.5.2</version>
92+
</plugin>
93+
<plugin>
94+
<artifactId>maven-deploy-plugin</artifactId>
95+
<version>2.8.2</version>
96+
</plugin>
97+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
98+
<plugin>
99+
<artifactId>maven-site-plugin</artifactId>
100+
<version>3.7.1</version>
101+
</plugin>
102+
<plugin>
103+
<artifactId>maven-project-info-reports-plugin</artifactId>
104+
<version>3.0.0</version>
105+
</plugin>
106+
</plugins>
107+
</pluginManagement>
108+
<plugins>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<configuration>
113+
<source>8</source>
114+
<target>8</target>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</build>
119+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.manueldeveloper;
2+
3+
public class Employee {
4+
5+
private Integer id;
6+
private String name;
7+
private Double salary;
8+
9+
public Employee(Integer id, String name, Double salary) {
10+
this.id = id;
11+
this.name = name;
12+
this.salary = salary;
13+
}
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public Double getSalary() {
32+
return salary;
33+
}
34+
35+
public void setSalary(Double salary) {
36+
this.salary = salary;
37+
}
38+
39+
public void salaryIncrement(Double percentage) {
40+
Double newSalary = salary + ((percentage * salary)/100);
41+
setSalary(newSalary);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "Employee{" +
47+
"id=" + id +
48+
", name='" + name + '\'' +
49+
", salary=" + salary +
50+
'}';
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.manueldeveloper;
2+
3+
import java.util.List;
4+
5+
public class EmployeeRepository {
6+
7+
private List<Employee> employeeList;
8+
9+
public EmployeeRepository(List<Employee> employeeList) {
10+
this.employeeList = employeeList;
11+
}
12+
13+
public Employee findById(Integer id) {
14+
for (Employee employee : employeeList) {
15+
if(employee.getId().equals(id)) {
16+
return employee;
17+
}
18+
}
19+
20+
return null;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.manueldeveloper;
2+
3+
import com.manueldeveloper.config.EmployeeFactory;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.NoSuchElementException;
10+
import java.util.stream.Collectors;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class ComparisonOperationsTest {
15+
16+
private final EmployeeFactory employeeFactory = new EmployeeFactory();
17+
private List<Employee> employees = employeeFactory.getEmployees();
18+
19+
20+
/**
21+
* sorted -> "Pedicate" function & Lazy operation
22+
*/
23+
@Test
24+
public void sorted_operation() {
25+
// When
26+
final List<Employee> result = employees.stream()
27+
.sorted((e1, e2) -> e1.getName().compareTo(e2.getName()))
28+
.collect(Collectors.toList());
29+
30+
// Then
31+
assertEquals("Bill Gates", result.get(0).getName());
32+
assertEquals("Jeff Bezos", result.get(1).getName());
33+
assertEquals("Mark Zuckerberg", result.get(2).getName());
34+
}
35+
36+
/**
37+
* sorted -> "Pedicate" function & Lazy operation
38+
*/
39+
@Test
40+
public void sorted_with_comparing_operation() {
41+
// When
42+
final List<Employee> result = employees.stream()
43+
.sorted(Comparator.comparing(Employee::getName))
44+
.collect(Collectors.toList());
45+
46+
// Then
47+
assertEquals("Bill Gates", result.get(0).getName());
48+
assertEquals("Jeff Bezos", result.get(1).getName());
49+
assertEquals("Mark Zuckerberg", result.get(2).getName());
50+
}
51+
52+
/**
53+
* min > "Predicate" function & Terminal operation
54+
*/
55+
@Test
56+
public void min_operation() {
57+
// When
58+
final Employee result = employees.stream()
59+
.min(Comparator.comparingInt(Employee::getId))
60+
.orElseThrow(NoSuchElementException::new);
61+
62+
// Then
63+
assertEquals(Integer.valueOf(1), result.getId());
64+
}
65+
66+
/**
67+
* max > "Predicate" function & Terminal operation
68+
*/
69+
@Test
70+
public void max_operation() {
71+
// When
72+
final Employee result = employees.stream()
73+
.max(Comparator.comparingDouble(Employee::getSalary))
74+
.orElseThrow(NoSuchElementException::new);
75+
76+
// Then
77+
assertEquals(Double.valueOf(300000.0), result.getSalary());
78+
}
79+
80+
/**
81+
* allMatch > Predicate function & Terminal operation
82+
*/
83+
@Test
84+
public void allMatch_operation() {
85+
// Given
86+
List<Integer> intList = Arrays.asList(2, 4, 5, 6, 8);
87+
88+
// When
89+
final boolean result = intList.stream().allMatch(i -> i % 2 == 0);
90+
91+
// Then
92+
assertEquals(false, result);
93+
}
94+
95+
/**
96+
* anyMatch > Predicate function & Terminal operation
97+
*/
98+
@Test
99+
public void anyMatch_operation() {
100+
// Given
101+
List<Integer> intList = Arrays.asList(2, 4, 5, 6, 8);
102+
103+
// When
104+
final boolean result = intList.stream().anyMatch(i -> i % 2 == 0);
105+
106+
// Then
107+
assertEquals(true, result);
108+
}
109+
110+
/**
111+
* noneMatch > Predicate function & Terminal operation
112+
*/
113+
@Test
114+
public void noneMatch_operation() {
115+
// Given
116+
List<Integer> intList = Arrays.asList(2, 4, 5, 6, 8);
117+
118+
// When
119+
final boolean result = intList.stream().noneMatch(i -> i % 3 == 0);
120+
121+
// Then
122+
assertEquals(false, result);
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.manueldeveloper;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.io.PrintWriter;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class FileStreamTest {
17+
18+
private static String fileName = "src/test/resources/test.txt";
19+
20+
@Test
21+
public void stream_to_file() throws IOException {
22+
// Given
23+
String[] words = {
24+
"hello",
25+
"refer",
26+
"world",
27+
"level"
28+
};
29+
30+
// When
31+
PrintWriter writer = new PrintWriter(Files.newBufferedWriter(Paths.get(fileName)));
32+
Stream.of(words)
33+
.peek(System.out::println)
34+
.forEach(writer::println);
35+
writer.close();
36+
}
37+
38+
@Test
39+
public void file_to_stream() throws IOException {
40+
// Given
41+
final List<String> expected = Arrays.asList("refer", "level");
42+
43+
// When
44+
final List<String> result = getPalindrome(Files.lines(Paths.get(fileName)), 5);
45+
46+
// Then
47+
assertEquals(expected, result);
48+
}
49+
50+
private List<String> getPalindrome(Stream<String> stream, int length) {
51+
return stream.filter(s -> s.length() == length)
52+
.filter(s -> s.compareToIgnoreCase(new StringBuilder(s).reverse().toString()) == 0)
53+
.collect(Collectors.toList());
54+
}
55+
}

0 commit comments

Comments
 (0)