├── .github
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
└── workflows
│ └── validation.yml
├── .gitignore
├── code_of_conduct.md
├── contributing.md
├── license
├── readme.md
├── src
├── tasks
│ ├── StreamTasks.java
│ ├── StreamTasksExample.java
│ ├── StreamTasksMain.java
│ ├── package-info.java
│ ├── solution
│ │ ├── StreamTasksSolution.java
│ │ └── package-info.java
│ └── utils
│ │ ├── Box.java
│ │ ├── Item.java
│ │ ├── Parcel.java
│ │ └── package-info.java
└── tests
│ ├── InvocationResult.java
│ ├── OutputAssertions.java
│ ├── StreamTasksTests.java
│ ├── TaskTests.java
│ ├── TestInputGenerator.java
│ ├── TestInvokerGenerator.java
│ ├── TestVerdict.java
│ ├── TestsPool.java
│ ├── TestsRunner.java
│ ├── inputs
│ ├── ConstantValueProvider.java
│ ├── ConsumerGenerator.java
│ ├── ConsumerMode.java
│ ├── SequenceWithStatistics.java
│ ├── SupplierMode.java
│ └── package-info.java
│ ├── package-info.java
│ ├── presets
│ ├── Boxes.java
│ ├── DataMappingPreset.java
│ ├── DataPreset.java
│ ├── HexStrNumbers.java
│ ├── IntNumbers.java
│ ├── IntStrNumbers.java
│ ├── Items.java
│ ├── Names.java
│ └── package-info.java
│ └── utils
│ ├── F3.java
│ ├── F4.java
│ ├── F5.java
│ ├── Pair.java
│ ├── Test.java
│ ├── TestInputCollection.java
│ ├── TestInputConstant.java
│ ├── TestInputConsumer.java
│ ├── TestInputFunction.java
│ ├── TestInputPredicate.java
│ ├── TestInputSupplier.java
│ ├── TestResult.java
│ └── package-info.java
├── test.cmd
└── test.sh
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: bug
6 | assignees: Shemplo
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **Expected behavior**
14 | A clear and concise description of what you expected to happen.
15 |
16 | **Screenshots**
17 | If applicable, add screenshots to help explain your problem.
18 |
19 | **Desktop (please complete the following information):**
20 | - OS: [e.g. Windows, iOS]
21 | - Other useful information aboul your platform
22 |
23 | **Additional context**
24 | Add any other context about the problem here.
25 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: enhancement
6 | assignees: Shemplo
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/workflows/validation.yml:
--------------------------------------------------------------------------------
1 | name: Solution validation
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | jobs:
10 | build:
11 | runs-on: ubuntu-latest
12 |
13 | steps:
14 | - uses: actions/checkout@v2
15 | - name: Set up JDK 11
16 | uses: actions/setup-java@v1
17 | with:
18 | java-version: 11
19 | - name: Run tests
20 | run: javac -sourcepath src -d bin src/tasks/StreamTasksMain.java && java -ea -cp bin tasks.StreamTasksMain validate
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings/
2 | /build/
3 | /bin/
4 |
5 | .classpath
6 | .project
--------------------------------------------------------------------------------
/code_of_conduct.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, sex characteristics, gender identity and expression,
9 | level of experience, education, socio-economic status, nationality, personal
10 | appearance, race, religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at shemplo@outlook.com. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 | For answers to common questions about this code of conduct, see
76 | https://www.contributor-covenant.org/faq
77 |
--------------------------------------------------------------------------------
/contributing.md:
--------------------------------------------------------------------------------
1 | **Everybody is welcome to contribute**
2 |
3 | Key statements:
4 | * Applying changes should be related to the main goal of this repository - education
5 | * All applying changes should be accepted by code owners
6 | * Be polite, do not swear
7 |
--------------------------------------------------------------------------------
/license:
--------------------------------------------------------------------------------
1 | Eclipse Public License - v 2.0
2 |
3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION
5 | OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
6 |
7 | 1. DEFINITIONS
8 |
9 | "Contribution" means:
10 |
11 | a) in the case of the initial Contributor, the initial content
12 | Distributed under this Agreement, and
13 |
14 | b) in the case of each subsequent Contributor:
15 | i) changes to the Program, and
16 | ii) additions to the Program;
17 | where such changes and/or additions to the Program originate from
18 | and are Distributed by that particular Contributor. A Contribution
19 | "originates" from a Contributor if it was added to the Program by
20 | such Contributor itself or anyone acting on such Contributor's behalf.
21 | Contributions do not include changes or additions to the Program that
22 | are not Modified Works.
23 |
24 | "Contributor" means any person or entity that Distributes the Program.
25 |
26 | "Licensed Patents" mean patent claims licensable by a Contributor which
27 | are necessarily infringed by the use or sale of its Contribution alone
28 | or when combined with the Program.
29 |
30 | "Program" means the Contributions Distributed in accordance with this
31 | Agreement.
32 |
33 | "Recipient" means anyone who receives the Program under this Agreement
34 | or any Secondary License (as applicable), including Contributors.
35 |
36 | "Derivative Works" shall mean any work, whether in Source Code or other
37 | form, that is based on (or derived from) the Program and for which the
38 | editorial revisions, annotations, elaborations, or other modifications
39 | represent, as a whole, an original work of authorship.
40 |
41 | "Modified Works" shall mean any work in Source Code or other form that
42 | results from an addition to, deletion from, or modification of the
43 | contents of the Program, including, for purposes of clarity any new file
44 | in Source Code form that contains any contents of the Program. Modified
45 | Works shall not include works that contain only declarations,
46 | interfaces, types, classes, structures, or files of the Program solely
47 | in each case in order to link to, bind by name, or subclass the Program
48 | or Modified Works thereof.
49 |
50 | "Distribute" means the acts of a) distributing or b) making available
51 | in any manner that enables the transfer of a copy.
52 |
53 | "Source Code" means the form of a Program preferred for making
54 | modifications, including but not limited to software source code,
55 | documentation source, and configuration files.
56 |
57 | "Secondary License" means either the GNU General Public License,
58 | Version 2.0, or any later versions of that license, including any
59 | exceptions or additional permissions as identified by the initial
60 | Contributor.
61 |
62 | 2. GRANT OF RIGHTS
63 |
64 | a) Subject to the terms of this Agreement, each Contributor hereby
65 | grants Recipient a non-exclusive, worldwide, royalty-free copyright
66 | license to reproduce, prepare Derivative Works of, publicly display,
67 | publicly perform, Distribute and sublicense the Contribution of such
68 | Contributor, if any, and such Derivative Works.
69 |
70 | b) Subject to the terms of this Agreement, each Contributor hereby
71 | grants Recipient a non-exclusive, worldwide, royalty-free patent
72 | license under Licensed Patents to make, use, sell, offer to sell,
73 | import and otherwise transfer the Contribution of such Contributor,
74 | if any, in Source Code or other form. This patent license shall
75 | apply to the combination of the Contribution and the Program if, at
76 | the time the Contribution is added by the Contributor, such addition
77 | of the Contribution causes such combination to be covered by the
78 | Licensed Patents. The patent license shall not apply to any other
79 | combinations which include the Contribution. No hardware per se is
80 | licensed hereunder.
81 |
82 | c) Recipient understands that although each Contributor grants the
83 | licenses to its Contributions set forth herein, no assurances are
84 | provided by any Contributor that the Program does not infringe the
85 | patent or other intellectual property rights of any other entity.
86 | Each Contributor disclaims any liability to Recipient for claims
87 | brought by any other entity based on infringement of intellectual
88 | property rights or otherwise. As a condition to exercising the
89 | rights and licenses granted hereunder, each Recipient hereby
90 | assumes sole responsibility to secure any other intellectual
91 | property rights needed, if any. For example, if a third party
92 | patent license is required to allow Recipient to Distribute the
93 | Program, it is Recipient's responsibility to acquire that license
94 | before distributing the Program.
95 |
96 | d) Each Contributor represents that to its knowledge it has
97 | sufficient copyright rights in its Contribution, if any, to grant
98 | the copyright license set forth in this Agreement.
99 |
100 | e) Notwithstanding the terms of any Secondary License, no
101 | Contributor makes additional grants to any Recipient (other than
102 | those set forth in this Agreement) as a result of such Recipient's
103 | receipt of the Program under the terms of a Secondary License
104 | (if permitted under the terms of Section 3).
105 |
106 | 3. REQUIREMENTS
107 |
108 | 3.1 If a Contributor Distributes the Program in any form, then:
109 |
110 | a) the Program must also be made available as Source Code, in
111 | accordance with section 3.2, and the Contributor must accompany
112 | the Program with a statement that the Source Code for the Program
113 | is available under this Agreement, and informs Recipients how to
114 | obtain it in a reasonable manner on or through a medium customarily
115 | used for software exchange; and
116 |
117 | b) the Contributor may Distribute the Program under a license
118 | different than this Agreement, provided that such license:
119 | i) effectively disclaims on behalf of all other Contributors all
120 | warranties and conditions, express and implied, including
121 | warranties or conditions of title and non-infringement, and
122 | implied warranties or conditions of merchantability and fitness
123 | for a particular purpose;
124 |
125 | ii) effectively excludes on behalf of all other Contributors all
126 | liability for damages, including direct, indirect, special,
127 | incidental and consequential damages, such as lost profits;
128 |
129 | iii) does not attempt to limit or alter the recipients' rights
130 | in the Source Code under section 3.2; and
131 |
132 | iv) requires any subsequent distribution of the Program by any
133 | party to be under a license that satisfies the requirements
134 | of this section 3.
135 |
136 | 3.2 When the Program is Distributed as Source Code:
137 |
138 | a) it must be made available under this Agreement, or if the
139 | Program (i) is combined with other material in a separate file or
140 | files made available under a Secondary License, and (ii) the initial
141 | Contributor attached to the Source Code the notice described in
142 | Exhibit A of this Agreement, then the Program may be made available
143 | under the terms of such Secondary Licenses, and
144 |
145 | b) a copy of this Agreement must be included with each copy of
146 | the Program.
147 |
148 | 3.3 Contributors may not remove or alter any copyright, patent,
149 | trademark, attribution notices, disclaimers of warranty, or limitations
150 | of liability ("notices") contained within the Program from any copy of
151 | the Program which they Distribute, provided that Contributors may add
152 | their own appropriate notices.
153 |
154 | 4. COMMERCIAL DISTRIBUTION
155 |
156 | Commercial distributors of software may accept certain responsibilities
157 | with respect to end users, business partners and the like. While this
158 | license is intended to facilitate the commercial use of the Program,
159 | the Contributor who includes the Program in a commercial product
160 | offering should do so in a manner which does not create potential
161 | liability for other Contributors. Therefore, if a Contributor includes
162 | the Program in a commercial product offering, such Contributor
163 | ("Commercial Contributor") hereby agrees to defend and indemnify every
164 | other Contributor ("Indemnified Contributor") against any losses,
165 | damages and costs (collectively "Losses") arising from claims, lawsuits
166 | and other legal actions brought by a third party against the Indemnified
167 | Contributor to the extent caused by the acts or omissions of such
168 | Commercial Contributor in connection with its distribution of the Program
169 | in a commercial product offering. The obligations in this section do not
170 | apply to any claims or Losses relating to any actual or alleged
171 | intellectual property infringement. In order to qualify, an Indemnified
172 | Contributor must: a) promptly notify the Commercial Contributor in
173 | writing of such claim, and b) allow the Commercial Contributor to control,
174 | and cooperate with the Commercial Contributor in, the defense and any
175 | related settlement negotiations. The Indemnified Contributor may
176 | participate in any such claim at its own expense.
177 |
178 | For example, a Contributor might include the Program in a commercial
179 | product offering, Product X. That Contributor is then a Commercial
180 | Contributor. If that Commercial Contributor then makes performance
181 | claims, or offers warranties related to Product X, those performance
182 | claims and warranties are such Commercial Contributor's responsibility
183 | alone. Under this section, the Commercial Contributor would have to
184 | defend claims against the other Contributors related to those performance
185 | claims and warranties, and if a court requires any other Contributor to
186 | pay any damages as a result, the Commercial Contributor must pay
187 | those damages.
188 |
189 | 5. NO WARRANTY
190 |
191 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT
192 | PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS"
193 | BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
194 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF
195 | TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
196 | PURPOSE. Each Recipient is solely responsible for determining the
197 | appropriateness of using and distributing the Program and assumes all
198 | risks associated with its exercise of rights under this Agreement,
199 | including but not limited to the risks and costs of program errors,
200 | compliance with applicable laws, damage to or loss of data, programs
201 | or equipment, and unavailability or interruption of operations.
202 |
203 | 6. DISCLAIMER OF LIABILITY
204 |
205 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT
206 | PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS
207 | SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
208 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST
209 | PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
210 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
211 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
212 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
213 | POSSIBILITY OF SUCH DAMAGES.
214 |
215 | 7. GENERAL
216 |
217 | If any provision of this Agreement is invalid or unenforceable under
218 | applicable law, it shall not affect the validity or enforceability of
219 | the remainder of the terms of this Agreement, and without further
220 | action by the parties hereto, such provision shall be reformed to the
221 | minimum extent necessary to make such provision valid and enforceable.
222 |
223 | If Recipient institutes patent litigation against any entity
224 | (including a cross-claim or counterclaim in a lawsuit) alleging that the
225 | Program itself (excluding combinations of the Program with other software
226 | or hardware) infringes such Recipient's patent(s), then such Recipient's
227 | rights granted under Section 2(b) shall terminate as of the date such
228 | litigation is filed.
229 |
230 | All Recipient's rights under this Agreement shall terminate if it
231 | fails to comply with any of the material terms or conditions of this
232 | Agreement and does not cure such failure in a reasonable period of
233 | time after becoming aware of such noncompliance. If all Recipient's
234 | rights under this Agreement terminate, Recipient agrees to cease use
235 | and distribution of the Program as soon as reasonably practicable.
236 | However, Recipient's obligations under this Agreement and any licenses
237 | granted by Recipient relating to the Program shall continue and survive.
238 |
239 | Everyone is permitted to copy and distribute copies of this Agreement,
240 | but in order to avoid inconsistency the Agreement is copyrighted and
241 | may only be modified in the following manner. The Agreement Steward
242 | reserves the right to publish new versions (including revisions) of
243 | this Agreement from time to time. No one other than the Agreement
244 | Steward has the right to modify this Agreement. The Eclipse Foundation
245 | is the initial Agreement Steward. The Eclipse Foundation may assign the
246 | responsibility to serve as the Agreement Steward to a suitable separate
247 | entity. Each new version of the Agreement will be given a distinguishing
248 | version number. The Program (including Contributions) may always be
249 | Distributed subject to the version of the Agreement under which it was
250 | received. In addition, after a new version of the Agreement is published,
251 | Contributor may elect to Distribute the Program (including its
252 | Contributions) under the new version.
253 |
254 | Except as expressly stated in Sections 2(a) and 2(b) above, Recipient
255 | receives no rights or licenses to the intellectual property of any
256 | Contributor under this Agreement, whether expressly, by implication,
257 | estoppel or otherwise. All rights in the Program not expressly granted
258 | under this Agreement are reserved. Nothing in this Agreement is intended
259 | to be enforceable by any entity that is not a Contributor or Recipient.
260 | No third-party beneficiary rights are created under this Agreement.
261 |
262 | Exhibit A - Form of Secondary Licenses Notice
263 |
264 | "This Source Code may also be made available under the following
265 | Secondary Licenses when the conditions for such availability set forth
266 | in the Eclipse Public License, v. 2.0 are satisfied: {name license(s),
267 | version(s), and exceptions or additional permissions here}."
268 |
269 | Simply including a copy of this Agreement, including this Exhibit A
270 | is not sufficient to license the Source Code under Secondary Licenses.
271 |
272 | If it is not possible or desirable to put the notice in a particular
273 | file, then You may include the notice in a location (such as a LICENSE
274 | file in a relevant directory) where a recipient would be likely to
275 | look for such a notice.
276 |
277 | You may add additional accurate notices of copyright ownership.
278 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | ## Java Streams Etudes
2 |
3 | [](https://github.com/Shemplo/Java-Streams-Etudes/blob/master/)
4 | [](https://github.com/Shemplo/Java-Streams-Etudes/blob/master/LICENSE)
5 | [](https://github.com/Shemplo/Java-Streams-Etudes/stargazers)
6 |
7 | Do you know how to use Java Stream API?
8 |
9 | * If **NO** but you are interested in it then you can use this project as a roadmap of things to learn
10 | * If **YES** then you can have a practice here to check your skills
11 |
12 | Apply you knowledge of _streams_, _lambdas_, _method handlers_ and other functional Java things.
13 |
14 | There are also some tasks that will make think even experienced programmers. Try to solve them all.
15 |
16 | > **25** tasks are available now
17 |
18 | ### What to do
19 |
20 | > Clone repository → implement methods → run tests → fix mistakes
21 |
22 | Methods to implement are placed in class `tasks.StreamTasks` in `src` folder.
23 | Initially they are stubbed with standard `UnsupportedOperationException` (and tests know about that).
24 | You need to replace such lines with solution consists of stream flow or something related to it
25 | (description and hints for each task are provided).
26 |
27 | It's **prohibited** to use standard cycles (`for`, `while`, `do-while`, recursion) at all and
28 | conditions (`if`, `if-else`, ...) out of stream operations. The main goal of this project is to
29 | teach you how to use functional approach in practice. One **possible exclusion** from pure functional
30 | paradigm is declaring and usage of local variables.
31 |
32 | ### Running tests
33 |
34 | Due to this project is orientated to beginners **no dependent libraries** are required.
35 | If you know how to add dependencies and really know for what then you can do it manually
36 | (but, be pointed, all tasks can be solved using only standard Java library).
37 |
38 | > Main requirement is **JVM of 11 version (or higher)**
39 |
40 | Run main method from `tasks.StreamTasksMain` class in your preferred IDE or run a script file `test.(cmd|sh)`.
41 |
42 | Flag `-ea` is required to enable `assert` key word for tests checker. Otherwise all tests will not be run properly.
43 |
44 | #### Check program and verdicts
45 |
46 | Check program will run tests for all tasks and then write verdict for each of tasks.
47 |
48 | Possible verdicts:
49 |
50 | * **accepted** - you solved this task
51 | * **wrong answer** - you almost solved this task (code compiles but logic is wrong)
52 | * **not implemented** - you didn't start to implement this task
53 | * **missed NULL check** - you forgot to check something for `null` reference
54 | * **runtime exception** - something goes extremely bad
55 |
56 | Until you solved all tasks with verdict `accepted` the program will finish with non-zero exit code.
57 |
58 | ##### Examples
59 |
60 | You can run check program with argument `example`.
61 | For that add `example` word to passing arguments in IDE or to the end of running scripts:
62 | `test.cmd example` (for Windows) and `./test.sh example` (for Unix)
63 |
64 | There are some implemented tasks in class `tasks.StreamTasksExample`, so they would be tested in `example` mode.
65 |
66 | > Not guaranteed that implementation is correct or in optimal way ;)
67 |
68 | #### Solutions
69 |
70 | In case you don't know how to solve some task than you can see solution in `tasks.solution.StreamTasksSolution`
71 | class in `src` folder. This class is used as reference solution to check answers, so you can copy-paste code to the
72 | task and this task will be accepted.
73 |
74 | Try to avoid any interaction with solutions until you completed all tasks :)
75 |
76 | #### Challenges
77 |
78 | You can also use tasks from this project for different challenges.
79 | For example, you can check how many tasks you can do in **limited time**
80 | or how much time you will need to **solve them all**, etc.
81 |
82 | ##### Good luck, have fun progress!
83 |
84 |
85 |
86 | Version 0.1.0 is available [here](https://github.com/Shemplo/Java-Streams-Etudes/tree/7206e9138a3c2ae0347d983696dfaf56002485a0)
87 |
88 | * The first draft of this project
89 | * It has different 60 tasks
90 |
91 |
92 |
93 | Future plans:
94 |
95 | * `[ ]` Rise number of tasks to 100
96 | * `[~]` Add more input generators to test engine
97 | * `[✓]` For `Map , ?>` type
98 | * `[✓]` For `List >` type (and any enclosure level)
99 | * `[ ]` Else
100 | * `[ ]` Add more input variations to test engine (f.e. collection as extension of another parameter that is collection too)
101 | * `[~]` Сome up with some new task ideas
102 | * `[ ]` Split tasks to several classes by topics
103 |
--------------------------------------------------------------------------------
/src/tasks/StreamTasks.java:
--------------------------------------------------------------------------------
1 | package tasks;
2 | import java.util.Collection;
3 | import java.util.List;
4 | import java.util.Set;
5 | import java.util.function.Function;
6 | import java.util.function.IntFunction;
7 | import java.util.function.IntPredicate;
8 | import java.util.function.IntUnaryOperator;
9 | import java.util.function.Predicate;
10 | import java.util.stream.IntStream;
11 | import java.util.stream.Stream;
12 |
13 | import tests.StreamTasksTests;
14 |
15 | public class StreamTasks extends StreamTasksTests {
16 |
17 | // INTRODUCTION //
18 |
19 | /**
20 | * @return List of given constants [a, b + c - a, c]
21 | * @see List#of()
22 | * @lines 1
23 | */
24 | public List task1 (int a, int b, int c) {
25 | throw new UnsupportedOperationException ("Implement method instead of this line");
26 | }
27 |
28 | /**
29 | * @return Set of given constants [a, b, c]
30 | * @see Set#of()
31 | * @lines 1
32 | */
33 | public Set task2 (int a, int b, int c) {
34 | throw new UnsupportedOperationException ("Implement method instead of this line");
35 | }
36 |
37 | /**
38 | * @return Set of values from given list
39 | * @see Set#copyOf(Collection)
40 | * @lines 1
41 | */
42 | public Set task3 (List values) {
43 | throw new UnsupportedOperationException ("Implement method instead of this line");
44 | }
45 |
46 | /**
47 | * @return Stream of values from given list
48 | * @see Collection#stream()
49 | * @lines 1
50 | */
51 | public Stream task4 (List values) {
52 | throw new UnsupportedOperationException ("Implement method instead of this line");
53 | }
54 |
55 | /**
56 | * @return Stream of values from given set
57 | * @see Collection#stream()
58 | * @lines 1
59 | */
60 | public Stream task5 (Set values) {
61 | throw new UnsupportedOperationException ("Implement method instead of this line");
62 | }
63 |
64 | /**
65 | * @return Number of elements in the stream
66 | * @see Stream#count()
67 | * @lines 1
68 | */
69 | public int task6 (Stream values) {
70 | throw new UnsupportedOperationException ("Implement method instead of this line");
71 | }
72 |
73 | /**
74 | * @return Given stream that is limited by size
75 | * @see Stream#limit(long)
76 | * @lines 1
77 | */
78 | public Stream task7 (Stream values, int limit) {
79 | throw new UnsupportedOperationException ("Implement method instead of this line");
80 | }
81 |
82 | /**
83 | * @return Stream that contains last `n` elements of sequence with general `size` size from given stream
84 | * @example input: [A, B, C, D, E, F, G, H, I, ...], 3, 7 -> output: [E, F, G]
85 | * @see Stream#skip(long)
86 | * @lines 1
87 | */
88 | public Stream task8 (Stream values, int n, int size) {
89 | throw new UnsupportedOperationException ("Implement method instead of this line");
90 | }
91 |
92 | /**
93 | * @return Stream that contains all values from given streams
94 | * @see Stream#concat(Stream, Stream)
95 | * @lines 1
96 | */
97 | public Stream task9 (Stream values1, Stream values2) {
98 | throw new UnsupportedOperationException ("Implement method instead of this line");
99 | }
100 |
101 | /**
102 | * @return Stream that contains all values from given streams
103 | * @lines 1
104 | */
105 | public Stream task10 (Stream values1, Stream values2, Stream values3) {
106 | throw new UnsupportedOperationException ("Implement method instead of this line");
107 | }
108 |
109 | // FIRST FLOWS //
110 |
111 | /**
112 | * @return Stream that contains only values accepted by predicate (condition)
113 | * @see Stream#filter(Predicate)
114 | * @lines 1
115 | */
116 | public Stream task11 (Stream numbers, Predicate condition) {
117 | throw new UnsupportedOperationException ("Implement method instead of this line");
118 | }
119 |
120 | /**
121 | * @return Predicate that will accept numbers in segment [from, to]
122 | * @lines 1
123 | */
124 | public Predicate task12 (int from, int to) {
125 | throw new UnsupportedOperationException ("Implement method instead of this line");
126 | }
127 |
128 | /**
129 | * @return Predicate that combine two: `positive` as well and negation of `negative`
130 | * @see Predicate#and(Predicate)
131 | * @see Predicate#negate()
132 | * @lines 1
133 | */
134 | public Predicate task13 (Predicate positive, Predicate negative) {
135 | throw new UnsupportedOperationException ("Implement method instead of this line");
136 | }
137 |
138 | /**
139 | * @return Stream that contains converted values by `converter`
140 | * @see Stream#map(Function)
141 | * @lines 1
142 | */
143 | public Stream task14 (Stream numbers, Function converter) {
144 | throw new UnsupportedOperationException ("Implement method instead of this line");
145 | }
146 |
147 | /**
148 | * @return Function that convert each element (x) to expression: k * x + b
149 | * @lines 1
150 | */
151 | public Function task15 (int k, int b) {
152 | throw new UnsupportedOperationException ("Implement method instead of this line");
153 | }
154 |
155 | /**
156 | * @return Function that convert each element (x) to expression: g (f (x) + 1)
157 | * @see Function#compose(Function)
158 | * @lines 1
159 | */
160 | public Function task16 (Function f, Function g) {
161 | throw new UnsupportedOperationException ("Implement method instead of this line");
162 | }
163 |
164 | /**
165 | * @return Stream of parsed integer numbers from its' string representation
166 | * @see Integer#parseInt(String)
167 | * @lines 1
168 | */
169 | public Stream task17 (Stream numbers) {
170 | throw new UnsupportedOperationException ("Implement method instead of this line");
171 | }
172 |
173 | /**
174 | * @return Stream of parsed hex integer numbers from its' string representation
175 | * @hint Hex numbers starts from '0x', don't forget to remove this prefix
176 | * @see Integer#parseInt(String, int)
177 | * @lines 1
178 | */
179 | public Stream task18 (Stream numbers) {
180 | throw new UnsupportedOperationException ("Implement method instead of this line");
181 | }
182 |
183 | /**
184 | * @return Sorted stream (ascending) of unique numbers
185 | * @see Stream#distinct()
186 | * @see Stream#sorted()
187 | * @lines 1
188 | */
189 | public Stream task19 (Stream numbers) {
190 | throw new UnsupportedOperationException ("Implement method instead of this line");
191 | }
192 |
193 | /**
194 | * @return Sorted stream (ascending) of numbers whose square doesn't exceed `limit` (less or equal)
195 | * @lines 1
196 | */
197 | public Stream task20 (Stream numbers, int limit) {
198 | throw new UnsupportedOperationException ("Implement method instead of this line");
199 | }
200 |
201 | // INT STREAMS //
202 |
203 | /**
204 | * @return Stream of integers from segment [0, `to`]
205 | * @see IntStream#range(int, int)
206 | * @lines 1
207 | */
208 | public IntStream task21 (int to) {
209 | throw new UnsupportedOperationException ("Implement method instead of this line");
210 | }
211 |
212 | /**
213 | * @return Stream of integer numbers from given list with added index: `numbers` [i] + i
214 | * @see IntStream#map(IntUnaryOperator)
215 | * @lines 1
216 | */
217 | public IntStream task22 (List numbers) {
218 | throw new UnsupportedOperationException ("Implement method instead of this line");
219 | }
220 |
221 | /**
222 | * @return Stream of integer numbers on `offset` positions (0 position included)
223 | * @see IntStream#iterate(int, IntPredicate, IntUnaryOperator)
224 | * @example [0, 1, 2, 3, 4, 5, 6, 7, 8], 3 -> [0, 3, 6]
225 | * @lines 1
226 | */
227 | public IntStream task23 (List numbers, int offset) {
228 | throw new UnsupportedOperationException ("Implement method instead of this line");
229 | }
230 |
231 | /**
232 | * @return Infinite stream of integers (cycled on given `numbers`)
233 | * @see IntStream#iterate(int, IntUnaryOperator)
234 | * @see IntStream#mapToObj(IntFunction)
235 | * @see IntStream#boxed()
236 | * @example [0, 1, 2, 3, 4] -> [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, ...]
237 | * @lines 1
238 | */
239 | public Stream task24 (List numbers) {
240 | throw new UnsupportedOperationException ("Implement method instead of this line");
241 | }
242 |
243 | /**
244 | * @return Stream of summed values on the same positions from given lists of numbers
245 | * (only positions that there are in both lists)
246 | * @example [0, 1, 2, 3, 4], [10, 12, 14] -> [10, 13, 16]
247 | * @lines 1
248 | */
249 | public IntStream task25 (List numbers1, List numbers2) {
250 | throw new UnsupportedOperationException ("Implement method instead of this line");
251 | }
252 |
253 | }
254 |
--------------------------------------------------------------------------------
/src/tasks/StreamTasksExample.java:
--------------------------------------------------------------------------------
1 | package tasks;
2 |
3 | public class StreamTasksExample extends StreamTasks {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/src/tasks/StreamTasksMain.java:
--------------------------------------------------------------------------------
1 | package tasks;
2 | import java.util.Locale;
3 |
4 | import tasks.solution.StreamTasksSolution;
5 | import tests.StreamTasksTests;
6 | import tests.TestsRunner;
7 |
8 | public class StreamTasksMain {
9 |
10 | /*******************************/
11 | /* DO NOT TOUCH METHODS BELLOW */
12 | /*******************************/
13 |
14 | public static void main (String ... args) {
15 | Locale.setDefault (Locale.ENGLISH);
16 |
17 | try {
18 | assert false;
19 | System.out.println ("JVM flag `-ea` is not enabled");
20 | System.exit (1);
21 | } catch (AssertionError ae) {
22 | // This is necessary to check whether -ea flag is set
23 | }
24 |
25 | StreamTasksTests implementation = null;
26 | if (args.length > 0 && "validate".equals (args [0])) {
27 | implementation = new StreamTasksSolution ();
28 | } else if (args.length > 0 && "example".equals (args [0])) {
29 | implementation = new StreamTasksExample ();
30 | } else {
31 | implementation = new StreamTasks ();
32 | }
33 |
34 | int exitCode = new TestsRunner (implementation, new StreamTasksSolution ()).test ();
35 | System.exit (exitCode);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/tasks/package-info.java:
--------------------------------------------------------------------------------
1 | package tasks;
2 |
--------------------------------------------------------------------------------
/src/tasks/solution/StreamTasksSolution.java:
--------------------------------------------------------------------------------
1 | package tasks.solution;
2 | import java.util.List;
3 | import java.util.Set;
4 | import java.util.function.Function;
5 | import java.util.function.Predicate;
6 | import java.util.stream.IntStream;
7 | import java.util.stream.Stream;
8 |
9 | import tests.StreamTasksTests;
10 |
11 | public class StreamTasksSolution extends StreamTasksTests {
12 |
13 | @Override
14 | public List task1 (int a, int b, int c) {
15 | return List.of (a, b + c - a, c);
16 | }
17 |
18 | @Override
19 | public Set task2 (int a, int b, int c) {
20 | return Set.copyOf (List.of (a, b, c));
21 | }
22 |
23 | @Override
24 | public Set task3 (List values) {
25 | return Set.copyOf (values);
26 | }
27 |
28 | @Override
29 | public Stream task4 (List values) {
30 | return values.stream ();
31 | }
32 |
33 | @Override
34 | public Stream task5 (Set values) {
35 | return values.stream ();
36 | }
37 |
38 | @Override
39 | public int task6 (Stream values) {
40 | return (int) values.count ();
41 | }
42 |
43 | @Override
44 | public Stream task7 (Stream values, int limit) {
45 | return values.limit (limit);
46 | }
47 |
48 | @Override
49 | public Stream task8 (Stream values, int n, int size) {
50 | return values.skip (size - n).limit (n);
51 | }
52 |
53 | @Override
54 | public Stream task9 (Stream values1, Stream values2) {
55 | return Stream.concat (values1, values2);
56 | }
57 |
58 | @Override
59 | public Stream task10 (Stream values1, Stream values2, Stream values3) {
60 | return Stream.concat (Stream.concat (values1, values2), values3);
61 | }
62 |
63 | @Override
64 | public Stream task11 (Stream numbers, Predicate condition) {
65 | return numbers.filter (condition);
66 | }
67 |
68 | @Override
69 | public Predicate task12 (int from, int to) {
70 | return i -> i >= from && i <= to;
71 | }
72 |
73 | @Override
74 | public Predicate task13 (Predicate positive, Predicate negative) {
75 | return positive.and (negative.negate ());
76 | }
77 |
78 | @Override
79 | public Stream task14 (Stream numbers, Function converter) {
80 | return numbers.map (converter);
81 | }
82 |
83 | @Override
84 | public Function task15 (int k, int b) {
85 | return x -> k * x + b;
86 | }
87 |
88 | @Override
89 | public Function task16 (Function f, Function g) {
90 | return g.compose (x -> f.apply (x) + 1);
91 | }
92 |
93 | @Override
94 | public Stream task17 (Stream numbers) {
95 | return numbers.map (Integer::parseInt);
96 | }
97 |
98 | @Override
99 | public Stream task18 (Stream numbers) {
100 | return numbers.map (num -> num.substring (2)).map (num -> Integer.parseInt (num, 16));
101 | }
102 |
103 | @Override
104 | public Stream task19 (Stream numbers) {
105 | return numbers.distinct ().sorted ();
106 | }
107 |
108 | @Override
109 | public Stream task20 (Stream numbers, int limit) {
110 | return numbers.filter (x -> x * x <= limit).sorted ();
111 | }
112 |
113 | @Override
114 | public IntStream task21 (int to) {
115 | return IntStream.range (0, to + 1);
116 | }
117 |
118 | @Override
119 | public IntStream task22 (List numbers) {
120 | return IntStream.range (0, numbers.size ()).map (i -> numbers.get (i) + i);
121 | }
122 |
123 | @Override
124 | public IntStream task23 (List numbers, int offset) {
125 | return IntStream.iterate (0, i -> i < numbers.size (), i -> i + offset).map (numbers::get);
126 | }
127 |
128 | @Override
129 | public Stream task24 (List numbers) {
130 | return IntStream.iterate (0, i -> (i + 1) % numbers.size ()).mapToObj (numbers::get);
131 | }
132 |
133 | @Override
134 | public IntStream task25 (List numbers1, List numbers2) {
135 | return IntStream.range (0, Math.min (numbers1.size (), numbers2.size ()))
136 | . map (i -> numbers1.get (i) + numbers2.get (i));
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/tasks/solution/package-info.java:
--------------------------------------------------------------------------------
1 | package tasks.solution;
2 |
--------------------------------------------------------------------------------
/src/tasks/utils/Box.java:
--------------------------------------------------------------------------------
1 | package tasks.utils;
2 |
3 | import java.util.Collections;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 | import java.util.stream.Collectors;
7 |
8 | public class Box extends Item {
9 |
10 | protected final List items = new LinkedList <> ();
11 |
12 | public Box addItem (Item item) {
13 | items.add (item);
14 | return this;
15 | }
16 |
17 | public Box addItems (List items) {
18 | this.items.addAll (items);
19 | return this;
20 | }
21 |
22 | public List getItems () {
23 | return Collections.unmodifiableList (items);
24 | }
25 |
26 | public int getItemsNumber () {
27 | return items.size ();
28 | }
29 |
30 | @Override
31 | public void setWeight (double weight) {
32 | throw new UnsupportedOperationException ();
33 | }
34 |
35 | @Override
36 | public double getWeight () {
37 | return items.stream ().mapToDouble (Item::getWeight).sum ();
38 | }
39 |
40 | @Override
41 | public void setCategory (char category) {
42 | throw new UnsupportedOperationException ();
43 | }
44 |
45 | @Override
46 | public char getCategory () {
47 | return '#';
48 | }
49 |
50 | @Override
51 | public String toString () {
52 | return items.stream ().map (Item::toString).collect (Collectors.joining (", ", "Box[", "]"));
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/tasks/utils/Item.java:
--------------------------------------------------------------------------------
1 | package tasks.utils;
2 |
3 |
4 | public class Item {
5 |
6 | protected double weight;
7 |
8 | protected String description, barcode;
9 |
10 | protected char category = 'A';
11 |
12 | public void setWeight (double weight) {
13 | this.weight = weight;
14 | }
15 |
16 | public double getWeight () {
17 | return weight;
18 | }
19 |
20 | public void setDescription (String description) {
21 | this.description = description;
22 | }
23 |
24 | public String getDescription () {
25 | return description;
26 | }
27 |
28 | public void setBarcode (String barcode) {
29 | this.barcode = barcode;
30 | }
31 |
32 | public String getBarcode () {
33 | return barcode;
34 | }
35 |
36 | public void setCategory (char category) {
37 | this.category = category;
38 | }
39 |
40 | public char getCategory () {
41 | return category;
42 | }
43 |
44 | @Override
45 | public String toString () {
46 | return String.format ("Item[%c; %s; %.3f; %s]", getCategory (), getBarcode (), getWeight (), getDescription ());
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/tasks/utils/Parcel.java:
--------------------------------------------------------------------------------
1 | package tasks.utils;
2 |
3 | import java.util.stream.Collectors;
4 |
5 | public class Parcel extends Box {
6 |
7 | private String from, to;
8 |
9 | public void setFrom (String from) {
10 | this.from = from;
11 | }
12 |
13 | public String getFrom () {
14 | return from;
15 | }
16 |
17 | public void setTo (String to) {
18 | this.to = to;
19 | }
20 |
21 | public String getTo () {
22 | return to;
23 | }
24 |
25 | @Override
26 | public String toString () {
27 | final var prefix = String.format ("Parcel(%s -> %s)[", getFrom (), getTo ());
28 | return items.stream ().map (Item::toString).collect (Collectors.joining (", ", prefix, "]"));
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/tasks/utils/package-info.java:
--------------------------------------------------------------------------------
1 | package tasks.utils;
2 |
--------------------------------------------------------------------------------
/src/tests/InvocationResult.java:
--------------------------------------------------------------------------------
1 | package tests;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | public class InvocationResult {
8 |
9 | public final Object result;
10 | private long runtime;
11 |
12 | private final List