[JAVA-6173] Difference Between parallelStream() and stream().parallel() (#13653)

* [JAVA-6173] stream.parallel and parallelStream

* [JAVA-6173] stream.parallel and parallelStream test cases

---------

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2023-03-24 23:10:30 +05:30 committed by GitHub
parent ae6493b568
commit bc0627317c
5 changed files with 258 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.streams.parallelstream;
public class Book {
private String name;
private String author;
private int yearPublished;
public Book(String name, String author, int yearPublished) {
this.name = name;
this.author = author;
this.yearPublished = yearPublished;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getYearPublished() {
return yearPublished;
}
public void setYearPublished(int yearPublished) {
this.yearPublished = yearPublished;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.streams.parallelstream;
import java.util.Spliterator;
import java.util.function.Consumer;
public class BookSpliterator<T> implements Spliterator<T> {
private final Object[] books;
private int startIndex;
public BookSpliterator(Object[] books, int startIndex) {
this.books = books;
this.startIndex = startIndex;
}
@Override
public Spliterator<T> trySplit() {
// Always Assuming that the source is too small to split, returning null
return null;
}
// Other overridden methods such as tryAdvance(), estimateSize() etc
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (startIndex < books.length) {
startIndex += 2;
return true;
}
return false;
}
@Override
public long estimateSize() {
return books.length - startIndex;
}
@Override
public int characteristics() {
return CONCURRENT;
}
}

View File

@ -0,0 +1,93 @@
package com.baeldung.streams.parallelstream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class MyBookContainer<T> implements Collection<T> {
private static final long serialVersionUID = 1L;
private T[] elements;
public MyBookContainer(T[] elements) {
this.elements = elements;
}
@Override
public Spliterator<T> spliterator() {
return new BookSpliterator(elements, 0);
}
@Override
public Stream<T> parallelStream() {
return StreamSupport.stream(spliterator(), false);
}
// standard overridden methods of Collection Interface
@Override
public int size() {
return elements.length;
}
@Override
public boolean isEmpty() {
return elements.length == 0;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<T> iterator() {
return null;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T1> T1[] toArray(T1[] a) {
return null;
}
@Override
public boolean add(T t) {
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends T> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.streams.parallelstream;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong;
public class ParallelStreamApplication {
public long usingCollectionsParallel(Collection<Book> listOfbooks, int year) {
AtomicLong countOfBooks = new AtomicLong();
listOfbooks.parallelStream()
.forEach(book -> {
if (book.getYearPublished() == year) {
countOfBooks.getAndIncrement();
}
});
return countOfBooks.get();
}
public long usingStreamParallel(Collection<Book> listOfBooks, int year) {
AtomicLong countOfBooks = new AtomicLong();
listOfBooks.stream()
.parallel()
.forEach(book -> {
if (book.getYearPublished() == year) {
countOfBooks.getAndIncrement();
}
});
return countOfBooks.get();
}
public long usingWithCustomSpliterator(MyBookContainer<Book> listOfBooks, int year) {
AtomicLong countOfBooks = new AtomicLong();
listOfBooks.parallelStream()
.forEach(book -> {
if (book.getYearPublished() == year) {
countOfBooks.getAndIncrement();
}
});
return countOfBooks.get();
}
}

View File

@ -0,0 +1,47 @@
package parallelstream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.streams.parallelstream.Book;
import com.baeldung.streams.parallelstream.MyBookContainer;
import com.baeldung.streams.parallelstream.ParallelStreamApplication;
public class ParallelStreamUnitTest {
@Test
public void givenCollectionWhenCollectionsParallelIsUsedThenReturnCount() {
ParallelStreamApplication parallelStreamApplication = new ParallelStreamApplication();
Assert.assertEquals(parallelStreamApplication.usingCollectionsParallel(generateListOfBooks(), 1974), 2);
}
@Test
public void givenCollectionWhenStreamParallelIsUsedThenReturnCount() {
ParallelStreamApplication parallelStreamApplication = new ParallelStreamApplication();
Assert.assertEquals(parallelStreamApplication.usingStreamParallel(generateListOfBooks(), 1974), 2);
}
@Test
public void givenBookContainerWhenParallelStreamIsUsedThenReturnIncorrectCount() {
ParallelStreamApplication parallelStreamApplication = new ParallelStreamApplication();
Assert.assertNotEquals(parallelStreamApplication.usingWithCustomSpliterator(getBookContainer(), 1974), 2);
}
private List<Book> generateListOfBooks() {
Book book1 = new Book("The Blue Umbrella", "Ruskin Bond", 1974);
Book book2 = new Book("Carrie", "Stephen King", 1974);
Book book3 = new Book("The Psychology of money", "Morgan Housel", 2020);
List<Book> books = List.of(book1, book2, book3);
return books;
}
private MyBookContainer<Book> getBookContainer() {
MyBookContainer<Book> listOfBooks = new MyBookContainer<>(new Book[] { new Book("The Blue Umbrella", "Ruskin Bond", 1974),
new Book("Carrie", "Stephen King", 1974),
new Book("The Psychology of money", "Morgan Housel", 2020)});
return listOfBooks;
}
}