> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/interview-guide/technical/java-interview-companion/java-practice-programs/set-5-streams-and-collection.md).

# Set 5 - Streams and Collection

## Problem 1: Filtering and Collecting as Map

Given a list of Person class containing name and age fields, filter the person by allowing only above 20 years of age and get a map of persons by age.

{% hint style="warning" %}
Assumption

* Name is unique
  {% endhint %}

*person.java class*

```java
import java.util.Objects;

public class Person {
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }
}
```

```java
Person p1 = new Person("Alice", 21);
Person p2 = new Person("Larve", 22);
Person p3 = new Person("Kido", 20);
Person p4 = new Person("Nikee", 21);
Person p5 = new Person("Vieq", 19);
Person p6 = new Person("Yuk", 22);

List<Person> personList = List.of(p1,p2,p3,p4,p5,p6);
Map<Integer, List<Person>> personsByAge = personList.stream()
            .filter(person -> person.getAge() >= 20)
            .collect(Collectors.groupingBy(Person::getAge));

personsByAge.forEach((age, persons) -> System.out.println("Age " + age + ": " + persons));

// Output
// Age 20: [Person{name='Kido', age=20}]
// Age 21: [Person{name='Alice', age=21}, Person{name='Nikee', age=21}]
// Age 22: [Person{name='Larve', age=22}, Person{name='Yuk', age=22}]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/interview-guide/technical/java-interview-companion/java-practice-programs/set-5-streams-and-collection.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
