Создание списка объектов из другого с использованием потоков java8
Я пытаюсь понять потоки Java 8.
У меня есть два класса:
public class UserMeal {
protected final LocalDateTime dateTime;
protected final String description;
protected final int calories;
public UserMeal(LocalDateTime dateTime, String description, int calories) {
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public String getDescription() {
return description;
}
public int getCalories() {
return calories;
}
}
и
public class UserMealWithExceed {
protected final LocalDateTime dateTime;
protected final String description;
protected final int calories;
protected final boolean exceed;
public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
this.exceed = exceed;
}
}
В поле exceed
указывается, будет ли сумма калорий за весь день. Это поле совпадает для всех записей за этот день.
Я пытаюсь получить объект из List<UserMeal> mealList
, группировать по дням, вычислять калории в течение определенного периода времени и создавать List<UserMealWithExceed>
:
public static List<UserMealWithExceed> getFilteredMealsWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
return mealList.stream()
.filter(userMeal -> userMeal.getDateTime().toLocalTime().isAfter(startTime)&&userMeal.getDateTime().toLocalTime().isBefore(endTime))
.collect(Collectors.groupingBy(userMeal -> userMeal.getDateTime().getDayOfMonth(),
Collectors.summingInt(userMeal -> userMeal.getCalories())))
.forEach( ????? );
}
но я не понимаю, как создать новый объект в forEach
и вернуть коллекцию.
Как я вижу в псевдокоде:
.foreach(
if (sumCalories>caloriesPerDay)
{return new UserMealWithExceed(userMeal.getdateTime, usermeal.getDescription, usermeal.getCalories, true);}
else
{return new UserMealWithExceed(userMeal.getdateTime, usermeal.getDescription, usermeal.getCalories, false)
}
)//foreach
Ответы
Ответ 1
Если вы хотите перебрать список и создать новый список с "преобразованными" объектами, вы должны использовать функцию map()
в stream + collect()
. В следующем примере я нахожу всех людей с фамилией "l1" и каждого человека, которого я "сопоставляю" с новым экземпляром Employee.
public class Test {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("e1", "l1"),
new Person("e2", "l1"),
new Person("e3", "l2"),
new Person("e4", "l2")
);
List<Employee> employees = persons.stream()
.filter(p -> p.getLastName().equals("l1"))
.map(p -> new Employee(p.getName(), p.getLastName(), 1000))
.collect(Collectors.toList());
System.out.println(employees);
}
}
class Person {
private String name;
private String lastName;
public Person(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
// Getter & Setter
}
class Employee extends Person {
private double salary;
public Employee(String name, String lastName, double salary) {
super(name, lastName);
this.salary = salary;
}
// Getter & Setter
}
Ответ 2
Возможно, вы ищете map()
. Вы можете "преобразовать" объекты в поток в другой, сопоставляя этот путь:
...
.map(userMeal -> new UserMealExceed(...))
...
Ответ 3
В дополнение к решению от @Rafael Teles. Синтаксический сахар Collectors.mapping
делает то же самое за один шаг:
//...
List<Employee> employees = persons.stream()
.filter(p -> p.getLastName().equals("l1"))
.collect(
Collectors.mapping(
p -> new Employee(p.getName(), p.getLastName(), 1000),
Collectors.toList()));
Подробный пример можно найти здесь