user1556718
user1556718

Reputation: 101

Hashset duplicate values but I dont have custom objects

I am using jsoup parser to extract my anchor tags and then I am just adding the links to a hash set. The code is as follows

Posting my entire code. I understand the issue is because I am using toString and the value would change My goal is when I get a bunch of links I want to eliminate links such as http://cse.syr.edu and http://cse.syr.edu/ so that my hashSet contains unique elements. How could I do this

for ( Element link : links)
{
        String test=link.attr("abs:href");

        if(!(link.attr("abs:href").contains("http://cse.syr.edu")))
            continue ;
        else if(h.isEmpty()){
            h.add(test);
        }
        else if(h.contains(test) || h.contains(test+"/")) // I now removed (test+"/")
            continue;
        else {

        h.add(test);

}

I have updated my question now thanks RJ

Upvotes: 0

Views: 114

Answers (2)

NPE
NPE

Reputation: 500733

If we are talking about java.util.HashSet, the most likely explanation is that your diagnosis of the problem is incorrect. Make sure that the strings in the set are indeed identical (and not subtly different), and that you are not accidentally re-creating or clearing the HashSet between adding identical strings.

Upvotes: 1

Kayaman
Kayaman

Reputation: 73568

There's probably whitespace in your Strings. HashSet works just fine.

Upvotes: 5

Related Questions