Merge pull request #6153 from cror/stream-count
BAEL-2546: Stream.count
This commit is contained in:
commit
8a4c326357
|
@ -32,7 +32,7 @@ public class Customer {
|
||||||
return this.points > points;
|
return this.points > points;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasOverThousandPoints() {
|
public boolean hasOverHundredPoints() {
|
||||||
return this.points > 100;
|
return this.points > 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
package com.baeldung.stream.filter;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class StreamCountUnitTest {
|
||||||
|
|
||||||
|
private List<Customer> customers;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
|
||||||
|
Customer sarah = new Customer("Sarah M.", 200);
|
||||||
|
Customer charles = new Customer("Charles B.", 150);
|
||||||
|
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
|
||||||
|
customers = Arrays.asList(john, sarah, charles, mary);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfCustomers_whenCount_thenGetListSize() {
|
||||||
|
long count = customers
|
||||||
|
.stream()
|
||||||
|
.count();
|
||||||
|
|
||||||
|
assertThat(count).isEqualTo(4L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfCustomers_whenFilterByPointsOver100AndCount_thenGetTwo() {
|
||||||
|
long countBigCustomers = customers
|
||||||
|
.stream()
|
||||||
|
.filter(c -> c.getPoints() > 100)
|
||||||
|
.count();
|
||||||
|
|
||||||
|
assertThat(countBigCustomers).isEqualTo(2L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfCustomers_whenFilterByPointsAndNameAndCount_thenGetOne() {
|
||||||
|
long count = customers
|
||||||
|
.stream()
|
||||||
|
.filter(c -> c.getPoints() > 10 && c.getName().startsWith("Charles"))
|
||||||
|
.count();
|
||||||
|
|
||||||
|
assertThat(count).isEqualTo(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfCustomers_whenNoneMatchesFilterAndCount_thenGetZero() {
|
||||||
|
long count = customers
|
||||||
|
.stream()
|
||||||
|
.filter(c -> c.getPoints() > 500)
|
||||||
|
.count();
|
||||||
|
|
||||||
|
assertThat(count).isEqualTo(0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfCustomers_whenUsingMethodOverHundredPointsAndCount_thenGetTwo() {
|
||||||
|
long count = customers
|
||||||
|
.stream()
|
||||||
|
.filter(Customer::hasOverHundredPoints)
|
||||||
|
.count();
|
||||||
|
|
||||||
|
assertThat(count).isEqualTo(2L);
|
||||||
|
}
|
||||||
|
}
|
|
@ -62,7 +62,7 @@ public class StreamFilterUnitTest {
|
||||||
|
|
||||||
List<Customer> customersWithMoreThan100Points = customers
|
List<Customer> customersWithMoreThan100Points = customers
|
||||||
.stream()
|
.stream()
|
||||||
.filter(Customer::hasOverThousandPoints)
|
.filter(Customer::hasOverHundredPoints)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
assertThat(customersWithMoreThan100Points).hasSize(2);
|
assertThat(customersWithMoreThan100Points).hasSize(2);
|
||||||
|
@ -81,7 +81,7 @@ public class StreamFilterUnitTest {
|
||||||
.flatMap(c -> c
|
.flatMap(c -> c
|
||||||
.map(Stream::of)
|
.map(Stream::of)
|
||||||
.orElseGet(Stream::empty))
|
.orElseGet(Stream::empty))
|
||||||
.filter(Customer::hasOverThousandPoints)
|
.filter(Customer::hasOverHundredPoints)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
assertThat(customersWithMoreThan100Points).hasSize(2);
|
assertThat(customersWithMoreThan100Points).hasSize(2);
|
||||||
|
@ -156,4 +156,5 @@ public class StreamFilterUnitTest {
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList())).isInstanceOf(RuntimeException.class);
|
.collect(Collectors.toList())).isInstanceOf(RuntimeException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue