/java/

Java8 – Stream simple example. part 2

2015-01-14 21:46:12

Example 1

List

<animal> animals = NoeShip.getAnimals();

animals
        .stream()
        .forEach(System.out::println);

Example 2

List<animal> animals = NoeShip.getAnimals();

animals
        .stream()
        .filter(a -&gt; a.getColor().equals(Animal.COLOR.GREEN))
        .forEach(System.out::println);

Example 3

List<animal> animals = NoeShip.getAnimals();

animals
        .stream()
        .filter(a -&gt; a.getColor().equals(Animal.COLOR.GREEN))
        .filter(a -&gt; a.getSex().equals(Animal.SEX.MEN))
        .forEach(System.out::println);

Example 4

List<animal> animals = NoeShip.getAnimals();

animals
        .stream()
        .map(a -&gt; {
            a.setSex(Animal.SEX.MEN);
            return a;
        })
        .forEach(System.out::println);

Example 5

List<animal> animals = NoeShip.getAnimals();

List<animaldto> dtos =
        animals
                .stream()
                .map(a -&gt; new AnimalDto(a))
                .collect(Collectors.toList());

dtos.forEach(System.out::println);

Example 6

List<animal> animals = NoeShip.getAnimals();

List<animaldto> dtosFiltred =
        animals
                .stream()
                .filter(a -&gt; a.getSex().equals(Animal.SEX.MEN))
                .map(a -&gt; new AnimalDto(a))
                .collect(Collectors.toList());

dtosFiltred.forEach(System.out::println);

Animal.java

public class Animal {

    public static void showDetails(Animal animal) {
        System.out.println("Details: " + animal);
    }

    public enum COLOR {WHITE, YELLOW, RED, BROWN, GREY, GREEN, BLACK}

    public enum SEX {MEN, FEMALE}

    private String name;
    private String species;
    private COLOR color;
    private SEX sex;

    public Animal() {
    }

    public Animal(String name, String species, COLOR color, SEX sex) {
        this.name = name;
        this.species = species;
        this.color = color;
        this.sex = sex;
    }

    // set &amp; get

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", species='" + species + '\'' +
                ", color=" + color +
                ", sex=" + sex +
                '}';
    }
}

AnimalDto.java

package example6;

import common.Animal;

public class AnimalDto {

    private String name;
    private String species;
    private String color;
    private String sex;

    public AnimalDto(Animal animal) {
        this.name = animal.getName();
        this.species = animal.getSpecies();
        this.color = animal.getColor().toString();
        this.sex = animal.getSex().toString();
    }

    @Override
    public String toString() {
        return "AnimalDto{" +
                "name='" + name + '\'' +
                ", species='" + species + '\'' +
                ", color='" + color + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

NoeShip.java

import java.util.ArrayList;
import java.util.List;

public class NoeShip {
    public static List<animal> getAnimals() {
        List<animal> animals = new ArrayList&lt;&gt;();
        animals.add(new Animal("azor", "dog", Animal.COLOR.BROWN, Animal.SEX.MEN));
        animals.add(new Animal("wiz", "dog", Animal.COLOR.RED, Animal.SEX.FEMALE));
        animals.add(new Animal("mara", "cat", Animal.COLOR.GREY, Animal.SEX.FEMALE));
        animals.add(new Animal("nat", "cat", Animal.COLOR.BLACK, Animal.SEX.MEN));
        animals.add(new Animal("michal", "human", Animal.COLOR.WHITE, Animal.SEX.MEN));
        animals.add(new Animal("cola", "bird", Animal.COLOR.BROWN, Animal.SEX.FEMALE));
        animals.add(new Animal("tank", "turtle", Animal.COLOR.GREEN, Animal.SEX.MEN));
        animals.add(new Animal("tirla", "turtle", Animal.COLOR.GREEN, Animal.SEX.FEMALE));
        return animals;
    }
}