🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Beyond Stream.distinct()

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




A Stream of objects



Starting with Java 8, you can use a Stream to process elements from a Collection (mostly classes derived from List or Set will be used for this) in an iterating way, defining filtering and mapping lazily, and then "consume" the Stream.



You can transform the elements in a Stream for further processing by extracting values from objects (e.g. getting the city name from a person or calculating the length of a string) and filter elements (e.g. only persons older than x years or cities starting with "D") before consuming the resulting Stream of elements.






What defines "distinct"?



One special way of filtering elements in a Stream is using distinct() to return unique elements. Checking for uniqueness is internally done via the equals() method of the processed elements, so you need to implement both equals() and hashCode() accordingly to be able to use distinct(). While simple classes like String, Long or LocalDate work like expected, it might be difficult to correctly implement those methods for custom objects. And of course, this puts a limit on how custom objects can be processed, because you can only implement one way of checking for "equal" elements.






A more flexible, yet easy solution



First, you need to define a function that extracts the unique field from your custom object.



For example, we take this object representing a very simple person entry:




CODE
public class Person {
private int id;
private String firstname;
private String lastname;

public Person() { }

public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return this.firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return this.lastname;
}
}






You can get the ID by calling p.getId() on a Person object p. Or, to describe this as a function reference: Person::getId.

The same goes for Person::getFirstname and Person::getLastname.



Our list l (e.g. an ArrayList<Person>) contains some person entries and we want to get unique entries from that list, based on the lastname property.



For this we can use Collectors.groupingBy(...) with referencing the access function of a property and another Collector that "remembers" the first non-null person element:




CODE
Map<String, Person> m = l.stream()
.collect(Collectors.groupingBy(
Person::getLastname,
Collectors.reducing(null, (a, b) -> (a != null) ? a : b))
);






In result, we will get a map with all distinct last names as keys and the first person element with that last name as corresponding value.

If needed, the reduction collector can be changed to return the last person instead: Collectors.reducing(null, (a, b) -> (b != null) ? b : a))



Optionally, you can insert LinkedHashMap::new between the the access function and the reduction collector. This specifies the type of map that will be used to return the grouped data. When using a LinkedHashMap the processing order of the elements will be retained in the result. However, this is actually limited by the specifications of the given stream, so the results may vary.



This also works for simple element types, so you can use this logic for both custom and simple objects. For simple objects the access function is just (obj) -> obj or Function.identity().






Downsides and alternatives



The main downside of this method is that Stream.collect(...) is a terminal operation. This means that calling collect(...) will consume the stream and process all the filterings and mappings that are defined on the Stream. It will then return a final result of the collect operation, in our case the result map.

Of course, you can get a new Stream on the values or key set of that map for further processing, but the source Stream has been consumed at that point.

In contrast, distinct() is a stateful intermediate operation which returns a new Stream, but no operation is done at that moment on the source Stream.



Some open source projects like Eclipse Collections and Vavr provide alternatives to lists, sets, maps and streams which also include the option to define an access function to an object property when collecting distinct elements.



And there are some other clever alternatives described in the answers of this StackOverflow question. Btw. my personal favorite there is returning a Predicate via a helper method distinctByKey(...).






Conclusion



The standard method Stream.distinct() works well for simple types and custom objects that provide a fixed way to determine object equality.

If you need more flexibility on defining which property (or even a combination of multiple properties) make an object unique and filter data accordingly, you can utilize provided Collector implementations, build custom Predicates or Collectors or use other open source collection frameworks.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Beyond Stream.distinct()

Thematisch verwandte Begriffe: Beyond, Streamdistinct · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...