Make Selenium Easy

MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into a List Of Maps Using Java Stream APIs

Last updated on May 9th, 2020 at 06:58 am

I started a series of “Ready To Use Selenium Java Code”. In last post we have already seen a detailed Java code to store a web table data in to List of Maps. In this post, we will do the same stuff in more optimal way using Java Stream APIs.

WHAT IS WEB TABLE?

A table is made of rows and columns. When we create a table for a web page, that is called as a web table. In HTML, a web table is created using <table> tag. Web table is a HTML structure for creating rows and columns on a Web page.

A web table can consists below parts:

  1. Header(s): It is created using <th> tag.
  2. Row(s):It is created using <tr> tag.
  3. Columns(s):It is created using <td> tag.

A typical web table’s first row is marked as header and from second row onward, it stores values. Table headers can be grouped using <thead> and remaining row can be grouped as <tbody>. Last row can also be grouped as <tfoot>. An example is shown below:-

If you save above html code as <someName>.html and open a web browser, you will see output as below:-

So, we are going to learn how can we store a web table data in a Java Collection. Let’s start with logic and connect dots.

Let’s have a bigger web table first. HTML Code is as below:-

Save above html code in a file with .html extension and open in a web browser. You will see a table as below:-

Let’s break logic to multiple points first:-

1.First row is header and remaining are data.

2. We can store all data in a Java Collection List but identification of data is difficult i.e. which data represents which value.

3. It is good to store each row in a map where key will be header name and value will be current row cell value. For E.g. [Name=Amod, Age=29…..]

4. After adding a row in to Map, add that Map in to List. So we will have a List of Map.

[{ID=1, Name=Amod, Age=29, Expereince=7 Years, Location=Bengaluru, Marital Status=No, Native=Bihar, Email ID=amod@gmail.com, Mobile No=12345, Ready To Relocate=Yes},

{ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email ID=mukesh@gmail.com, Mobile No=23456, Ready To Relocate=Yes}, …]

5. You can customize in whatever you want. You may want to store in a Map where ID or Name will be key and remaining as value for easy picking.

[{Amod = {ID=1, Name=Amod, Age=29, Expereince=7 Years, Location=Bengaluru, Marital Status=No, Native=Bihar, Email ID=amod@gmail.com, Mobile No=12345, Ready To Relocate=Yes},

{Mukesh = {ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email ID=mukesh@gmail.com, Mobile No=23456, Ready To Relocate=Yes}, …]

I have used Stream APIs of Java. It helps in writing logic in shorter form.

Java Code:-

Output:-

You may need to edit code based on your web table but logic will be less or more same.

You can clone code from my git repo.

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

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.

MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into a List Of Maps Using Java Stream APIs

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