Make Selenium Easy

Usage of Java Stream API in Selenium – Retrieving Unique Text From a List Of WebElements

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


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.


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.


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


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

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Usage of Java Stream API in Selenium – Retrieving Unique Text From a List Of WebElements

by Amod Mahajan time to read: 3 min
%d bloggers like this: