Skip to content

Commit 90c9744

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents bee7faf + c9aaa0d commit 90c9744

52 files changed

Lines changed: 1723 additions & 173 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.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.algorithms.prime;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.IntStream;
8+
9+
public class PrimeGenerator {
10+
public static List<Integer> sieveOfEratosthenes(int n) {
11+
final boolean prime[] = new boolean[n + 1];
12+
Arrays.fill(prime, true);
13+
14+
for (int p = 2; p * p <= n; p++) {
15+
if (prime[p]) {
16+
for (int i = p * 2; i <= n; i += p)
17+
prime[i] = false;
18+
}
19+
}
20+
21+
final List<Integer> primes = new LinkedList<>();
22+
for (int i = 2; i <= n; i++) {
23+
if (prime[i])
24+
primes.add(i);
25+
}
26+
return primes;
27+
}
28+
29+
public static List<Integer> primeNumbersBruteForce(int max) {
30+
final List<Integer> primeNumbers = new LinkedList<Integer>();
31+
for (int i = 2; i <= max; i++) {
32+
if (isPrimeBruteForce(i)) {
33+
primeNumbers.add(i);
34+
}
35+
}
36+
return primeNumbers;
37+
}
38+
39+
private static boolean isPrimeBruteForce(int x) {
40+
for (int i = 2; i < x; i++) {
41+
if (x % i == 0) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
}
47+
48+
public static List<Integer> primeNumbersTill(int max) {
49+
return IntStream.rangeClosed(2, max)
50+
.filter(x -> isPrime(x))
51+
.boxed()
52+
.collect(Collectors.toList());
53+
}
54+
55+
private static boolean isPrime(int x) {
56+
return IntStream.rangeClosed(2, (int) (Math.sqrt(x)))
57+
.allMatch(n -> x % n != 0);
58+
}
59+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.algorithms.prime;
2+
3+
import static com.baeldung.algorithms.prime.PrimeGenerator.*;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
public class PrimeGeneratorTest {
11+
@Test
12+
public void whenBruteForced_returnsSuccessfully() {
13+
final List<Integer> primeNumbers = primeNumbersBruteForce(20);
14+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
15+
}
16+
17+
@Test
18+
public void whenOptimized_returnsSuccessfully() {
19+
final List<Integer> primeNumbers = primeNumbersTill(20);
20+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
21+
}
22+
23+
@Test
24+
public void whenSieveOfEratosthenes_returnsSuccessfully() {
25+
final List<Integer> primeNumbers = sieveOfEratosthenes(20);
26+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.prime;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.IntStream;
8+
9+
public class PrimeGenerator {
10+
public static List<Integer> sieveOfEratosthenes(int n) {
11+
final boolean prime[] = new boolean[n + 1];
12+
Arrays.fill(prime, true);
13+
14+
for (int p = 2; p * p <= n; p++) {
15+
if (prime[p]) {
16+
for (int i = p * 2; i <= n; i += p)
17+
prime[i] = false;
18+
}
19+
}
20+
21+
final List<Integer> primes = new LinkedList<>();
22+
for (int i = 2; i <= n; i++) {
23+
if (prime[i])
24+
primes.add(i);
25+
}
26+
return primes;
27+
}
28+
29+
public static List<Integer> primeNumbersBruteForce(int max) {
30+
final List<Integer> primeNumbers = new LinkedList<Integer>();
31+
for (int i = 2; i <= max; i++) {
32+
if (isPrimeBruteForce(i)) {
33+
primeNumbers.add(i);
34+
}
35+
}
36+
return primeNumbers;
37+
}
38+
39+
private static boolean isPrimeBruteForce(int x) {
40+
for (int i = 2; i < x; i++) {
41+
if (x % i == 0) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
}
47+
48+
public static List<Integer> primeNumbersTill(int max) {
49+
return IntStream.rangeClosed(2, max)
50+
.filter(x -> isPrime(x))
51+
.boxed()
52+
.collect(Collectors.toList());
53+
}
54+
55+
private static boolean isPrime(int x) {
56+
return IntStream.rangeClosed(2, (int) (Math.sqrt(x)))
57+
.allMatch(n -> x % n != 0);
58+
}
59+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.prime;
2+
3+
import static com.baeldung.prime.PrimeGenerator.*;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
public class PrimeGeneratorTest {
11+
@Test
12+
public void whenBruteForced_returnsSuccessfully() {
13+
final List<Integer> primeNumbers = primeNumbersBruteForce(20);
14+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
15+
}
16+
17+
@Test
18+
public void whenOptimized_returnsSuccessfully() {
19+
final List<Integer> primeNumbers = primeNumbersTill(20);
20+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
21+
}
22+
23+
@Test
24+
public void whenSieveOfEratosthenes_returnsSuccessfully() {
25+
final List<Integer> primeNumbers = sieveOfEratosthenes(20);
26+
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
27+
}
28+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.baeldung.stack;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.junit.Assert.*;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.ListIterator;
9+
import java.util.Stack;
10+
11+
import org.junit.Test;
12+
public class StackUnitTest {
13+
14+
@Test
15+
public void whenStackIsCreated_thenItHasSize0() {
16+
Stack intStack = new Stack();
17+
assertEquals(0, intStack.size());
18+
}
19+
20+
@Test
21+
public void givenEmptyStack_whenElementIsPushed_thenStackSizeisIncreased() {
22+
Stack intStack = new Stack();
23+
intStack.push(1);
24+
assertEquals(1, intStack.size());
25+
}
26+
27+
@Test
28+
public void givenEmptyStack_whenMultipleElementsArePushed_thenStackSizeisIncreased() {
29+
Stack intStack = new Stack();
30+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
31+
boolean result = intStack.addAll(intList);
32+
assertTrue(result);
33+
assertEquals(7, intList.size());
34+
}
35+
36+
@Test
37+
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
38+
Stack<Integer> intStack = new Stack();
39+
intStack.push(5);
40+
intStack.pop();
41+
assertTrue(intStack.isEmpty());
42+
}
43+
44+
@Test
45+
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
46+
Stack<Integer> intStack = new Stack();
47+
intStack.push(5);
48+
intStack.peek();
49+
assertEquals(1, intStack.search(5));
50+
assertEquals(1, intStack.size());
51+
}
52+
53+
@Test
54+
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
55+
Stack<Integer> intStack = new Stack();
56+
intStack.push(5);
57+
assertEquals(1, intStack.search(5));
58+
}
59+
60+
@Test
61+
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
62+
Stack<Integer> intStack = new Stack();
63+
intStack.push(5);
64+
int indexOf = intStack.indexOf(5);
65+
assertEquals(0, indexOf);
66+
}
67+
68+
@Test
69+
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
70+
Stack<Integer> intStack = new Stack();
71+
intStack.push(5);
72+
intStack.push(5);
73+
intStack.push(5);
74+
int lastIndexOf = intStack.lastIndexOf(5);
75+
assertEquals(2, lastIndexOf);
76+
}
77+
78+
@Test
79+
public void givenElementOnStack_whenRemoveElementIsInvoked_thenElementIsRemoved() {
80+
Stack<Integer> intStack = new Stack();
81+
intStack.push(5);
82+
intStack.push(5);
83+
intStack.removeElement(5);
84+
assertEquals(1, intStack.size());
85+
}
86+
87+
@Test
88+
public void givenElementOnStack_whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
89+
Stack<Integer> intStack = new Stack();
90+
intStack.push(5);
91+
intStack.push(7);
92+
intStack.removeElementAt(1);
93+
assertEquals(-1, intStack.search(7));
94+
}
95+
96+
@Test
97+
public void givenElementsOnStack_whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
98+
Stack<Integer> intStack = new Stack();
99+
intStack.push(5);
100+
intStack.push(7);
101+
intStack.removeAllElements();
102+
assertTrue(intStack.isEmpty());
103+
}
104+
105+
@Test
106+
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
107+
Stack<Integer> intStack = new Stack();
108+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
109+
intStack.addAll(intList);
110+
intStack.add(500);
111+
intStack.removeAll(intList);
112+
assertEquals(1, intStack.size());
113+
}
114+
115+
@Test
116+
public void givenElementsOnStack_whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
117+
Stack<Integer> intStack = new Stack();
118+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
119+
intStack.addAll(intList);
120+
intStack.removeIf(element -> element < 6);
121+
assertEquals(2, intStack.size());
122+
}
123+
124+
@Test
125+
public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() {
126+
Stack<Integer> intStack = new Stack<>();
127+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
128+
intStack.addAll(intList);
129+
ListIterator<Integer> it = intStack.listIterator();
130+
Stack<Integer> result = new Stack();
131+
while(it.hasNext()) {
132+
result.push(it.next());
133+
}
134+
135+
assertThat(result, equalTo(intStack));
136+
}
137+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.stringpool;
2+
3+
import org.junit.Test;
4+
import static org.assertj.core.api.Assertions.*;
5+
6+
public class StringPoolUnitTest {
7+
8+
@Test
9+
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
10+
String constantString1 = "Baeldung";
11+
String constantString2 = "Baeldung";
12+
13+
assertThat(constantString1).isSameAs(constantString2);
14+
}
15+
16+
@Test
17+
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
18+
String newString1 = new String("Baeldung");
19+
String newString2 = new String("Baeldung");
20+
21+
assertThat(newString1).isNotSameAs(newString2);
22+
}
23+
24+
@Test
25+
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
26+
String constantString = "Baeldung";
27+
String newString = new String("Baeldung");
28+
29+
assertThat(constantString).isNotSameAs(newString);
30+
}
31+
32+
@Test
33+
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
34+
String constantString = "interned Baeldung";
35+
String newString = new String("interned Baeldung");
36+
37+
assertThat(constantString).isNotSameAs(newString);
38+
39+
String internedString = newString.intern();
40+
41+
assertThat(constantString).isSameAs(internedString);
42+
}
43+
}

‎drools/backward-chaining/pom.xml‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
54
<modelVersion>4.0.0</modelVersion>
65

76
<artifactId>drools-backward-chaining</artifactId>
@@ -17,7 +16,6 @@
1716
<properties>
1817
<runtime.version>6.4.0.Final</runtime.version>
1918
</properties>
20-
2119
<dependencies>
2220
<dependency>
2321
<groupId>org.kie</groupId>
@@ -26,7 +24,6 @@
2624
</dependency>
2725
<dependency>
2826
<groupId>org.drools</groupId>
29-
<artifactId>drools-core</artifactId>
3027
<version>${runtime.version}</version>
3128
</dependency>
3229
<dependency>
@@ -35,4 +32,4 @@
3532
<version>${runtime.version}</version>
3633
</dependency>
3734
</dependencies>
38-
</project>
35+
</project>

0 commit comments

Comments
 (0)