2019 Books

How to Read the Bible for All It’s Worth by Gordon Fee and Douglas Stuart. “2019 Books” is published by Riley Rapp in Riley Rapp.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Share several methods of deduplicating lists through Stream in Java 8

Here I will share several methods for de-duplicating the list. It can be regarded as a sorting. If there are any mistakes, please let me know.

distinct()is a method provided by Stream in Java 8, which returns a stream composed of different elements in the stream. distinct()Use the hashCode()and eqauls()methods to get the different elements. Therefore, classes that need to be deduplicated must implement hashCode()and equals()methods. In other words, we hashCode()can equals()achieve deduplication for some special needs by overriding custom and methods.

distinct()The method declaration is as follows:

Because the Stringclass has overridden the equals()and hashCode()methods , it can be deduplicated successfully.

The result is as follows:

Note: In the code, we use Lombokthe @Dataannotations of the plugin, which can automatically override equals()and hashCode()methods.

The result is as follows:

The result is as follows:

We first create a method as Stream.filter()the parameter of , and its return type is Predicate, the principle is to judge whether an element can be added Setto , the code is as follows:

Use as follows:

The result is as follows:

The above are several methods for list deduplication that I want to share. Of course, there is no more detailed performance analysis here. If there are any mistakes, please let me know.

Add a comment

Related posts:

6 Ways To Improve Your Decision Making Skills

Everything you do in your daily life is a decision. Whether you are choosing between working out and watching Netflix, reading a meaningful book and mindless scrolling the internet, doing high…

Facts About Streaming New Release Movies at Home

Streaming films legally at home as they are playing in cinemas is such a controversial topic. To get to the bottom of things, we asked the opinion of thousands of people. The results may surprise you.

ADHD and Autistic Burnout

ADHD and Autistic Burnout: What are they, how are they different, and what can you do about it? Burnout can be a lot of things and will look and feel different for each person, but there are some commonalities. I can only speak from my own experiences, but hopefully, they will be relatable and helpful for others.