Skip to content

Instantly share code, notes, and snippets.

@BenHunt-io
Created May 22, 2023 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenHunt-io/7a0748aec8967c3de710ada064cc8787 to your computer and use it in GitHub Desktop.
Save BenHunt-io/7a0748aec8967c3de710ada064cc8787 to your computer and use it in GitHub Desktop.
package streams;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class StreamPlayground {
private class User {
String username;
boolean active;
Address address;
public User(String username, boolean active, Address address) {
this.username = username;
this.active = active;
this.address = address;
}
}
private class Address {
String state;
String street;
public Address(String state, String street) {
this.state = state;
this.street = street;
}
}
private List<User> users = new ArrayList<>();
@BeforeEach
void beforeEach() {
users.clear(); // Clear the list before each test
users.add(new User("User1", true, new Address("California", "123 Main St")));
users.add(new User("User2", false, new Address("New York", "456 Broadway")));
users.add(new User("User3", true, new Address("Texas", "789 Houston Ave")));
users.add(new User("User4", false, new Address("Florida", "321 Miami Blvd")));
users.add(new User("User5", true, new Address("Illinois", "654 Chicago Rd")));
users.add(new User("User6", false, new Address("Washington", "888 Seattle Way")));
users.add(new User("User7", true, new Address("Oregon", "999 Portland Ave")));
users.add(new User("User8", false, new Address("Nevada", "111 Las Vegas Blvd")));
users.add(new User("User9", true, new Address("Arizona", "222 Phoenix St")));
users.add(new User("User10", false, new Address("Michigan", "333 Detroit Ave")));
}
// class Streem {
// Listt list;
// public Streem(Listt list){
// this.list = list;
// }
// public filter()
// }
// class Listt {
// String[] firstNames;
// Streem stream(){
// return new Streem(this);
// }
// }
@Test
void activeUsersInTexasTest(){
long count = 0;
count = users.stream()
.filter(user -> user.address.state.equals("Texas") && user.active)
//no more stream ops, terminate
.count();
//count = list.size
assertEquals(1l, count);
// 1. Call stream() on any collection object to get reference to Stream class
// 2. Perform functional operations like map, filter, min, max, etc
// 3. Perform terminating operation
}
@Test
void streamDemo() {
// Imperative
List<String> usernames = new ArrayList<>();
for(int i = 0; i<users.size(); i++){
usernames.add(users.get(i).username);
}
// Functional
usernames = users.stream() // stream of 10 users
// for each user, return me just the username
.map(user -> user.username)
// I want to only keep the usernames that are less the 10 characters in length
.filter(username -> username.length() < 10)
.collect(Collectors.toList());
// Functional
Optional<String> firstUsernameLessThan10Chars = users.stream() // stream of 10 users
// for each user, return me just the username
.map(user -> user.username)
// I want to only keep the usernames that are less the 10 characters in length
.filter(username -> username.length() < 10)
.findFirst();
if(firstUsernameLessThan10Chars.isPresent()){
System.out.println("present");
}else{
throw new RuntimeException("error");
}
// 1. Call stream() on any collection object to get reference to Stream class
// 2. Perform functional operations like map, filter, min, max, etc
// 3. Perform terminating operation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment