[JAVA-6173] stream.parallelStream and Collections parallel stream differences

This commit is contained in:
Bhaskar
2023-04-10 23:49:37 +05:30
parent acedc7f216
commit 48255ee387
6 changed files with 77 additions and 6 deletions

View File

@@ -1,47 +0,0 @@
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;
}
}