Skip to content

Commit cebfa6b

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 531d6da + c8d11b4 commit cebfa6b

272 files changed

Lines changed: 184631 additions & 480 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.

‎apache-poi/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
### Relevant Articles:
22
- [Microsoft Word Processing in Java with Apache POI](http://www.baeldung.com/java-microsoft-word-with-apache-poi)
33
- [Working with Microsoft Excel in Java](http://www.baeldung.com/java-microsoft-excel)
4+
- [Creating a MS PowerPoint Presentation in Java](https://github.com/eugenp/tutorials/tree/master/apache-poi)

‎core-java-8/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@
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)
40+
- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency)
41+
- [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams)
42+
- [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)

‎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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
public class RelatedAuthorCounter {
4+
private final int counter;
5+
private final boolean isRelated;
6+
7+
public RelatedAuthorCounter(int counter, boolean isRelated) {
8+
this.counter = counter;
9+
this.isRelated = isRelated;
10+
}
11+
12+
public RelatedAuthorCounter accumulate(Author author) {
13+
if (author.getRelatedArticleId() == 0) {
14+
return isRelated ? this : new RelatedAuthorCounter(counter, true);
15+
} else {
16+
return isRelated ? new RelatedAuthorCounter(counter + 1, false) : this;
17+
}
18+
}
19+
20+
public RelatedAuthorCounter combine(RelatedAuthorCounter RelatedAuthorCounter) {
21+
return new RelatedAuthorCounter(counter + RelatedAuthorCounter.counter, RelatedAuthorCounter.isRelated);
22+
}
23+
24+
public int getCounter() {
25+
return counter;
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
import java.util.List;
4+
import java.util.Spliterator;
5+
import java.util.function.Consumer;
6+
7+
public class RelatedAuthorSpliterator implements Spliterator<Author> {
8+
private final List<Author> list;
9+
private int current = 0;
10+
11+
public RelatedAuthorSpliterator(List<Author> list) {
12+
this.list = list;
13+
}
14+
15+
@Override
16+
public boolean tryAdvance(Consumer<? super Author> action) {
17+
action.accept(list.get(current++));
18+
return current < list.size();
19+
}
20+
21+
@Override
22+
public Spliterator<Author> trySplit() {
23+
int currentSize = list.size() - current;
24+
if (currentSize < 10) {
25+
return null;
26+
}
27+
for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) {
28+
if (list.get(splitPos)
29+
.getRelatedArticleId() == 0) {
30+
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos));
31+
current = splitPos;
32+
return spliterator;
33+
}
34+
}
35+
return null;
36+
}
37+
38+
@Override
39+
public long estimateSize() {
40+
return list.size() - current;
41+
}
42+
43+
@Override
44+
public int characteristics() {
45+
return SIZED + CONCURRENT;
46+
}
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
import java.util.Spliterator;
4+
5+
public class Task implements Runnable {
6+
private Spliterator<Article> spliterator;
7+
private final static String SUFFIX = "- published by Baeldung";
8+
9+
public Task(Spliterator<Article> spliterator) {
10+
this.spliterator = spliterator;
11+
}
12+
13+
@Override
14+
public void run() {
15+
int current = 0;
16+
while (spliterator.tryAdvance(article -> {
17+
article.setName(article.getName()
18+
.concat(SUFFIX));
19+
})) {
20+
current++;
21+
}
22+
;
23+
System.out.println(Thread.currentThread()
24+
.getName() + ":" + current);
25+
}
26+
}

core-java/src/main/java/com/baeldung/stream/StreamIndices.java renamed to core-java-8/src/main/java/com/baeldung/stream/StreamIndices.java

File renamed without changes.

0 commit comments

Comments
 (0)