The Stream.collect() Method
List<Integer> numberList = Arrays.asList(11, 3, 2, 3, 8, 9, 0, 7, 2);
numberList.stream().collect(Collectors.toSet());
Collect metodu Stream türündeki nesneleri başka biçimdeki nesneye, veri yapısına dönüştürmek için kullanılır. Collector arayüzünden türeyen bir parametre beklemektedir. Bu parametre bilgisi ile istenilen türe dönüşüm sağlanabilir. Collector türünden arayüzler Collectors sınıfının içinde bulunan metodlar ile elde edilebilir.
Collector Interface
- yeni bir sonuç containerının oluşturulması (supplier())
- yeni bir elemanı sonuc containerına dahi etme (accumulator())
- iki sonuç containerını bir araya getirmek (combiner())
- containerda isteğe bağlı bir son dönüşüm gerçekleştirme (finisher())
.Collect(Collectors.) örnekleri
List givenList = Arrays.asList("a", "bb", "ccc", "dd");
- toList()
List<String> result = givenList.stream().collect(toList());
- Collectors.toUnmodifiableList()
List<String>result=givenList.stream()
.collect(toUnmodifiableList());
- toSet()
Set<String> result = givenList.stream()
.collect(toSet());
- Collectors.toUnmodifiableSet()
Set<String> result = givenList.stream()
.collect(toUnmodifiableSet());
- Collectors.toCollection()
List<String> result = givenList.stream()
.collect(toCollection(LinkedList::new))
- Collectors.toMap()
Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length))
identity fonksiyonu
static <T> Function<T, T> identity() {
return t -> t;
duplicate bir veri gelirse
List<String> listWithDuplicates =
Arrays.asList("a", "bb", "c", "d", "bb"); assertThatThrownBy(
() -> { listWithDuplicates.stream()
.collect(toMap(Function.identity(), String::length)); })
.isInstanceOf(IllegalStateException.class);
çözümü
Map<String, Integer> result =
givenList.stream()
.collect(toMap(Function.identity(),
String::length,
(item, identicalItem) -> item));
- Collectors.toUnmodifiableMap()
Map<String, Integer> result = givenList.stream()
.collect(toUnmodifiableMap(Function.identity(),
String::length))
- Collectors.collectingAndThen()
List<String> result = givenList.stream()
.collect(collectingAndThen(toList(), ImmutableList::copyOf))
- Collectors.joining()
String result = givenList.stream(joining());
"abbcccdd"
String result = givenList.stream()
.collect(joining(" "));
"a bb ccc dd"
String result = givenList.stream()
.collect(joining(" ", "PRE-", "-POST"));
"PRE-a bb ccc dd-POST"
- Collectors.counting()
Long result = givenList.stream()
.collect(counting());
- Collectors.summarizingDouble/Long/Int()
DoubleSummaryStatistics result = givenList.stream()
.collect(summarizingDouble(String::length));
assertThat(result.getAverage()).isEqualTo(2); assertThat(result.getCount()).isEqualTo(4); assertThat(result.getMax()).isEqualTo(3); assertThat(result.getMin()).isEqualTo(1); assertThat(result.getSum()).isEqualTo(8);
- Collectors.maxBy()/minBy()
Optional<String> result = givenList.stream()
.collect(maxBy(Comparator.naturalOrder()));
- Collectors.groupingBy()
Map<Department, List<Employee>> byDept =
employees.stream()
.collect(Collectors.
groupingBy(Employee::getDepartment));
Map<Integer, Set<String>> result = givenList.stream()
.collect
(groupingBy(String::length, toSet()));
Map<Department, Integer> totalByDept = employees.stream()
.collect(Collectors
.groupingBy(Employee::getDepartment
.Collectors.summingInt(Employee::getSalary)));
- Collectors.partitioningBy()
Map<Boolean, List<String>> result =
givenList.stream()
.collect(partitioningBy(s -> s.length() > 2))
{false=["a", "bb", "dd"], true=["ccc"]}
Map<Boolean, List<Student>> passingFailing =
students.stream().collect(Collectors
.partitioningBy
(s -> s.getGrade() >= PASS_THRESHOLD));
- Collectors.teeing()
List<Integer> numbers = Arrays.asList(42, 4, 2, 24);
Optional<Integer> min =
numbers.stream().collect(minBy(Integer::compareTo));
Optional<Integer> max =
numbers.stream().collect(maxBy(Integer::compareTo));
--java 12 sonrası
numbers.stream()
.collect(teeing( minBy(Integer::compareTo),
maxBy(Integer::compareTo),
(min, max) ->
));
HashMap<String, Employee> result =
employeeList.stream().collect(Collectors.teeing(
Collectors.maxBy(Comparator.
comparing(Employee::getSalary)).
Collectors.minBy(Comparator.
comparing(Employee::getSalary)),
(e1, e2) -> {
HashMap<String, Employee> map = new HashMap();
map.put("MAX", e1.get());
map.put("MIN", e2.get());
return map;
}
));
Top comments (0)