Merge pull request #13353 from amit88265/collections-vs-stream
BAEL-5953 - Added code of collections-vs-stream
This commit is contained in:
commit
cde92e09f9
|
@ -5,6 +5,9 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-query-3</artifactId>
|
||||
<name>spring-data-jpa-query-3</name>
|
||||
<properties>
|
||||
<javafaker.version>0.15</javafaker.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -22,6 +25,11 @@
|
|||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>${javafaker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.spring.data.jpa.collectionsvsstream;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ListVsStreamQueryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ListVsStreamQueryApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.spring.data.jpa.collectionsvsstream;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "_user")
|
||||
public class User {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String firstName, String lastName, int age) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public User(String firstName, String lastName, int age, int id) {
|
||||
this(firstName, lastName, age);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.spring.data.jpa.collectionsvsstream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, String> {
|
||||
Stream<User> findAllByAgeGreaterThan(int age);
|
||||
|
||||
List<User> findByAgeGreaterThan(int age);
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.spring.data.jpa.collectionsvsstream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
|
||||
import com.github.javafaker.Faker;
|
||||
|
||||
@DataJpaTest
|
||||
class UserRepositoryUnitTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
Faker faker = new Faker();
|
||||
List<User> people = IntStream.range(1, 100)
|
||||
.parallel()
|
||||
.mapToObj(i -> new User(faker.name()
|
||||
.firstName(), faker.name()
|
||||
.lastName(), faker.number()
|
||||
.numberBetween(1, 100), i))
|
||||
.collect(Collectors.toList());
|
||||
userRepository.saveAll(people);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAList() {
|
||||
List<User> users = userRepository.findByAgeGreaterThan(20);
|
||||
assertThat(users).isNotEmpty();
|
||||
assertThat(users.stream()
|
||||
.map(User::getAge)
|
||||
.allMatch(age -> age > 20)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAStream() {
|
||||
Stream<User> users = userRepository.findAllByAgeGreaterThan(20);
|
||||
assertThat(users).isNotNull();
|
||||
assertThat(users.map(User::getAge)
|
||||
.allMatch(age -> age > 20)).isTrue();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue