In previous post, we learnt to Retrieve text from a list of web elements using Java Stream APIs.

You may require just to retrieve unique text only. We used a List in previous post which allows duplicates. We can just use a Set to avoid duplicates.

Java Code:-

Output:-

You must be thinking why a separate post just to say use Set for unique elements. Anyone can think the same. Yes you are correct but here I am going to show you usage of some other aggregate functions of stream APIs.

map():

There is another method in Stream api named “map()”. It returns a stream consisting of the results of applying the given function to the elements of this stream. For example:- Suppose I have an array of int and I want to get square of each element of array. e.g. [1,2,3,4] => [1,4,9,16]. We can achieve the same without iterating explicitly using map.

distinct():-

It returns a stream consisting of the distinct elements of this stream. You can remove duplicates from a source using this method.

collect() :-

It is a terminal operation. We can retrieve the final stream in to a list or a set.

I will show usage of each method in below Selenium script:-

Using distinct():-

Output:-

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply