Skip to content

Commit 1dfeb49

Browse files
author
Swapan Pramanick
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-2221
2 parents 4c5c380 + 90978bf commit 1dfeb49

76 files changed

Lines changed: 1391 additions & 118 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core-java-8/findItemsBasedOnValues/.gitignore‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

core-java-8/findItemsBasedOnValues/src/findItems/FindItemsBasedOnValues.java renamed to core-java-collections/src/test/java/com/baeldung/findItems/FindItemsBasedOnOtherStreamUnitTest.java

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,89 @@
1-
package findItems;
2-
3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.text.ParseException;
6-
import java.util.ArrayList;
7-
import java.util.Collections;
8-
import java.util.List;
9-
import java.util.stream.Collectors;
10-
11-
import org.junit.Test;
12-
13-
public class FindItemsBasedOnValues {
14-
15-
List<Employee> EmplList = new ArrayList<Employee>();
16-
List<Department> deptList = new ArrayList<Department>();
17-
18-
public static void main(String[] args) throws ParseException {
19-
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
20-
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
21-
}
22-
23-
@Test
24-
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
25-
Integer expectedId = 1002;
26-
27-
populate(EmplList, deptList);
28-
29-
List<Employee> filteredList = EmplList.stream()
30-
.filter(empl -> deptList.stream()
31-
.anyMatch(dept -> dept.getDepartment()
32-
.equals("sales") && empl.getEmployeeId()
33-
.equals(dept.getEmployeeId())))
34-
.collect(Collectors.toList());
35-
36-
assertEquals(expectedId, filteredList.get(0)
37-
.getEmployeeId());
38-
}
39-
40-
private void populate(List<Employee> EmplList, List<Department> deptList) {
41-
Employee employee1 = new Employee(1001, "empl1");
42-
Employee employee2 = new Employee(1002, "empl2");
43-
Employee employee3 = new Employee(1003, "empl3");
44-
45-
Collections.addAll(EmplList, employee1, employee2, employee3);
46-
47-
Department department1 = new Department(1002, "sales");
48-
Department department2 = new Department(1003, "marketing");
49-
Department department3 = new Department(1004, "sales");
50-
51-
Collections.addAll(deptList, department1, department2, department3);
52-
}
53-
}
54-
55-
class Employee {
56-
Integer employeeId;
57-
String employeeName;
58-
59-
public Employee(Integer employeeId, String employeeName) {
60-
super();
61-
this.employeeId = employeeId;
62-
this.employeeName = employeeName;
63-
}
64-
65-
public Integer getEmployeeId() {
66-
return employeeId;
67-
}
68-
69-
public String getEmployeeName() {
70-
return employeeName;
71-
}
72-
73-
}
74-
75-
class Department {
76-
Integer employeeId;
77-
String department;
78-
79-
public Department(Integer employeeId, String department) {
80-
super();
81-
this.employeeId = employeeId;
82-
this.department = department;
83-
}
84-
85-
public Integer getEmployeeId() {
86-
return employeeId;
87-
}
88-
89-
public String getDepartment() {
90-
return department;
91-
}
92-
1+
package com.baeldung.findItems;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.text.ParseException;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
import org.junit.Test;
12+
13+
public class FindItemsBasedOnOtherStreamUnitTest {
14+
15+
private List<Employee> employeeList = new ArrayList<Employee>();
16+
17+
private List<Department> departmentList = new ArrayList<Department>();
18+
19+
@Test
20+
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
21+
Integer expectedId = 1002;
22+
23+
populate(employeeList, departmentList);
24+
25+
List<Employee> filteredList = employeeList.stream()
26+
.filter(empl -> departmentList.stream()
27+
.anyMatch(dept -> dept.getDepartment()
28+
.equals("sales") && empl.getEmployeeId()
29+
.equals(dept.getEmployeeId())))
30+
.collect(Collectors.toList());
31+
32+
assertEquals(expectedId, filteredList.get(0)
33+
.getEmployeeId());
34+
}
35+
36+
private void populate(List<Employee> EmplList, List<Department> deptList) {
37+
Employee employee1 = new Employee(1001, "empl1");
38+
Employee employee2 = new Employee(1002, "empl2");
39+
Employee employee3 = new Employee(1003, "empl3");
40+
41+
Collections.addAll(EmplList, employee1, employee2, employee3);
42+
43+
Department department1 = new Department(1002, "sales");
44+
Department department2 = new Department(1003, "marketing");
45+
Department department3 = new Department(1004, "sales");
46+
47+
Collections.addAll(deptList, department1, department2, department3);
48+
}
49+
}
50+
51+
class Employee {
52+
private Integer employeeId;
53+
private String employeeName;
54+
55+
Employee(Integer employeeId, String employeeName) {
56+
super();
57+
this.employeeId = employeeId;
58+
this.employeeName = employeeName;
59+
}
60+
61+
Integer getEmployeeId() {
62+
return employeeId;
63+
}
64+
65+
public String getEmployeeName() {
66+
return employeeName;
67+
}
68+
69+
}
70+
71+
class Department {
72+
private Integer employeeId;
73+
private String department;
74+
75+
Department(Integer employeeId, String department) {
76+
super();
77+
this.employeeId = employeeId;
78+
this.department = department;
79+
}
80+
81+
Integer getEmployeeId() {
82+
return employeeId;
83+
}
84+
85+
String getDepartment() {
86+
return department;
87+
}
88+
9389
}

‎core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,25 @@ public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputatio
184184

185185
assertEquals("Hello World", future.get());
186186
}
187+
188+
@Test
189+
public void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException {
190+
CompletableFuture<Integer> finalResult = compute().thenApply(s -> s + 1);
191+
assertTrue(finalResult.get() == 11);
192+
}
193+
194+
@Test
195+
public void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException {
196+
CompletableFuture<Integer> finalResult = compute().thenCompose(this::computeAnother);
197+
assertTrue(finalResult.get() == 20);
198+
}
199+
200+
public CompletableFuture<Integer> compute(){
201+
return CompletableFuture.supplyAsync(() -> 10);
202+
}
203+
204+
public CompletableFuture<Integer> computeAnother(Integer i){
205+
return CompletableFuture.supplyAsync(() -> 10 + i);
206+
}
187207

188208
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.baeldung.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.stream.IntStream;
8+
import java.util.stream.Stream;
9+
10+
public class ArrayReferenceGuide {
11+
12+
public static void main(String[] args) {
13+
declaration();
14+
15+
initialization();
16+
17+
access();
18+
19+
iterating();
20+
21+
varargs();
22+
23+
transformIntoList();
24+
25+
transformIntoStream();
26+
27+
sort();
28+
29+
search();
30+
31+
merge();
32+
}
33+
34+
private static void declaration() {
35+
int[] anArray;
36+
int anotherArray[];
37+
}
38+
39+
private static void initialization() {
40+
int[] anArray = new int[10];
41+
anArray[0] = 10;
42+
anArray[5] = 4;
43+
44+
int[] anotherArray = new int[] {1, 2, 3, 4, 5};
45+
}
46+
47+
private static void access() {
48+
int[] anArray = new int[10];
49+
anArray[0] = 10;
50+
anArray[5] = 4;
51+
52+
System.out.println(anArray[0]);
53+
}
54+
55+
private static void iterating() {
56+
int[] anArray = new int[] {1, 2, 3, 4, 5};
57+
for (int i = 0; i < anArray.length; i++) {
58+
System.out.println(anArray[i]);
59+
}
60+
61+
for (int element : anArray) {
62+
System.out.println(element);
63+
}
64+
}
65+
66+
private static void varargs() {
67+
String[] groceries = new String[] {"Milk", "Tomato", "Chips"};
68+
varargMethod(groceries);
69+
varargMethod("Milk", "Tomato", "Chips");
70+
}
71+
72+
private static void varargMethod(String... varargs) {
73+
for (String element : varargs) {
74+
System.out.println(element);
75+
}
76+
}
77+
78+
private static void transformIntoList() {
79+
Integer[] anArray = new Integer[] {1, 2, 3, 4, 5};
80+
81+
// Naïve implementation
82+
List<Integer> aList = new ArrayList<>(); // We create an empty list
83+
for (int element : anArray) {
84+
// We iterate over array's elements and add them to the list
85+
aList.add(element);
86+
}
87+
88+
// Pretty implementation
89+
aList = Arrays.asList(anArray);
90+
91+
// Drawbacks
92+
try {
93+
aList.remove(0);
94+
} catch (UnsupportedOperationException e) {
95+
System.out.println(e.getMessage());
96+
}
97+
98+
try {
99+
aList.add(6);
100+
} catch (UnsupportedOperationException e) {
101+
System.out.println(e.getMessage());
102+
}
103+
104+
int[] anotherArray = new int[] {1, 2, 3, 4, 5};
105+
// List<Integer> anotherList = Arrays.asList(anotherArray);
106+
}
107+
108+
private static void transformIntoStream() {
109+
int[] anArray = new int[] {1, 2, 3, 4, 5};
110+
IntStream aStream = Arrays.stream(anArray);
111+
112+
Integer[] anotherArray = new Integer[] {1, 2, 3, 4, 5};
113+
Stream<Integer> anotherStream = Arrays.stream(anotherArray, 2, 4);
114+
}
115+
116+
private static void sort() {
117+
int[] anArray = new int[] {5, 2, 1, 4, 8};
118+
Arrays.sort(anArray); // anArray is now {1, 2, 4, 5, 8}
119+
120+
Integer[] anotherArray = new Integer[] {5, 2, 1, 4, 8};
121+
Arrays.sort(anotherArray); // anArray is now {1, 2, 4, 5, 8}
122+
123+
String[] yetAnotherArray = new String[] {"A", "E", "Z", "B", "C"};
124+
Arrays.sort(yetAnotherArray, 1, 3, Comparator.comparing(String::toString).reversed()); // yetAnotherArray is now {"A", "Z", "E", "B", "C"}
125+
}
126+
127+
private static void search() {
128+
int[] anArray = new int[] {5, 2, 1, 4, 8};
129+
for (int i = 0; i < anArray.length; i++) {
130+
if (anArray[i] == 4) {
131+
System.out.println("Found at index " + i);
132+
break;
133+
}
134+
}
135+
136+
Arrays.sort(anArray);
137+
int index = Arrays.binarySearch(anArray, 4);
138+
System.out.println("Found at index " + index);
139+
}
140+
141+
private static void merge() {
142+
int[] anArray = new int[] {5, 2, 1, 4, 8};
143+
int[] anotherArray = new int[] {10, 4, 9, 11, 2};
144+
145+
int[] resultArray = new int[anArray.length + anotherArray.length];
146+
for (int i = 0; i < resultArray.length; i++) {
147+
resultArray[i] = (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length]);
148+
}
149+
for (int element : resultArray) {
150+
System.out.println(element);
151+
}
152+
153+
Arrays.setAll(resultArray, i -> (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length]));
154+
for (int element : resultArray) {
155+
System.out.println(element);
156+
}
157+
}
158+
159+
}

0 commit comments

Comments
 (0)