Skip to content

Commit f5352c5

Browse files
author
markusgulden
authored
Merge pull request eugenp#3 from eugenp/master
Update
2 parents 02b0c1d + 6f3710b commit f5352c5

346 files changed

Lines changed: 186674 additions & 1335 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.

‎.travis.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ language: java
33
before_install:
44
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
55

6-
install: travis_wait 60 mvn -q test -fae
6+
install: skip
7+
script: travis_wait 60 mvn -q test -fae
78

89
sudo: required
910

‎algorithms/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
1616
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
1717
- [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance)
18+
- [How to Find the Kth Largest Element in Java](http://www.baeldung.com/java-kth-largest-element)

‎algorithms/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<artifactId>jgrapht-core</artifactId>
4040
<version>1.0.1</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-core</artifactId>
45+
<version>3.9.0</version>
46+
<scope>test</scope>
47+
</dependency>
4248
</dependencies>
4349

4450
<build>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.baeldung.algorithms.kthlargest;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.stream.IntStream;
6+
7+
public class FindKthLargest {
8+
9+
public int findKthLargestBySorting(Integer[] arr, int k) {
10+
Arrays.sort(arr);
11+
int targetIndex = arr.length - k;
12+
return arr[targetIndex];
13+
}
14+
15+
public int findKthLargestBySortingDesc(Integer[] arr, int k) {
16+
Arrays.sort(arr, Collections.reverseOrder());
17+
return arr[k - 1];
18+
}
19+
20+
public int findKthElementByQuickSelect(Integer[] arr, int left, int right, int k) {
21+
if (k >= 0 && k <= right - left + 1) {
22+
int pos = partition(arr, left, right);
23+
if (pos - left == k) {
24+
return arr[pos];
25+
}
26+
if (pos - left > k) {
27+
return findKthElementByQuickSelect(arr, left, pos - 1, k);
28+
}
29+
return findKthElementByQuickSelect(arr, pos + 1, right, k - pos + left - 1);
30+
}
31+
return 0;
32+
}
33+
34+
public int findKthElementByQuickSelectWithIterativePartition(Integer[] arr, int left, int right, int k) {
35+
if (k >= 0 && k <= right - left + 1) {
36+
int pos = partitionIterative(arr, left, right);
37+
if (pos - left == k) {
38+
return arr[pos];
39+
}
40+
if (pos - left > k) {
41+
return findKthElementByQuickSelectWithIterativePartition(arr, left, pos - 1, k);
42+
}
43+
return findKthElementByQuickSelectWithIterativePartition(arr, pos + 1, right, k - pos + left - 1);
44+
}
45+
return 0;
46+
}
47+
48+
private int partition(Integer[] arr, int left, int right) {
49+
int pivot = arr[right];
50+
Integer[] leftArr;
51+
Integer[] rightArr;
52+
53+
leftArr = IntStream.range(left, right)
54+
.filter(i -> arr[i] < pivot)
55+
.map(i -> arr[i])
56+
.boxed()
57+
.toArray(Integer[]::new);
58+
59+
rightArr = IntStream.range(left, right)
60+
.filter(i -> arr[i] > pivot)
61+
.map(i -> arr[i])
62+
.boxed()
63+
.toArray(Integer[]::new);
64+
65+
int leftArraySize = leftArr.length;
66+
System.arraycopy(leftArr, 0, arr, left, leftArraySize);
67+
arr[leftArraySize + left] = pivot;
68+
System.arraycopy(rightArr, 0, arr, left + leftArraySize + 1, rightArr.length);
69+
70+
return left + leftArraySize;
71+
}
72+
73+
private int partitionIterative(Integer[] arr, int left, int right) {
74+
int pivot = arr[right], i = left;
75+
for (int j = left; j <= right - 1; j++) {
76+
if (arr[j] <= pivot) {
77+
swap(arr, i, j);
78+
i++;
79+
}
80+
}
81+
swap(arr, i, right);
82+
return i;
83+
}
84+
85+
public int findKthElementByRandomizedQuickSelect(Integer[] arr, int left, int right, int k) {
86+
if (k >= 0 && k <= right - left + 1) {
87+
int pos = randomPartition(arr, left, right);
88+
if (pos - left == k) {
89+
return arr[pos];
90+
}
91+
if (pos - left > k) {
92+
return findKthElementByRandomizedQuickSelect(arr, left, pos - 1, k);
93+
}
94+
return findKthElementByRandomizedQuickSelect(arr, pos + 1, right, k - pos + left - 1);
95+
}
96+
return 0;
97+
}
98+
99+
private int randomPartition(Integer arr[], int left, int right) {
100+
int n = right - left + 1;
101+
int pivot = (int) (Math.random()) % n;
102+
swap(arr, left + pivot, right);
103+
return partition(arr, left, right);
104+
}
105+
106+
private void swap(Integer[] arr, int n1, int n2) {
107+
int temp = arr[n2];
108+
arr[n2] = arr[n1];
109+
arr[n1] = temp;
110+
}
111+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.algorithms.kthlargest;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class FindKthLargestUnitTest {
9+
10+
private FindKthLargest findKthLargest;
11+
private Integer[] arr = { 3, 7, 1, 2, 8, 10, 4, 5, 6, 9 };
12+
13+
@Before
14+
public void setup() {
15+
findKthLargest = new FindKthLargest();
16+
}
17+
18+
@Test
19+
public void givenIntArray_whenFindKthLargestBySorting_thenGetResult() {
20+
int k = 3;
21+
assertThat(findKthLargest.findKthLargestBySorting(arr, k)).isEqualTo(8);
22+
}
23+
24+
@Test
25+
public void givenIntArray_whenFindKthLargestBySortingDesc_thenGetResult() {
26+
int k = 3;
27+
assertThat(findKthLargest.findKthLargestBySortingDesc(arr, k)).isEqualTo(8);
28+
}
29+
30+
@Test
31+
public void givenIntArray_whenFindKthLargestByQuickSelect_thenGetResult() {
32+
int k = 3;
33+
int kthLargest = arr.length - k;
34+
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
35+
}
36+
37+
@Test
38+
public void givenIntArray_whenFindKthElementByQuickSelectIterative_thenGetResult() {
39+
int k = 3;
40+
int kthLargest = arr.length - k;
41+
assertThat(findKthLargest.findKthElementByQuickSelectWithIterativePartition(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
42+
}
43+
44+
@Test
45+
public void givenIntArray_whenFindKthSmallestByQuickSelect_thenGetResult() {
46+
int k = 3;
47+
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, k - 1)).isEqualTo(3);
48+
}
49+
50+
@Test
51+
public void givenIntArray_whenFindKthLargestByRandomizedQuickSelect_thenGetResult() {
52+
int k = 3;
53+
int kthLargest = arr.length - k;
54+
assertThat(findKthLargest.findKthElementByRandomizedQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
55+
}
56+
57+
}

‎core-java-8/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
3535
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)
3636
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
37+
- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream)
38+
- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream)
39+
- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices)

‎core-java-8/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@
101101
<version>1.19</version>
102102
</dependency>
103103

104+
<dependency>
105+
<groupId>com.codepoetics</groupId>
106+
<artifactId>protonpack</artifactId>
107+
<version>${protonpack.version}</version>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>io.vavr</groupId>
112+
<artifactId>vavr</artifactId>
113+
<version>${vavr.version}</version>
114+
</dependency>
115+
116+
<dependency>
117+
<groupId>one.util</groupId>
118+
<artifactId>streamex</artifactId>
119+
<version>${streamex.version}</version>
120+
</dependency>
104121
</dependencies>
105122

106123
<build>
@@ -267,6 +284,9 @@
267284
<collections-generic.version>4.01</collections-generic.version>
268285
<commons-codec.version>1.10</commons-codec.version>
269286
<lombok.version>1.16.12</lombok.version>
287+
<vavr.version>0.9.0</vavr.version>
288+
<protonpack.version>1.13</protonpack.version>
289+
<streamex.version>0.6.5</streamex.version>
270290

271291
<!-- testing -->
272292
<assertj.version>3.6.1</assertj.version>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
import java.util.List;
4+
5+
public class Article {
6+
private List<Author> listOfAuthors;
7+
private int id;
8+
private String name;
9+
10+
public Article(String name) {
11+
this.name = name;
12+
}
13+
14+
public Article(List<Author> listOfAuthors, int id) {
15+
super();
16+
this.listOfAuthors = listOfAuthors;
17+
this.id = id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public int getId() {
29+
return id;
30+
}
31+
32+
public void setId(int id) {
33+
this.id = id;
34+
}
35+
36+
public List<Author> getListOfAuthors() {
37+
return listOfAuthors;
38+
}
39+
40+
public void setListOfAuthors(List<Author> listOfAuthors) {
41+
this.listOfAuthors = listOfAuthors;
42+
}
43+
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
public class Author {
4+
private String name;
5+
private int relatedArticleId;
6+
7+
public Author(String name, int relatedArticleId) {
8+
this.name = name;
9+
this.relatedArticleId = relatedArticleId;
10+
}
11+
12+
public int getRelatedArticleId() {
13+
return relatedArticleId;
14+
}
15+
16+
public void setRelatedArticleId(int relatedArticleId) {
17+
this.relatedArticleId = relatedArticleId;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "[name: " + name + ", relatedId: " + relatedArticleId + "]";
31+
}
32+
}
33+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Spliterator;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.IntStream;
10+
import java.util.stream.Stream;
11+
import java.util.stream.StreamSupport;
12+
13+
public class Executor {
14+
public void executeCustomSpliterator() {
15+
Article article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
16+
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
17+
new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
18+
Stream<Author> stream = IntStream.range(0, article.getListOfAuthors()
19+
.size())
20+
.mapToObj(article.getListOfAuthors()::get);
21+
System.out.println("count= " + countAutors(stream.parallel()));
22+
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
23+
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
24+
System.out.println("count= " + countAutors(stream2.parallel()));
25+
}
26+
27+
public void executeSpliterator() {
28+
Spliterator<Article> split1 = generateElements().spliterator();
29+
Spliterator<Article> split2 = split1.trySplit();
30+
ExecutorService service = Executors.newCachedThreadPool();
31+
service.execute(new Task(split1));
32+
service.execute(new Task(split2));
33+
}
34+
35+
private static int countAutors(Stream<Author> stream) {
36+
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
37+
return wordCounter.getCounter();
38+
}
39+
40+
private List<Article> generateElements() {
41+
return Stream.generate(() -> new Article("Java"))
42+
.limit(35000)
43+
.collect(Collectors.toList());
44+
}
45+
}

0 commit comments

Comments
 (0)