1. toList()
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ToListExam {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("lee",9, Person.Gender.MALE),
new Person("kim",5, Person.Gender.FEMALE),
new Person("park",6, Person.Gender.FEMALE),
new Person("sin",3, Person.Gender.FEMALE),
new Person("hong",4, Person.Gender.MALE),
new Person("ahn",7, Person.Gender.MALE),
new Person("moo",1, Person.Gender.MALE)
);
// 남학생만 필터링 후 List 생성
List<Person> maleList = personList.stream()
.filter(s->s.getGender()==Person.Gender.MALE)
.collect(Collectors.toList());
/*
Collector<T, ?, List<T>> toList()
T : 참조타입
? : 수집기 List로 받기 때문에 수집기가 필요하지 않음
List<T> : 리턴값
*/
maleList.forEach(System.out::println);
}
}
2. toCollection
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ToListExam {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("lee",9, Person.Gender.MALE),
new Person("kim",5, Person.Gender.FEMALE),
new Person("park",6, Person.Gender.FEMALE),
new Person("sin",3, Person.Gender.FEMALE),
new Person("hong",4, Person.Gender.MALE),
new Person("ahn",7, Person.Gender.MALE),
new Person("moo",1, Person.Gender.MALE)
);
Set<Person> femaleSet = personList.stream()
.filter(s->s.getGender()==Person.Gender.FEMALE)
.collect(Collectors.toCollection(new Supplier<Set<Person>>() {
@Override
public Set<Person> get() {
return new HashSet<>();
}
}));
femaleSet.forEach(System.out::println);
}
}
메소드 참조
Set<Person> femaleSet = personList.stream()
.filter(s->s.getGender()==Person.Gender.FEMALE)
.collect(Collectors.toCollection(HashSet::new));
3. 사용자 정의 수집
/* 수집 대상 객체 */
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 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.ArrayList;
import java.util.List;
public class MalePerson {
private List<Person> list;
public MalePerson() {
list = new ArrayList<>();
}
public void accumulate(Person person){
list.add(person);
}
public List<Person> getList() {
return list;
}
public void combine(MalePerson other){
list.addAll(other.getList());
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class CollectExam {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("lee",9, Person.Gender.MALE),
new Person("kim",5, Person.Gender.FEMALE),
new Person("park",6, Person.Gender.FEMALE),
new Person("sin",3, Person.Gender.FEMALE),
new Person("hong",4, Person.Gender.MALE),
new Person("ahn",7, Person.Gender.MALE),
new Person("moo",1, Person.Gender.MALE)
);
MalePerson malePerson =
personList.stream().filter(i->i.getGender()== Person.Gender.MALE)
.collect(
// @Param1
new Supplier<MalePerson>() { // supplier : 사용자정의 수집기
@Override
public MalePerson get() {
return new MalePerson();
}
},
// @Param2
new BiConsumer<MalePerson, Person>() { // @param2 accumulator : 수집기에 누적
@Override
public void accept(MalePerson malePerson, Person person) {
malePerson.accumulate(person);
}
},
// @Param3
new BiConsumer<MalePerson, MalePerson>() { // @param2 combiner : 병렬처리에서 서로 다른 수집기 결합
@Override
public void accept(MalePerson malePerson1, MalePerson malePerson2) {
malePerson1.combine(malePerson2);
}
}
);
malePerson.getList().stream().forEach(System.out::println);
}
}
/* 람다 */
MalePerson malePerson =
personList.stream().filter(i->i.getGender()== Person.Gender.MALE)
.collect(
() -> new MalePerson(),
(mp, p) -> mp.accumulate(p),
(mp1, mp2) -> mp1.combine(mp2)
);
/* 메소드 참조 */
MalePerson malePerson =
personList.stream().filter(i->i.getGender()== Person.Gender.MALE)
.collect(MalePerson::new, MalePerson::accumulate, MalePerson::combine);
[Lambda] 람다 기본 문법 (0) | 2020.06.19 |
---|---|
[Stream] Collect - groupingBy : 컬렉션 요소를 그룹핑해서 Map객체 얻기 (0) | 2020.06.18 |
[Stream] 스트림 커스텀 집계 reduce (0) | 2020.06.18 |
[Stream] 스트림 Optional : orElse, ifPresent (0) | 2020.06.18 |
[Stream] 스트림 - 집계 Aggregate : count(), sum(), average(), max(), min() (0) | 2020.06.18 |
댓글 영역