Skip to content

Commit 1fa5e1a

Browse files
committed
Merge remote-tracking branch 'upstream/master' into tutorials/binaryTreeJava
2 parents 15bc324 + ae109df commit 1fa5e1a

132 files changed

Lines changed: 2546 additions & 964 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)
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/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

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

‎core-java-9/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
- [How to Get All Dates Between Two Dates?](http://www.baeldung.com/java-between-dates)
1919
- [Java 9 java.util.Objects Additions](http://www.baeldung.com/java-9-objects-new)
2020
- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string)
21+
- [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime)

‎core-java-concurrency/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@
3131
- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent)
3232
- [Semaphores in Java](http://www.baeldung.com/java-semaphore)
3333
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
34+
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
35+
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)

core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java renamed to core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishManualTest.java

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
import static junit.framework.TestCase.assertTrue;
1212

13-
public class WaitingForThreadsToFinishTest {
13+
public class WaitingForThreadsToFinishManualTest {
1414

15-
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishTest.class);
15+
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class);
1616
private final static ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
1717

1818
public void awaitTerminationAfterShutdown(ExecutorService threadPool) {
@@ -142,66 +142,4 @@ public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShould
142142
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
143143
}
144144
}
145-
146-
@Test
147-
public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShouldWaitForAllToFinish() {
148-
149-
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
150-
151-
try {
152-
Thread.sleep(1000);
153-
} catch (InterruptedException e) {
154-
e.printStackTrace();
155-
}
156-
157-
return "Hello";
158-
});
159-
160-
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
161-
162-
try {
163-
Thread.sleep(5000);
164-
} catch (InterruptedException e) {
165-
e.printStackTrace();
166-
}
167-
168-
return "Beautiful";
169-
});
170-
171-
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
172-
173-
try {
174-
Thread.sleep(3000);
175-
} catch (InterruptedException e) {
176-
e.printStackTrace();
177-
}
178-
179-
return "World";
180-
});
181-
182-
long startProcessingTime = System.currentTimeMillis();
183-
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
184-
combinedFuture.join();
185-
186-
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
187-
assertTrue(totalProcessingTime >= 5000 && totalProcessingTime < 6000);
188-
189-
LOG.debug("Responses from all threads are available after " + totalProcessingTime + " milliseconds");
190-
191-
try {
192-
String thread1Response = future1.get();
193-
assertTrue(thread1Response.equals("Hello"));
194-
195-
String thread2Response = future2.get();
196-
assertTrue(thread2Response.equals("Beautiful"));
197-
198-
String thread3Response = future3.get();
199-
assertTrue(thread3Response.equals("World"));
200-
201-
} catch (InterruptedException | ExecutionException e) {
202-
e.printStackTrace();
203-
}
204-
205-
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
206-
}
207145
}

‎core-java/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@
121121
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
122122
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
123123
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
124-
124+
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
125+
- [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break)
126+
- [Java – Append Data to a File](http://www.baeldung.com/java-append-to-file)
127+
- [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque)
128+
- [Guide to java.util.Formatter](http://www.baeldung.com/java-string-formatter)

‎core-java/src/main/java/com/baeldung/array/SearchArrayTest.java‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import java.util.*;
66
import java.util.concurrent.TimeUnit;
77

8+
@BenchmarkMode(Mode.AverageTime)
9+
@Warmup(iterations = 5)
10+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
811
public class SearchArrayTest {
912

1013
@Benchmark
11-
@BenchmarkMode(Mode.AverageTime)
12-
@Warmup(iterations = 5)
13-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
1414
public void searchArrayLoop() {
1515

1616
int count = 1000;
@@ -21,9 +21,6 @@ public void searchArrayLoop() {
2121
}
2222

2323
@Benchmark
24-
@BenchmarkMode(Mode.AverageTime)
25-
@Warmup(iterations = 5)
26-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
2724
public void searchArrayAllocNewList() {
2825

2926
int count = 1000;
@@ -35,9 +32,6 @@ public void searchArrayAllocNewList() {
3532
}
3633

3734
@Benchmark
38-
@BenchmarkMode(Mode.AverageTime)
39-
@Warmup(iterations = 5)
40-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
4135
public void searchArrayAllocNewSet() {
4236

4337
int count = 1000;
@@ -49,9 +43,6 @@ public void searchArrayAllocNewSet() {
4943

5044

5145
@Benchmark
52-
@BenchmarkMode(Mode.AverageTime)
53-
@Warmup(iterations = 5)
54-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
5546
public void searchArrayReuseList() {
5647

5748
int count = 1000;
@@ -66,9 +57,6 @@ public void searchArrayReuseList() {
6657

6758

6859
@Benchmark
69-
@BenchmarkMode(Mode.AverageTime)
70-
@Warmup(iterations = 5)
71-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
7260
public void searchArrayReuseSet() {
7361

7462
int count = 1000;
@@ -81,9 +69,6 @@ public void searchArrayReuseSet() {
8169

8270

8371
@Benchmark
84-
@BenchmarkMode(Mode.AverageTime)
85-
@Warmup(iterations = 5)
86-
@OutputTimeUnit(TimeUnit.MICROSECONDS)
8772
public void searchArrayBinarySearch() {
8873

8974
int count = 1000;

0 commit comments

Comments
 (0)