Skip to content

Commit 2dbb9e0

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 8ef39f8 + 7d7cae7 commit 2dbb9e0

File tree

149 files changed

+2200
-344
lines changed

Some content is hidden

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

149 files changed

+2200
-344
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ before_install:
44
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
55

66
install: skip
7-
script: travis_wait 60 mvn -q install -Pdefault
7+
script: travis_wait 60 mvn -q install -Pdefault-first,default-second
88

99
sudo: required
1010

core-java/src/main/java/com/baeldung/heapsort/Heap.java renamed to algorithms/src/main/java/com/baeldung/algorithms/heapsort/Heap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.heapsort;
1+
package com.baeldung.algorithms.heapsort;
22

33
import java.util.ArrayList;
44
import java.util.Arrays;

core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java renamed to algorithms/src/test/java/com/baeldung/algorithms/heapsort/HeapUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.heapsort;
1+
package com.baeldung.algorithms.heapsort;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

core-java-8/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
3333
- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance)
3434
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)
35+
- [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic)

core-java-8/findItemsBasedOnValues/.gitignore

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

core-java-collections/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@
5555
- [Sort a HashMap in Java](https://www.baeldung.com/java-hashmap-sort)
5656
- [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max)
5757
- [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream)
58+
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
public class CollectionRemoveIf {
7+
8+
public static void main(String args[]) {
9+
Collection<String> names = new ArrayList<>();
10+
names.add("John");
11+
names.add("Ana");
12+
names.add("Mary");
13+
names.add("Anthony");
14+
names.add("Mark");
15+
16+
names.removeIf(e -> e.startsWith("A"));
17+
System.out.println(String.join(",", names));
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
7+
public class Iterators {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Iterator<String> i = names.iterator();
18+
19+
while (i.hasNext()) {
20+
String e = i.next();
21+
if (e.startsWith("A")) {
22+
i.remove();
23+
}
24+
}
25+
26+
System.out.println(String.join(",", names));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamFilterAndCollector {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Collection<String> filteredCollection = names
18+
.stream()
19+
.filter(e -> !e.startsWith("A"))
20+
.collect(Collectors.toList());
21+
System.out.println(String.join(",", filteredCollection));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamPartitioningBy {
10+
11+
public static void main(String args[]) {
12+
Collection<String> names = new ArrayList<>();
13+
names.add("John");
14+
names.add("Ana");
15+
names.add("Mary");
16+
names.add("Anthony");
17+
names.add("Mark");
18+
19+
Map<Boolean, List<String>> classifiedElements = names
20+
.stream()
21+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
22+
23+
String matching = String.join(",", classifiedElements.get(Boolean.TRUE));
24+
String nonMatching = String.join(",", classifiedElements.get(Boolean.FALSE));
25+
System.out.println("Matching elements: " + matching);
26+
System.out.println("Non matching elements: " + nonMatching);
27+
}
28+
}
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
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.removal;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
import static org.hamcrest.Matchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class RemovalUnitTest {
13+
14+
Collection<String> names;
15+
Collection<String> expected;
16+
Collection<String> removed;
17+
18+
@Before
19+
public void setupTestData() {
20+
names = new ArrayList<>();
21+
expected = new ArrayList<>();
22+
removed = new ArrayList<>();
23+
24+
names.add("John");
25+
names.add("Ana");
26+
names.add("Mary");
27+
names.add("Anthony");
28+
names.add("Mark");
29+
30+
expected.add("John");
31+
expected.add("Mary");
32+
expected.add("Mark");
33+
34+
removed.add("Ana");
35+
removed.add("Anthony");
36+
}
37+
38+
@Test
39+
public void givenCollectionOfNames_whenUsingIteratorToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
40+
Iterator<String> i = names.iterator();
41+
42+
while (i.hasNext()) {
43+
String e = i.next();
44+
if (e.startsWith("A")) {
45+
i.remove();
46+
}
47+
}
48+
49+
assertThat(names, is(expected));
50+
}
51+
52+
@Test
53+
public void givenCollectionOfNames_whenUsingRemoveIfToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
54+
names.removeIf(e -> e.startsWith("A"));
55+
assertThat(names, is(expected));
56+
}
57+
58+
@Test
59+
public void givenCollectionOfNames_whenUsingStreamToFilterAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
60+
Collection<String> filteredCollection = names
61+
.stream()
62+
.filter(e -> !e.startsWith("A"))
63+
.collect(Collectors.toList());
64+
assertThat(filteredCollection, is(expected));
65+
}
66+
67+
@Test
68+
public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() {
69+
Map<Boolean, List<String>> classifiedElements = names
70+
.stream()
71+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
72+
73+
assertThat(classifiedElements.get(Boolean.TRUE), is(expected));
74+
assertThat(classifiedElements.get(Boolean.FALSE), is(removed));
75+
}
76+
77+
}

0 commit comments

Comments
 (0)