I get a set of elements by parsing a html document. There is a possibility that the elements may contain duplicates. What is the best way to list only unique elements?
I come from C++ background and see a possibility of doing it using a set and custom equality operation. However, not sure how to do it in Java. Appreciate any code that would help me do it the right and efficient way.
ArrayList<Element> values = new ArrayList<Element>();
// Parse the html and get the document
Document doc = Jsoup.parse(htmlDataInStringFormat);
// Go through each selector and find all matching elements
for ( String selector: selectors ) {
//Find elements matching this selector
Elements elements = doc.select(selector);
//If there are no matching elements, proceed to next selector
if ( 0 == elements.size() ) continue;
for (Element elem: elements ){
values.add(elem);
}
}
if ( elements.size() > 0 ) {
????? // Need to remove duplicates here
}