Repository navigation
Sheffield | 26-Java-June | Hassan Osman | Sprint 4 | Prep: Streams and Optionals #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed9f927
dd93b7a
24bb271
d0765af
0f368f8
02528ae
f835778
172fe39
5f3f7ab
2a06823
2c027c0
9169d0a
a230588
a545bc3
12fae81
7650ddc
2e3f3b2
347a47e
06d4b8b
abeb206
69d6a15
d2f23f7
5569886
a34ed07
de9cf83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # IntelliJ | ||
| .idea/ | ||
| *.iml | ||
|
|
||
| # IntelliJ build output | ||
| out/ | ||
|
|
||
| # Compiled class files | ||
| *.class |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>exercise</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>26</maven.compiler.source> | ||
| <maven.compiler.target>26</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>5.14.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class NameFinder { | ||
|
|
||
| public void printNamesStartingWithA(List<String> listOfNames) { | ||
| List<String> namesStartsWithA = listOfNames.stream() | ||
| .filter(name -> name.toUpperCase().startsWith("A")) | ||
| .forEach(System.out::println); | ||
|
|
||
| System.out.println(namesStartsWithA); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public void vowelNamesCapitalisedDescending(List<String> listOfNames) { | ||
| List<String> namesStartsWithVowel = listOfNames.stream() | ||
| .filter(name -> name.toUpperCase().matches("^[AEIOU].*")) | ||
| .collect(Collectors.toList()).reversed(); | ||
|
|
||
| System.out.println(namesStartsWithVowel); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Remainders { | ||
|
|
||
| public void printRemainders(int divisor) { | ||
| List<Integer> remainders = new ArrayList<>(); | ||
| for (int i = 1; i <= 20; i ++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come we aren't streaming here? |
||
| remainders.add(i % divisor); | ||
| } | ||
| String output = remainders.stream() | ||
| .map(String::valueOf) | ||
| .collect(Collectors.joining(",")); | ||
|
|
||
| System.out.println(output); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public Map<Integer, List<Integer>> remainderGroups(int divisor) { | ||
|
|
||
| Map<Integer, List<Integer>> result = IntStream.rangeClosed(1, 20) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| .boxed() | ||
| .collect(Collectors.groupingBy(number -> number % divisor)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public Map<Integer, Long> remainderCounts(int divisor) { | ||
|
|
||
| Map<Integer, Long> result = IntStream.rangeClosed(1, 20) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| .boxed() | ||
| .collect(Collectors.groupingBy(number -> number % divisor, Collectors.counting())); | ||
|
|
||
| return result; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ShapeCounter { | ||
|
|
||
| public long countShapesWithSides(Map<String, Integer> shapesToSides, int numberOfSides) { | ||
|
|
||
| return shapesToSides.values().stream() | ||
| .filter(value -> value == numberOfSides) | ||
| .count(); | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Can you do the printing without needing to create the list or any other variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can. I don't know for whatever reason I presumed that the stream out should be packaged back into a list.