Skip to content

Commit afe4a98

Browse files
committed
[BAEL-10866] - Resolved conflicts
2 parents 78735f5 + 5e02bec commit afe4a98

File tree

239 files changed

+3416
-2983
lines changed

Some content is hidden

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

239 files changed

+3416
-2983
lines changed

core-java-11/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Relevant articles
2+
3+
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
4+
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
5+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.Test;
8+
9+
public class NewStringAPIUnitTest {
10+
11+
@Test
12+
public void whenRepeatStringTwice_thenGetStringTwice() {
13+
String output = "La ".repeat(2) + "Land";
14+
15+
is(output).equals("La La Land");
16+
}
17+
18+
@Test
19+
public void whenStripString_thenReturnStringWithoutWhitespaces() {
20+
is("\n\t hello \u2005".strip()).equals("hello");
21+
}
22+
23+
@Test
24+
public void whenTrimAdvanceString_thenReturnStringWithWhitespaces() {
25+
is("\n\t hello \u2005".trim()).equals("hello \u2005");
26+
}
27+
28+
@Test
29+
public void whenBlankString_thenReturnTrue() {
30+
assertTrue("\n\t\u2005 ".isBlank());
31+
}
32+
33+
@Test
34+
public void whenMultilineString_thenReturnNonEmptyLineCount() {
35+
String multilineStr = "This is\n \n a multiline\n string.";
36+
37+
long lineCount = multilineStr.lines()
38+
.filter(String::isBlank)
39+
.count();
40+
41+
is(lineCount).equals(3L);
42+
}
43+
}

core-java-8/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
- [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing)
1616
- [Guide To Java 8 Optional](http://www.baeldung.com/java-optional)
1717
- [Guide to the Java 8 forEach](http://www.baeldung.com/foreach-java)
18-
- [Java Base64 Encoding and Decoding](http://www.baeldung.com/java-base64-encode-and-decode)
1918
- [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap)
2019
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
2120
- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.baeldung.java8;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.contains;
5+
import static org.junit.Assert.assertEquals;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.function.Predicate;
11+
import java.util.stream.Collectors;
12+
13+
import org.junit.Test;
14+
15+
public class Java8PredicateChainUnitTest {
16+
17+
private List<String> names = Arrays.asList("Adam", "Alexander", "John", "Tom");
18+
19+
@Test
20+
public void whenFilterList_thenSuccess() {
21+
List<String> result = names.stream()
22+
.filter(name -> name.startsWith("A"))
23+
.collect(Collectors.toList());
24+
25+
assertEquals(2, result.size());
26+
assertThat(result, contains("Adam", "Alexander"));
27+
}
28+
29+
@Test
30+
public void whenFilterListWithMultipleFilters_thenSuccess() {
31+
List<String> result = names.stream()
32+
.filter(name -> name.startsWith("A"))
33+
.filter(name -> name.length() < 5)
34+
.collect(Collectors.toList());
35+
36+
assertEquals(1, result.size());
37+
assertThat(result, contains("Adam"));
38+
}
39+
40+
@Test
41+
public void whenFilterListWithComplexPredicate_thenSuccess() {
42+
List<String> result = names.stream()
43+
.filter(name -> name.startsWith("A") && name.length() < 5)
44+
.collect(Collectors.toList());
45+
46+
assertEquals(1, result.size());
47+
assertThat(result, contains("Adam"));
48+
}
49+
50+
@Test
51+
public void whenFilterListWithCombinedPredicatesInline_thenSuccess() {
52+
List<String> result = names.stream()
53+
.filter(((Predicate<String>) name -> name.startsWith("A")).and(name -> name.length() < 5))
54+
.collect(Collectors.toList());
55+
56+
assertEquals(1, result.size());
57+
assertThat(result, contains("Adam"));
58+
}
59+
60+
@Test
61+
public void whenFilterListWithCombinedPredicatesUsingAnd_thenSuccess() {
62+
Predicate<String> predicate1 = str -> str.startsWith("A");
63+
Predicate<String> predicate2 = str -> str.length() < 5;
64+
65+
List<String> result = names.stream()
66+
.filter(predicate1.and(predicate2))
67+
.collect(Collectors.toList());
68+
69+
assertEquals(1, result.size());
70+
assertThat(result, contains("Adam"));
71+
}
72+
73+
@Test
74+
public void whenFilterListWithCombinedPredicatesUsingOr_thenSuccess() {
75+
Predicate<String> predicate1 = str -> str.startsWith("J");
76+
Predicate<String> predicate2 = str -> str.length() < 4;
77+
78+
List<String> result = names.stream()
79+
.filter(predicate1.or(predicate2))
80+
.collect(Collectors.toList());
81+
82+
assertEquals(2, result.size());
83+
assertThat(result, contains("John", "Tom"));
84+
}
85+
86+
@Test
87+
public void whenFilterListWithCombinedPredicatesUsingOrAndNegate_thenSuccess() {
88+
Predicate<String> predicate1 = str -> str.startsWith("J");
89+
Predicate<String> predicate2 = str -> str.length() < 4;
90+
91+
List<String> result = names.stream()
92+
.filter(predicate1.or(predicate2.negate()))
93+
.collect(Collectors.toList());
94+
95+
assertEquals(3, result.size());
96+
assertThat(result, contains("Adam", "Alexander", "John"));
97+
}
98+
99+
@Test
100+
public void whenFilterListWithCollectionOfPredicatesUsingAnd_thenSuccess() {
101+
List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>();
102+
allPredicates.add(str -> str.startsWith("A"));
103+
allPredicates.add(str -> str.contains("d"));
104+
allPredicates.add(str -> str.length() > 4);
105+
106+
List<String> result = names.stream()
107+
.filter(allPredicates.stream()
108+
.reduce(x -> true, Predicate::and))
109+
.collect(Collectors.toList());
110+
111+
assertEquals(1, result.size());
112+
assertThat(result, contains("Alexander"));
113+
}
114+
115+
@Test
116+
public void whenFilterListWithCollectionOfPredicatesUsingOr_thenSuccess() {
117+
List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>();
118+
allPredicates.add(str -> str.startsWith("A"));
119+
allPredicates.add(str -> str.contains("d"));
120+
allPredicates.add(str -> str.length() > 4);
121+
122+
List<String> result = names.stream()
123+
.filter(allPredicates.stream()
124+
.reduce(x -> false, Predicate::or))
125+
.collect(Collectors.toList());
126+
127+
assertEquals(2, result.size());
128+
assertThat(result, contains("Adam", "Alexander"));
129+
}
130+
131+
}

core-java-9/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
- [Java 9 Convenience Factory Methods for Collections](http://www.baeldung.com/java-9-collections-factory-methods)
1010
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
1111
- [Java 9 CompletableFuture API Improvements](http://www.baeldung.com/java-9-completablefuture)
12-
- [Spring Security – Redirect to the Previous URL After Login](http://www.baeldung.com/spring-security-redirect-login)
1312
- [Java 9 Process API Improvements](http://www.baeldung.com/java-9-process-api)
1413
- [Introduction to Java 9 StackWalking API](http://www.baeldung.com/java-9-stackwalking-api)
1514
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)

core-java-arrays/src/main/java/com/baeldung/arraycopy/model/Employee.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ public class Employee implements Serializable {
77
private int id;
88
private String name;
99

10+
public Employee() {
11+
}
12+
13+
public Employee(int id, String name) {
14+
this.id = id;
15+
this.name = name;
16+
}
17+
1018
public int getId() {
1119
return id;
1220
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.sort;
2+
3+
import com.baeldung.arraycopy.model.Employee;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.Comparator;
9+
import java.util.stream.IntStream;
10+
11+
import static org.junit.Assert.assertArrayEquals;
12+
13+
public class ArraySortUnitTest {
14+
private Employee[] employees;
15+
private int[] numbers;
16+
private String[] strings;
17+
18+
private Employee john = new Employee(6, "John");
19+
private Employee mary = new Employee(3, "Mary");
20+
private Employee david = new Employee(4, "David");
21+
22+
@Before
23+
public void setup() {
24+
createEmployeesArray();
25+
createNumbersArray();
26+
createStringArray();
27+
}
28+
29+
private void createEmployeesArray() {
30+
employees = new Employee[]{john, mary, david};
31+
}
32+
33+
private void createNumbersArray() {
34+
numbers = new int[]{-8, 7, 5, 9, 10, -2, 3};
35+
}
36+
37+
private void createStringArray() {
38+
strings = new String[]{"learning", "java", "with", "baeldung"};
39+
}
40+
41+
@Test
42+
public void givenIntArray_whenSortingAscending_thenCorrectlySorted() {
43+
Arrays.sort(numbers);
44+
45+
assertArrayEquals(new int[]{-8, -2, 3, 5, 7, 9, 10}, numbers);
46+
}
47+
48+
@Test
49+
public void givenIntArray_whenSortingDescending_thenCorrectlySorted() {
50+
numbers = IntStream.of(numbers).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray();
51+
52+
assertArrayEquals(new int[]{10, 9, 7, 5, 3, -2, -8}, numbers);
53+
}
54+
55+
@Test
56+
public void givenStringArray_whenSortingAscending_thenCorrectlySorted() {
57+
Arrays.sort(strings);
58+
59+
assertArrayEquals(new String[]{"baeldung", "java", "learning", "with"}, strings);
60+
}
61+
62+
@Test
63+
public void givenStringArray_whenSortingDescending_thenCorrectlySorted() {
64+
Arrays.sort(strings, Comparator.reverseOrder());
65+
66+
assertArrayEquals(new String[]{"with", "learning", "java", "baeldung"}, strings);
67+
}
68+
69+
@Test
70+
public void givenObjectArray_whenSortingAscending_thenCorrectlySorted() {
71+
Arrays.sort(employees, Comparator.comparing(Employee::getName));
72+
73+
assertArrayEquals(new Employee[]{david, john, mary}, employees);
74+
}
75+
76+
@Test
77+
public void givenObjectArray_whenSortingDescending_thenCorrectlySorted() {
78+
Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed());
79+
80+
assertArrayEquals(new Employee[]{mary, john, david}, employees);
81+
}
82+
83+
@Test
84+
public void givenObjectArray_whenSortingMultipleAttributesAscending_thenCorrectlySorted() {
85+
Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId));
86+
87+
assertArrayEquals(new Employee[]{david, john, mary}, employees);
88+
}
89+
90+
}

core-java-collections/src/test/java/org/baeldung/java/lists/ListJUnitTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
import java.util.Arrays;
77
import java.util.List;
8+
import java.util.Set;
9+
import java.util.HashSet;
10+
11+
import java.util.stream.Collectors;
812

913
public class ListJUnitTest {
1014

@@ -18,4 +22,26 @@ public void whenTestingForEquality_ShouldBeEqual() throws Exception {
1822
Assert.assertNotSame(list1, list2);
1923
Assert.assertNotEquals(list1, list3);
2024
}
25+
26+
@Test
27+
public void whenIntersection_ShouldReturnCommonElements() throws Exception {
28+
List<String> list = Arrays.asList("red", "blue", "blue", "green", "red");
29+
List<String> otherList = Arrays.asList("red", "green", "green", "yellow");
30+
31+
Set<String> commonElements = new HashSet(Arrays.asList("red", "green"));
32+
33+
Set<String> result = list.stream()
34+
.distinct()
35+
.filter(otherList::contains)
36+
.collect(Collectors.toSet());
37+
38+
Assert.assertEquals(commonElements, result);
39+
40+
Set<String> inverseResult = otherList.stream()
41+
.distinct()
42+
.filter(list::contains)
43+
.collect(Collectors.toSet());
44+
45+
Assert.assertEquals(commonElements, inverseResult);
46+
}
2147
}

0 commit comments

Comments
 (0)