Fetching data from a table alphabetically using webdriver - java

Is there a way to fetch data from a table alphabetically using webdriver.
for example:
a table contains "sno, language, text, link"
I would like to know is there away to fetch data alphabetically using language as the column. Lets say the languages are not alphabetically ordered... Russian, Spanish, English, Chinese... ETC

Yes you need a DataStructure to store the WebElements which are getting listed while finding elements of table.
For Java you can use a DataStructure like HashMap where you can save the link texts as the Key later you can use Collection.sort to sort this DataStructure with the attribute/ column you want.

Related

Data Structure choices based on requirements

I'm completely new to programming and to java in particular and I am trying to determine which data structure to use for a specific situation. Since I'm not familiar with Data Structures in general, I have no idea what structure does what and what the limitations are with each.
So I have a CSV file with a bunch of items on it, lets say Characters and matching Numbers. So my list looks like this:
A,1,B,2,B,3,C,4,D,5,E,6,E,7,E,8,E,9,F,10......etc.
I need to be able to read this in, and then:
1)display just the letters or just the numbers sorted alphabetically or numerically
2)search to see if an element is contained in either list.
3)search to see if an element pair (for example A - 1 or B-10) is contained in the matching list.
Think of it as an excel spreadsheet with two columns. I need to be able to sort by either column while maintaining the relationship and I need to be able to do an IF column A = some variable AND the corresponding column B contains some other variable, then do such and such.
I need to also be able to insert a pair into the original list at any location. So insert A into list 1 and insert 10 into list 2 but make sure they retain the relationship A-10.
I hope this makes sense and thank you for any help! I am working on purchasing a Data Structures in Java book to work through and trying to sign up for the class at our local college but its only offered every spring...
You could use two sorted Maps such as TreeMap.
One would map Characters to numbers (Map<Character,Number> or something similar). The other would perform the reverse mapping (Map<Number, Character>)
Let's look at your requirements:
1)display just the letters or just the numbers sorted alphabetically
or numerically
Just iterate over one of the maps. The iteration will be ordered.
2)search to see if an element is contained in either list.
Just check the corresponding map. Looking for a number? Check the Map whose keys are numbers.
3)search to see if an element pair (for example A - 1 or B-10) is
contained in the matching list.
Just get() the value for A from the Character map, and check whether that value is 10. If so, then A-10 exists. If there's no value, or the value is not 10, then A-10 doesn't exist.
When adding or removing elements you'd need to take care to modify both maps to keep them in sync.

Parsing HTML and storing in java collection

I have a requirement where I have to parse an HTML page that contains multiple tables of scorecard. table structure remains same but based on data for different matches,different tables can contain different, though column names are same.
Now i need to search based on table columnname and data contained in it with an argument pair. e.g. if i have a column called playername and multiple tables contain many player names. if i search for a particular player name by passing 2 arguments- playername(column name) and Jason, it should fetch all rows where playername column has Jason as its data. i can pass another pair of arguments as a AND - matchesplayed(column name) and 15, it should fetch all rows from above result set where Jason played 15 matches.
Can you assist how I can achieve this. Logic I tried is-
get the data for all columns in different array lists.then create a map with the column names as keys and its values as different arraylists containing that column's data. Is my approach correct or i need to solve it using different approach.
Thanks for your help.
Let's make order. I use your example.
1) The first thing you have to do is searching for rows where playername == Jason. Using jsoup or another HTML parser you can easily have access to the td where Jason is contained in. From there, you can easily access to the parend trand to the table.
2) Using the table you can access first tr or th to individuate columnname to use as keys. Then using a positional logic (first with first, second with second) you can understand which columnname corresponds to which content (inside the td
3) How to collect data is up to you. Probably, a Map<String, String> can be a solution. Or, if data is static, you can create a Player pojo and use reflection api to fill it.
Giving us more details and snippets of code we can help you more.
You can use Jsoup to get the HTML Document and then write a method with the input player name values. This method should parse through <table> elements in the html document to get you what is needed. Parsing will be easy if you understand Jquery/css selectors.
Check this link for Jsoup Selectors.
http://jsoup.org/apidocs/org/jsoup/select/Selector.html

Compare very large tables in java

I am not able to find any satisfying solution so asking here.
I need to compare data of two large tables(~50M) with the same schema definition in JAVA.
I can not use order by clause while getting the resultset object and records might be not in order in both of the tables.
Can anyone help me what can be the right way to do it?
You could extract the data of the first DB table into a text file, and create a while loop on the resultSet for the 2nd table. As you iterate through the ResultSet do a search/verify against the text file. This solution works if memory is of concern to you.
If not, then just use a HashMap to hold the data for the first table and do the while loop and look up the records of the 2nd table from the HashMap.
This really depends on what you mean by 'compare'? Are you trying to see if they both contain the exact same data? Find rows in one not in the other? Find rows with the same primary keys that have differing values?
Also, why do you have to do this in Java? Regardless of what exactly you are trying to do, it's probably easier to do with SQL.
In Java, you'll want to create an class that represents the primary key for the tables, and a second classthat represents the rest of the data, which also includes the primary key class. If you only have a single column as the primary key, then this is easier.
We'll call P the primary key class, and D the rest.
Map map = new HashMap();
Select all of the rows from the first table, and insert them into the hash map.
Query all of the rows in the second table.
For each row, create a P object.
Use that to see what data was in the first table with the same Key.
Now you know if both tables contained the same row, and you can compare the non-key values from both both.
Like I said, this is much much easier to do in straight SQL.
You basically do a full outer join between the two tables. How exactly that join looks depends on exactly what you are trying to do.

What data structure to choose for an ELO rating system with 20k+ entries?

I have some Objects (currently 20 000 +) that have the following Attributes
Object
----------
String name
int rating
I want to create an ELO rating for all these Objects. This implies the following:
To adjust the ELO of 2 Objects matched against each other I need to find those Objects in the list by name.
To Display the List I need to get every Object ordered by its rating.
The whole program will be implemented in Java, but I think its independent from the programming language.
Currently I am unsure which data model to choose for this Project. A friend advised me to use a 2-4 tree to insert the Objects ordered by name so I can change the rating of each object fast.
However the Objects are printed in order of their rating rather than by name and I don't want to sort so many Objects every time I output the list.
As I am pretty new to data structures: What is the usual way to solve this problem?
Creating another tree ordered by rating?
Having a list of ratings and each rating linked to each Object currently having that rating?
Edit: ELO rating is a mapping from the set of objects to the integers. Each object only gets one rating but a rating can have multiple Objects associated with it.
Creating another tree ordered by rating? Having a list of ratings and each rating linked to each Object currently having that rating?
Well , this is one way to do so , but will take huge space also since you have 20K+ entries .
The best way i can think of now is :
Use datastructure like multimap with key=name , and value = ratings.
This way , everytime you insert a new object in multimap , it will take O(logN) time .
To find all ratings with same name use equal_range , which is also an O(logN) operation .
Hope this helps !
A HashMap with name as the key will give you O(1) performance when matching the elements, keep a TreeSet with a rating Comparator and you'll have the items in order. Although you'll need to reinsert an element if the rating changes.

Clustering Data

I have millions of names stored in my database these names are nothing but customer names,
I Have to cluster names which are phonetically similar to each other internally,
one approach that i am using is matching each name with some selective similar names fetched from database based on sound-ex,meta-phone,initials..etc
But it is very slow ,
now i am thinking about generating unique id for each names and clustering similar unique ids,
but i am not able to generate unique ids.
there Names are Indian names and written using English Alphabet.
Is there any algorithm for clustering similar names.
please help
The key problem here is "phonetically similar". You'd need to know how to generate a unique ID from phonemes.
You don't say which language and alphabet these names are stored in.
Maybe the problem has more in common with speech synthesis algorithms:
http://social.msdn.microsoft.com/Forums/da/netfxbcl/thread/b6b88747-9616-462e-9cf6-78c19da32f38
Or this one for Java:
http://voce.sourceforge.net/

Categories