Can you generate a simple java 8 stream api task to check my knowledge
- User
Can you generate a simple java 8 stream api task to check my knowledge
- Assistant
Sure, here's a simple task for you.
You have a list of people with their names and ages. Your task is to use Java 8 Stream API to perform the following tasks:
- Filter all people who are older than 18.
- Transform the result to a list of names (i.e., not the whole Person objects but only names).
- Sort the resulting list of names alphabetically.
- Print the final list of names.
Here's the initial code for you:
import java.util.*; class Person { private String name; private int age; // constructor, getters and setters public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } } public class Main { public static void main(String[] args) { List<Person> people = Arrays.asList( new Person("John", 20), new Person("Sarah", 15), new Person("Greg", 25), new Person("Nancy", 10), new Person("Lisa", 30), new Person("Robert", 50) ); // Your solution here } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.