1. 성별을 키값으로 리스트 그룹핑 하기
public class Person {
enum Gender{ MALE, FEMALE }
enum City{ Seoul, Pusan }
private String name;
private Gender gender;
private int score;
private City city;
public Person(String name, int score, Gender gender) {
this.name = name;
this.score = score;
this.gender = gender;
}
public Person(String name, int score, Gender gender, City city) {
this.name = name;
this.gender = gender;
this.score = score;
this.city = city;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public Gender getGender() {
return gender;
}
public City getCity() {
return city;
}
@Override
public String toString() {
return "Person{name='" + name + '\'' + ", gender=" + gender + ", score=" + score + ", city=" + city + '}';
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GroupingExam {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("lee",9, Person.Gender.MALE, Person.City.Pusan),
new Person("kim",5, Person.Gender.FEMALE, Person.City.Seoul),
new Person("park",6, Person.Gender.FEMALE,Person.City.Pusan),
new Person("sin",3, Person.Gender.FEMALE, Person.City.Seoul ),
new Person("hong",4, Person.Gender.MALE, Person.City.Seoul),
new Person("ahn",7, Person.Gender.MALE,Person.City.Pusan),
new Person("moo",1, Person.Gender.MALE, Person.City.Seoul)
);
// 성별로 그룹핑
Map<Person.Gender, List<Person>> mapByGender =
personList.stream().collect(
Collectors.groupingBy(new Function<Person, Person.Gender>() {
@Override
public Person.Gender apply(Person person) {
return person.getGender(); // key값 결정
}
})
);
mapByGender.get(Person.Gender.MALE).stream().forEach(System.out::println);
System.out.println("======================================================");
mapByGender.get(Person.Gender.FEMALE).stream().forEach(System.out::println);
}
}
/* 메소드 참조 */
Map<Person.Gender, List<Person>> mapByGender =
personList.stream().collect(Collectors.groupingBy(Person::getGender));
2. 도시를 키값으로 그룹핑
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupingExam {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("lee", 9, Person.Gender.MALE, Person.City.Pusan),
new Person("kim", 5, Person.Gender.FEMALE, Person.City.Seoul),
new Person("park", 6, Person.Gender.FEMALE, Person.City.Pusan),
new Person("sin", 3, Person.Gender.FEMALE, Person.City.Seoul),
new Person("hong", 4, Person.Gender.MALE, Person.City.Seoul),
new Person("ahn", 7, Person.Gender.MALE, Person.City.Pusan),
new Person("moo", 1, Person.Gender.MALE, Person.City.Seoul)
);
// 도시로 그룹핑
Map<Person.City, List<Person>> mapByCity =
personList.stream().collect(Collectors.groupingBy(Person::getCity));
mapByCity.get(Person.City.Pusan).stream().forEach(System.out::println);
System.out.println("==================================================");
mapByCity.get(Person.City.Seoul).stream().forEach(System.out::println);
}
}
[Lambda] 함수형인터페이스 : Predicate (0) | 2020.06.19 |
---|---|
[Lambda] 람다 기본 문법 (0) | 2020.06.19 |
[Stream] 스트림 : collect - Collectors - toList, toCollection, 사용자 정의 수집 (0) | 2020.06.18 |
[Stream] 스트림 커스텀 집계 reduce (0) | 2020.06.18 |
[Stream] 스트림 Optional : orElse, ifPresent (0) | 2020.06.18 |
댓글 영역