Skip to content

Commit 172ecaa

Browse files
author
Igor Kugaudo
committed
Merge remote-tracking branch 'eugenp/master' into datepattern
2 parents 18ad65d + 52edd46 commit 172ecaa

274 files changed

Lines changed: 34942 additions & 1570 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.

‎cas/cas-secured-app/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Relevant articles:
2+
- [CAS SSO With Spring Security](http://www.baeldung.com/spring-security-cas-sso)

‎core-java-8/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
- [“Stream has already been operated upon or closed” Exception in Java](http://www.baeldung.com/java-stream-operated-upon-or-closed-exception)
3333
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
3434
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
35+
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)

‎core-java-8/src/main/java/com/baeldung/datetime/UseDuration.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.time.Duration;
44
import java.time.LocalTime;
5-
import java.time.Period;
65

76
public class UseDuration {
87

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.stream;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.IntStream;
5+
6+
class PrimitiveStreams {
7+
8+
int min(int[] integers) {
9+
return Arrays.stream(integers).min().getAsInt();
10+
}
11+
12+
int max(int... integers) {
13+
return IntStream.of(integers).max().getAsInt();
14+
}
15+
16+
int sum(int... integers) {
17+
return IntStream.of(integers).sum();
18+
}
19+
20+
double avg(int... integers) {
21+
return IntStream.of(integers).average().getAsDouble();
22+
}
23+
}

‎core-java-8/src/main/java/com/baeldung/streamApi/Product.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.baeldung.streamApi;
22

33
import java.util.List;
4-
import java.util.Optional;
5-
import java.util.stream.IntStream;
64
import java.util.stream.Stream;
75

86
/**

‎core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,14 @@ public int getCount() {
3838

3939
public static void counterWithMutableInteger(Map<String, MutableInteger> counterMap) {
4040
for (String country : COUNTRY_NAMES) {
41-
MutableInteger oldValue = counterMap.get(country);
42-
if (oldValue != null) {
43-
oldValue.increment();
44-
} else {
45-
counterMap.put(country, new MutableInteger(1));
46-
}
41+
counterMap.compute(country, (k, v) -> v == null ? new MutableInteger(0) : v)
42+
.increment();
4743
}
4844
}
4945

5046
public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
5147
for (String country : COUNTRY_NAMES) {
52-
int[] oldCounter = counterMap.get(country);
53-
if (oldCounter != null) {
54-
oldCounter[0] += 1;
55-
} else {
56-
counterMap.put(country, new int[] { 1 });
57-
}
48+
counterMap.compute(country, (k, v) -> v == null ? new int[] { 0 } : v)[0]++;
5849
}
5950
}
6051

‎core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTimeUnitTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.time.LocalTime;
55
import java.time.Month;
66

7-
import org.junit.Assert;
87
import org.junit.Test;
98

109
import static org.junit.Assert.assertEquals;

‎core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateUnitTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.time.LocalDate;
55
import java.time.LocalDateTime;
66

7-
import org.junit.Assert;
87
import org.junit.Test;
98

109
import static org.junit.Assert.assertEquals;

‎core-java-8/src/test/java/com/baeldung/java8/comparator/Java8ComparatorUnitTest.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.junit.Before;
77
import org.junit.Test;
88

9-
import lombok.Data;
109
import static org.junit.Assert.assertTrue;
1110

1211
public class Java8ComparatorUnitTest {
@@ -134,8 +133,7 @@ public void whenNullsLast_thenSortedByNameWithNullsLast() {
134133

135134
@Test
136135
public void whenThenComparing_thenSortedByAgeName() {
137-
Comparator<Employee> employee_Age_Name_Comparator = Comparator.comparing(Employee::getAge)
138-
.thenComparing(Employee::getName);
136+
Comparator<Employee> employee_Age_Name_Comparator = Comparator.comparing(Employee::getAge).thenComparing(Employee::getName);
139137

140138
Arrays.sort(someMoreEmployees, employee_Age_Name_Comparator);
141139
// System.out.println(Arrays.toString(someMoreEmployees));
@@ -144,8 +142,7 @@ public void whenThenComparing_thenSortedByAgeName() {
144142

145143
@Test
146144
public void whenThenComparing_thenSortedByNameAge() {
147-
Comparator<Employee> employee_Name_Age_Comparator = Comparator.comparing(Employee::getName)
148-
.thenComparingInt(Employee::getAge);
145+
Comparator<Employee> employee_Name_Age_Comparator = Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge);
149146

150147
Arrays.sort(someMoreEmployees, employee_Name_Age_Comparator);
151148
// System.out.println(Arrays.toString(someMoreEmployees));
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.stream;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.IntStream;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertTrue;
12+
13+
public class PrimitiveStreamsUnitTest {
14+
15+
private PrimitiveStreams streams = new PrimitiveStreams();
16+
17+
@Test
18+
public void givenAnArrayOfIntegersWhenMinIsCalledThenCorrectMinIsReturned() {
19+
int[] integers = new int[] {20, 98, 12, 7, 35};
20+
int min = streams.min(integers); // returns 7
21+
22+
assertEquals(7, min);
23+
}
24+
25+
@Test
26+
public void givenAnArrayOfIntegersWhenMaxIsCalledThenCorrectMaxIsReturned() {
27+
int max = streams.max(20, 98, 12, 7, 35);
28+
29+
assertEquals(98, max);
30+
}
31+
32+
@Test
33+
public void givenAnArrayOfIntegersWhenSumIsCalledThenCorrectSumIsReturned() {
34+
int sum = streams.sum(20, 98, 12, 7, 35);
35+
36+
assertEquals(172, sum);
37+
}
38+
39+
@Test
40+
public void givenAnArrayOfIntegersWhenAvgIsCalledThenCorrectAvgIsReturned() {
41+
double avg = streams.avg(20, 98, 12, 7, 35);
42+
43+
assertTrue(34.4 == avg);
44+
}
45+
46+
@Test
47+
public void givenARangeOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() {
48+
int sum = IntStream.range(1, 10).sum();
49+
50+
assertEquals(45, sum);
51+
}
52+
53+
@Test
54+
public void givenARangeClosedOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() {
55+
int sum = IntStream.rangeClosed(1, 10).sum();
56+
57+
assertEquals(55, sum);
58+
}
59+
60+
@Test
61+
public void givenARangeWhenForEachIsCalledThenTheIndicesWillBePrinted() {
62+
IntStream.rangeClosed(1, 5).parallel().forEach(System.out::println);
63+
}
64+
65+
@Test
66+
public void givenAnArrayWhenSumIsCalledThenTheCorrectSumIsReturned() {
67+
68+
int sum = Arrays.asList(33,45).stream().mapToInt(a -> a).sum();
69+
70+
assertEquals(78, sum);
71+
}
72+
73+
@Test
74+
public void givenAnIntStreamThenGetTheEvenIntegers() {
75+
List<Integer> evenInts = IntStream.rangeClosed(1, 10)
76+
.filter(i -> i % 2 == 0)
77+
.boxed()
78+
.collect(Collectors.toList());
79+
80+
List<Integer> expected = IntStream.of(2, 4, 6, 8, 10).boxed().collect(Collectors.toList());
81+
82+
assertEquals(expected, evenInts);
83+
}
84+
85+
class Person {
86+
private int age;
87+
88+
Person(int age) {
89+
this.age = age;
90+
}
91+
92+
int getAge() {
93+
return age;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)