Skip to content

Commit cc8a7ca

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 3cfedd0 + 8424a10 commit cc8a7ca

File tree

1,431 files changed

+85208
-5563
lines changed

Some content is hidden

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

1,431 files changed

+85208
-5563
lines changed

akka-http/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<properties>
4242
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4343
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
44-
<java.version>1.8</java.version>
4544
<akka.http.version>10.0.11</akka.http.version>
4645
<akka.stream.version>2.5.11</akka.stream.version>
4746
</properties>

akka-streams/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
<dependencies>
1515
<dependency>
1616
<groupId>com.typesafe.akka</groupId>
17-
<artifactId>akka-stream_2.11</artifactId>
17+
<artifactId>akka-stream_${scala.version}</artifactId>
1818
<version>${akkastreams.version}</version>
1919
</dependency>
2020
<dependency>
2121
<groupId>com.typesafe.akka</groupId>
22-
<artifactId>akka-stream-testkit_2.11</artifactId>
22+
<artifactId>akka-stream-testkit_${scala.version}</artifactId>
2323
<version>${akkastreams.version}</version>
2424
</dependency>
2525
</dependencies>
2626

2727
<properties>
2828
<akkastreams.version>2.5.2</akkastreams.version>
29+
<scala.version>2.11</scala.version>
2930
</properties>
3031

3132
</project>

algorithms-genetic/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
</build>
5555

5656
<properties>
57-
<lombok.version>1.16.12</lombok.version>
5857
<commons-math3.version>3.6.1</commons-math3.version>
5958
<io.jenetics.version>3.7.0</io.jenetics.version>
6059
<org.assertj.core.version>3.9.0</org.assertj.core.version>

algorithms-miscellaneous-1/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
1818
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
1919
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
20+
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)

algorithms-miscellaneous-1/pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<version>${org.assertj.core.version}</version>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.github.dpaukov</groupId>
44+
<artifactId>combinatoricslib3</artifactId>
45+
<version>3.3.0</version>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>
@@ -74,11 +79,10 @@
7479
</reporting>
7580

7681
<properties>
77-
<lombok.version>1.16.12</lombok.version>
7882
<commons-math3.version>3.6.1</commons-math3.version>
7983
<org.assertj.core.version>3.9.0</org.assertj.core.version>
8084
<commons-codec.version>1.11</commons-codec.version>
81-
<guava.version>25.1-jre</guava.version>
85+
<guava.version>27.0.1-jre</guava.version>
8286
</properties>
8387

8488
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.algorithms.combination;
2+
3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
6+
import org.apache.commons.math3.util.CombinatoricsUtils;
7+
8+
public class ApacheCommonsCombinationGenerator {
9+
10+
private static final int N = 6;
11+
private static final int R = 3;
12+
13+
/**
14+
* Print all combinations of r elements from a set
15+
* @param n - number of elements in set
16+
* @param r - number of elements in selection
17+
*/
18+
public static void generate(int n, int r) {
19+
Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(n, r);
20+
while (iterator.hasNext()) {
21+
final int[] combination = iterator.next();
22+
System.out.println(Arrays.toString(combination));
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
generate(N, R);
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.algorithms.combination;
2+
3+
import org.paukov.combinatorics3.Generator;
4+
5+
public class CombinatoricsLibCombinationGenerator {
6+
7+
public static void main(String[] args) {
8+
Generator.combination(0, 1, 2, 3, 4, 5)
9+
.simple(3)
10+
.stream()
11+
.forEach(System.out::println);
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.algorithms.combination;
2+
3+
import java.util.Arrays;
4+
import java.util.Set;
5+
6+
import com.google.common.collect.ImmutableSet;
7+
import com.google.common.collect.Sets;
8+
9+
public class GuavaCombinationsGenerator {
10+
11+
public static void main(String[] args) {
12+
13+
Set<Set<Integer>> combinations = Sets.combinations(ImmutableSet.of(0, 1, 2, 3, 4, 5), 3);
14+
System.out.println(combinations.size());
15+
System.out.println(Arrays.toString(combinations.toArray()));
16+
}
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.algorithms.combination;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class IterativeCombinationGenerator {
8+
9+
private static final int N = 5;
10+
private static final int R = 2;
11+
12+
/**
13+
* Generate all combinations of r elements from a set
14+
* @param n the number of elements in input set
15+
* @param r the number of elements in a combination
16+
* @return the list containing all combinations
17+
*/
18+
public List<int[]> generate(int n, int r) {
19+
List<int[]> combinations = new ArrayList<>();
20+
int[] combination = new int[r];
21+
22+
// initialize with lowest lexicographic combination
23+
for (int i = 0; i < r; i++) {
24+
combination[i] = i;
25+
}
26+
27+
while (combination[r - 1] < n) {
28+
combinations.add(combination.clone());
29+
30+
// generate next combination in lexicographic order
31+
int t = r - 1;
32+
while (t != 0 && combination[t] == n - r + t) {
33+
t--;
34+
}
35+
combination[t]++;
36+
for (int i = t + 1; i < r; i++) {
37+
combination[i] = combination[i - 1] + 1;
38+
}
39+
}
40+
41+
return combinations;
42+
}
43+
44+
public static void main(String[] args) {
45+
IterativeCombinationGenerator generator = new IterativeCombinationGenerator();
46+
List<int[]> combinations = generator.generate(N, R);
47+
System.out.println(combinations.size());
48+
for (int[] combination : combinations) {
49+
System.out.println(Arrays.toString(combination));
50+
}
51+
}
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.algorithms.combination;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class SelectionRecursiveCombinationGenerator {
8+
9+
private static final int N = 6;
10+
private static final int R = 3;
11+
12+
/**
13+
* Generate all combinations of r elements from a set
14+
* @param n - number of elements in input set
15+
* @param r - number of elements to be chosen
16+
* @return the list containing all combinations
17+
*/
18+
public List<int[]> generate(int n, int r) {
19+
List<int[]> combinations = new ArrayList<>();
20+
helper(combinations, new int[r], 0, n - 1, 0);
21+
return combinations;
22+
}
23+
24+
/**
25+
* Choose elements from set by recursing over elements selected
26+
* @param combinations - List to store generated combinations
27+
* @param data - current combination
28+
* @param start - starting element of remaining set
29+
* @param end - last element of remaining set
30+
* @param index - number of elements chosen so far.
31+
*/
32+
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
33+
if (index == data.length) {
34+
int[] combination = data.clone();
35+
combinations.add(combination);
36+
} else {
37+
int max = Math.min(end, end + 1 - data.length + index);
38+
for (int i = start; i <= max; i++) {
39+
data[index] = i;
40+
helper(combinations, data, i + 1, end, index + 1);
41+
}
42+
}
43+
}
44+
45+
public static void main(String[] args) {
46+
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
47+
List<int[]> combinations = generator.generate(N, R);
48+
for (int[] combination : combinations) {
49+
System.out.println(Arrays.toString(combination));
50+
}
51+
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
52+
}
53+
}

0 commit comments

Comments
 (0)