BOOKJAVA Telegram 4002
Stream Gathering with a Different Distinct Function

Статья обсуждает использование Gatherers в Java для определения собственной функции distinct. Автор предлагает альтернативный подход к стандартному методу distinct(), который позволяет более гибко определять уникальность элементов в потоке, что иногда может быть очень полезно.


import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class DistinctifyGatherer {
public static <T> Gatherer<T, ?, T> of(
ToIntFunction<T> hashCode,
BiPredicate<T, T> equals,
BinaryOperator<T> merger) {
class Key {
private final T t;
public Key(T t) {this.t = t;}
public int hashCode() {
return hashCode.applyAsInt(t);
}
public boolean equals(Object obj) {
return obj instanceof Key that
&& equals.test(this.t, that.t);
}
}
return Gatherer.<T, Map<Key, Key>, T>ofSequential(
LinkedHashMap::new,
(state, element, _) -> {
var key = new Key(element);
var existing = state.get(key);
if (existing != null) {
key = new Key(merger.apply(
existing.t, key.t));
}
state.put(key, key);
return true;
},
(keys, downstream) -> keys.values().stream()
.takeWhile(_ -> !downstream.isRejecting())
.map(key -> key.t)
.forEach(downstream::push)
);
}
}



https://www.javaspecialists.eu/archive/Issue326-Stream-Gathering-with-a-Different-Distinct-Function.html

👉@BookJava
👍2



tgoop.com/BookJava/4002
Create:
Last Update:

Stream Gathering with a Different Distinct Function

Статья обсуждает использование Gatherers в Java для определения собственной функции distinct. Автор предлагает альтернативный подход к стандартному методу distinct(), который позволяет более гибко определять уникальность элементов в потоке, что иногда может быть очень полезно.


import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class DistinctifyGatherer {
public static <T> Gatherer<T, ?, T> of(
ToIntFunction<T> hashCode,
BiPredicate<T, T> equals,
BinaryOperator<T> merger) {
class Key {
private final T t;
public Key(T t) {this.t = t;}
public int hashCode() {
return hashCode.applyAsInt(t);
}
public boolean equals(Object obj) {
return obj instanceof Key that
&& equals.test(this.t, that.t);
}
}
return Gatherer.<T, Map<Key, Key>, T>ofSequential(
LinkedHashMap::new,
(state, element, _) -> {
var key = new Key(element);
var existing = state.get(key);
if (existing != null) {
key = new Key(merger.apply(
existing.t, key.t));
}
state.put(key, key);
return true;
},
(keys, downstream) -> keys.values().stream()
.takeWhile(_ -> !downstream.isRejecting())
.map(key -> key.t)
.forEach(downstream::push)
);
}
}



https://www.javaspecialists.eu/archive/Issue326-Stream-Gathering-with-a-Different-Distinct-Function.html

👉@BookJava

BY Библиотека Java разработчика




Share with your friend now:
tgoop.com/BookJava/4002

View MORE
Open in Telegram


Telegram News

Date: |

Those being doxxed include outgoing Chief Executive Carrie Lam Cheng Yuet-ngor, Chung and police assistant commissioner Joe Chan Tung, who heads police's cyber security and technology crime bureau. Add up to 50 administrators Ng Man-ho, a 27-year-old computer technician, was convicted last month of seven counts of incitement charges after he made use of the 100,000-member Chinese-language channel that he runs and manages to post "seditious messages," which had been shut down since August 2020. The initiatives announced by Perekopsky include monitoring the content in groups. According to the executive, posts identified as lacking context or as containing false information will be flagged as a potential source of disinformation. The content is then forwarded to Telegram's fact-checking channels for analysis and subsequent publication of verified information. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be:
from us


Telegram Библиотека Java разработчика
FROM American