├── .idea
├── $CACHE_FILE$
├── $PRODUCT_WORKSPACE_FILE$
├── .gitignore
├── libraries
│ └── src.xml
├── misc.xml
├── modules.xml
├── stream-api-exercises-part2.iml
└── vcs.xml
├── LICENSE
├── README.md
└── src
└── com
└── example
└── animals
├── domain
├── Animal.java
├── Cat.java
├── Fish.java
├── Pet.java
└── Spider.java
└── exercises
├── Exercise1.java
├── Exercise2.java
├── Exercise3.java
├── Exercise4.java
├── Exercise5.java
├── Exercise6.java
├── Exercise7.java
└── Exercise8.java
/.idea/$CACHE_FILE$:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/$PRODUCT_WORKSPACE_FILE$:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | 1.8
8 |
9 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/.idea/libraries/src.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/stream-api-exercises-part2.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Binnur KURT
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Stream API Exercises : Part II
2 | Stream API Exercises : Part II
3 |
4 | `List animals= List.of(
5 | new Spider(),
6 | new Cat(),
7 | new Fish("Free Willy"),
8 | new Cat("Tekir"),
9 | new Fish("Jaws"),
10 | new Spider()
11 | );`
12 |
13 | **Q.1)** Take a list of wild animals
14 |
15 | **Q.2)** Take a list of pets
16 |
17 | **Q.3)** Find the animal with the highest number of legs
18 |
19 | **Q.4)** Take a list of 100 random animals
20 |
21 | **Q.5)** Find the total number of legs
22 |
23 | **Q.6)** Group the animals by their number of legs
24 |
25 | **Q.7)** Count the number of animals in each specie
26 |
27 | **Q.8)** Count the number of species
28 |
29 | **For Part I** : https://github.com/deepcloudlabs/stream-api-exercises-part1.git
30 |
31 | **For Part III** : https://github.com/deepcloudlabs/stream-api-exercises-part3.git
32 |
--------------------------------------------------------------------------------
/src/com/example/animals/domain/Animal.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.domain;
2 |
3 | /**
4 | * @author Binnur Kurt
5 | */
6 | public abstract class Animal {
7 | private int legs;
8 |
9 | public Animal(int legs) {
10 | this.legs = legs;
11 | }
12 |
13 | public int getLegs() {
14 | return legs;
15 | }
16 | public void walk() {
17 | System.out.println(
18 | String.format("Animal with %d legs is walking now...",legs)
19 | );
20 | }
21 | public abstract void eat(); // abstract method
22 | }
23 |
--------------------------------------------------------------------------------
/src/com/example/animals/domain/Cat.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.domain;
2 |
3 | /**
4 | * @author Binnur Kurt
5 | */
6 | public class Cat extends Animal implements Pet {
7 | private String name;
8 |
9 | public Cat() {
10 | this("Garfield");
11 | }
12 |
13 | public Cat(String name) {
14 | super(4);
15 | this.name = name;
16 | }
17 |
18 | @Override
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 |
23 | @Override
24 | public String getName() {
25 | return name;
26 | }
27 |
28 | @Override
29 | public void play() {
30 | System.out.println(String.format("%s is playing now...", name));
31 | }
32 |
33 | @Override
34 | public void eat() {
35 | System.out.println(String.format("%s is eating now...", name));
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/example/animals/domain/Fish.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.domain;
2 |
3 | /**
4 | * @author Binnur Kurt
5 | */
6 | public class Fish extends Animal implements Pet {
7 | private String name;
8 |
9 | public Fish(String name) {
10 | super(0);
11 | this.name = name;
12 | }
13 |
14 | @Override
15 | public void setName(String name) {
16 | this.name = name;
17 | }
18 |
19 | @Override
20 | public String getName() {
21 | return name;
22 | }
23 |
24 | @Override
25 | public void play() {
26 | System.out.println(String.format("%s is playing now...", name));
27 | }
28 |
29 | @Override
30 | public void eat() {
31 | System.out.println(String.format("%s is eating now...", name));
32 | }
33 |
34 | @Override
35 | public void walk() {
36 | System.out.println(String.format("%s is swimming now...", name));
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/com/example/animals/domain/Pet.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.domain;
2 |
3 | /**
4 | * @author Binnur Kurt
5 | */
6 | public abstract interface Pet {
7 | abstract void setName(String name);
8 |
9 | public String getName();
10 |
11 | abstract public void play();
12 |
13 | static void playIfPet(Animal animal) {
14 | if (animal instanceof Pet)
15 | ((Pet) animal).play();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/example/animals/domain/Spider.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.domain;
2 |
3 | /**
4 | * @author Binnur Kurt
5 | */
6 | public class Spider extends Animal {
7 |
8 | public Spider() {
9 | super(8);
10 | }
11 |
12 | @Override
13 | public void eat() {
14 | System.out.println("Spider is eating now...");
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise1.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.function.Predicate;
6 | import java.util.stream.Collectors;
7 |
8 | import com.example.animals.domain.Animal;
9 | import com.example.animals.domain.Cat;
10 | import com.example.animals.domain.Fish;
11 | import com.example.animals.domain.Pet;
12 | import com.example.animals.domain.Spider;
13 |
14 | /**
15 | *
16 | * @author Binnur Kurt
17 | *
18 | */
19 | public class Exercise1 {
20 | private static final List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"),
21 | new Fish("Free Willy"), new Spider(), new Fish("Jaws"));
22 | private static final Predicate isPet = Pet.class::isInstance;
23 | private static final Predicate isWild = isPet.negate();
24 |
25 | public static void main(String[] args) {
26 | // Take a list of wild animals
27 | List wildAnimals = animals.stream().filter(isWild).collect(Collectors.toList());
28 | wildAnimals.stream().map(Object::getClass).map(Class::getName).distinct().forEach(System.out::println);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise2.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.function.Predicate;
6 | import java.util.stream.Collectors;
7 |
8 | import com.example.animals.domain.Animal;
9 | import com.example.animals.domain.Cat;
10 | import com.example.animals.domain.Fish;
11 | import com.example.animals.domain.Pet;
12 | import com.example.animals.domain.Spider;
13 |
14 | /**
15 | *
16 | * @author Binnur Kurt
17 | *
18 | */
19 | public class Exercise2 {
20 | private static final List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"),
21 | new Fish("Free Willy"), new Spider(), new Fish("Jaws"));
22 | private static final Predicate isPet = Pet.class::isInstance;
23 |
24 | public static void main(String[] args) {
25 | // Take a list of pets
26 | List wildAnimals = animals.stream().filter(isPet).collect(Collectors.toList());
27 | wildAnimals.stream().map(Object::getClass).map(Class::getName).distinct().forEach(System.out::println);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise3.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 | import java.util.List;
6 | import java.util.function.Consumer;
7 |
8 | import com.example.animals.domain.Animal;
9 | import com.example.animals.domain.Cat;
10 | import com.example.animals.domain.Fish;
11 | import com.example.animals.domain.Spider;
12 |
13 | /**
14 | *
15 | * @author Binnur Kurt
16 | *
17 | */
18 | public class Exercise3 {
19 | private static final Consumer printAnimal = animal -> System.out
20 | .println(animal.getClass().getSimpleName() + " with " + animal.getLegs() + " legs.");
21 |
22 | public static void main(String[] args) {
23 | // Find the animal with the highest number of legs
24 | List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
25 | new Spider(), new Fish("Jaws"));
26 | animals.stream().max(Comparator.comparing(Animal::getLegs)).ifPresent(printAnimal);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise4.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.security.SecureRandom;
4 | import java.util.Arrays;
5 | import java.util.List;
6 | import java.util.Random;
7 | import java.util.function.Supplier;
8 | import java.util.stream.Collectors;
9 | import java.util.stream.IntStream;
10 |
11 | import com.example.animals.domain.Animal;
12 | import com.example.animals.domain.Cat;
13 | import com.example.animals.domain.Fish;
14 | import com.example.animals.domain.Spider;
15 |
16 | /**
17 | *
18 | * @author Binnur Kurt
19 | *
20 | */
21 | public class Exercise4 {
22 | private static final Random random = new SecureRandom();
23 | private static final Supplier spiderCreator = Spider::new;
24 | private static final Supplier catCreator = Cat::new;
25 | private static final Supplier fishCreator = () -> new Fish("Çakıl");
26 | private static final List> suppliers = Arrays.asList(spiderCreator, catCreator, fishCreator);
27 |
28 | public static void main(String[] args) {
29 | // Take a list of 100 random animals
30 | final List randomAnimals = IntStream.generate(() -> random.nextInt(suppliers.size())).mapToObj(suppliers::get)
31 | .map(Supplier::get).limit(100).collect(Collectors.toList());
32 | randomAnimals.forEach(System.out::println);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise5.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import com.example.animals.domain.Animal;
7 | import com.example.animals.domain.Cat;
8 | import com.example.animals.domain.Fish;
9 | import com.example.animals.domain.Spider;
10 |
11 | /**
12 | *
13 | * @author Binnur Kurt
14 | *
15 | */
16 | public class Exercise5 {
17 | public static void main(String[] args) {
18 | // Find the total number of legs
19 | List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
20 | new Spider(), new Fish("Jaws"));
21 | long totalNumOfLegs = animals.stream().mapToLong(Animal::getLegs).sum();
22 | System.out.println(totalNumOfLegs);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise6.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Map;
6 | import java.util.function.BiConsumer;
7 | import java.util.stream.Collectors;
8 |
9 | import com.example.animals.domain.Animal;
10 | import com.example.animals.domain.Cat;
11 | import com.example.animals.domain.Fish;
12 | import com.example.animals.domain.Spider;
13 |
14 | /**
15 | *
16 | * @author Binnur Kurt
17 | *
18 | */
19 | public class Exercise6 {
20 | private static final BiConsumer> printGroup = (count, list) -> System.out
21 | .println(count + ": " + list);
22 |
23 | public static void main(String[] args) {
24 | // Group the animals by their number of legs
25 | List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
26 | new Spider(), new Fish("Jaws"));
27 | Map> groupedAnimals = animals.stream().collect(Collectors.groupingBy(Animal::getLegs));
28 | groupedAnimals.forEach(printGroup);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise7.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Map;
6 | import java.util.function.BiConsumer;
7 | import java.util.stream.Collectors;
8 |
9 | import com.example.animals.domain.Animal;
10 | import com.example.animals.domain.Cat;
11 | import com.example.animals.domain.Fish;
12 | import com.example.animals.domain.Spider;
13 |
14 | /**
15 | *
16 | * @author Binnur Kurt
17 | *
18 | */
19 | public class Exercise7 {
20 | private static final BiConsumer, Long> printGroup = (clazz, count) -> System.out
21 | .println(clazz.getSimpleName() + ": " + count);
22 |
23 | public static void main(String[] args) {
24 | // Count the number of animals in each species
25 | List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
26 | new Spider(), new Fish("Jaws"));
27 | Map, Long> groupedAnimals = animals.stream()
28 | .collect(Collectors.groupingBy(Animal::getClass, Collectors.counting()));
29 | groupedAnimals.forEach(printGroup);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/example/animals/exercises/Exercise8.java:
--------------------------------------------------------------------------------
1 | package com.example.animals.exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import com.example.animals.domain.Animal;
7 | import com.example.animals.domain.Cat;
8 | import com.example.animals.domain.Fish;
9 | import com.example.animals.domain.Spider;
10 |
11 | /**
12 | *
13 | * @author Binnur Kurt
14 | *
15 | */
16 | public class Exercise8 {
17 | public static void main(String[] args) {
18 | // Count the number of species
19 | List animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
20 | new Spider(), new Fish("Jaws"));
21 | long totalNumberOfSpecies = animals.stream().map(Animal::getClass).map(Class::getSimpleName).distinct().count();
22 | System.out.println(totalNumberOfSpecies);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------