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: |

Earlier, crypto enthusiasts had created a self-described “meme app” dubbed “gm” app wherein users would greet each other with “gm” or “good morning” messages. However, in September 2021, the gm app was down after a hacker reportedly gained access to the user data. There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the “degenerate” community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: “hey degen, are you stressed? Just let it out all out. Voice only tg channel for screaming”. Joined by Telegram's representative in Brazil, Alan Campos, Perekopsky noted the platform was unable to cater to some of the TSE requests due to the company's operational setup. But Perekopsky added that these requests could be studied for future implementation. 6How to manage your Telegram channel? According to media reports, the privacy watchdog was considering “blacklisting” some online platforms that have repeatedly posted doxxing information, with sources saying most messages were shared on Telegram.
from us


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