Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use Java streams to map items

I am new to Java and started learning about java streams. I have understood filteration using streams however, understanding map is becoming a challenge.

I have a Person.java code

public class Person {

    // Fields
    private String name;
    private int age;
    private String gender;

    // Constructor
    public Person( String name,  int age,  String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    // Getters
    public String getName() {
        return this.name;
    }
    public int getAge() {
        return this.age;
    }
    public String getGender() {
        return this.gender;
    }

    // Setters
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

}

and Streams.java as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.List;
import java.util.stream.Collectors;


public class Streams {

    public static void main(String[] args) {
        final List <Person> people = getPeople();

        // Map
        System.out.println("Map using Streams | Increase Everyone's Age by 10:");
        List <Person> persons = people.stream()
            // this part is failing
            .map(person -> person.setAge(person.getAge() + 10))
            .collect(Collectors.toList()
        );
        persons.forEach(
            person ->
            System.out.printf("%s: %d\n", person.getName(), person.getAge())
        );

    }

    private static List<Person> getPeople() {
        return List.of(
            new Person("Female-Kid", 10, "female"),
            new Person("Male-Young", 21, "male"),
            new Person("Male-Mid", 34, "male"),
            new Person("Female-Old", 100, "female")
        );
    }
}

In above code, I have a list <Person> and I want to increase everyone’s age by 10, however above code is failing and is not correct.

Could someone let me know the correct implementation.

Cheers,
DD

>Solution :

The Stream.map() accepts the instance of Function interface. The problem is that most likely person.setAge() returns void, and therefore javac (Java most famous compiler, most likely you are using it), that is trying to infer the exact functional interface being used in lambda, cannot infer Function, since setAge returns void. In order to fix it, make setAge return this, such as:

public Person setAge(int age) {
    this.age = age;
    return this;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading