0
//retrieving a set of WebElement    

List <WebElement> discount =driver.findElements(By.className("saleD"));

for(int i=0;i<discount.size();i++)
{
     //getting text of webelements
     String disc_per= discount.get(i).getText();
     // Now I want that only unique texts to be stored. What can I do to get this.
}
System.out.println(count);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
smita
  • 1
  • 1
  • 1
    Please define your problem proper way.http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist – Subodh Joshi Aug 18 '15 at 13:34

2 Answers2

0

Convert the List to a Set , which will hold ONLY Unique values.

List<String> webElementText=  Store all (webElement.getText)
Set<String> uniquewebElementsText = new HashSet<String>(webElementText);

Now the uniquewebElementsText has all Unique entries.

Asit Tripathy
  • 133
  • 1
  • 9
-1

List#contains() method gives you if the list already has that element or not.

ArrayList list = new ArrayList();
for(int i = 0; i < discount.size(); ++i)
{
 if(!list.contains(discount.get(i))
   list.add(discount.get(i));
}
System.out.println("No of unique elements are : " + list.size());
System.out.println(list);

Or you could use a set.

Set set = new Set();
for(int i = 0; i < discount.size(); ++i)
{
   set.add(discount.get(i));
}

Or a 1 liner.

Set<String> s = new LinkedHashSet<String>(list);
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41