deluxan
deluxan

Reputation: 422

Duplicates is coming in HashSet

In my android development I am getting particular data from api. And using that data I iterate to create HashSet. The purpose of this implementation is to remove the duplicates. But still duplicate is presenting.

for(int i=0;i<array.length();i++) {
  HashSet<String> hashSetObject = new HashSet<String>();
  hashSetObject.add(leagueName);
  Log.d("HASHSET","values in HashSet object " + hashSetObject.toString());
}

In the above coding leagueName is a string from api. and the output of this code is as below...

D/HASHSET: values in HashSet object [League One]
D/HASHSET: values in HashSet object [Championship]
D/HASHSET: values in HashSet object [Premiership]
D/HASHSET: values in HashSet object [Championship]
D/HASHSET: values in HashSet object [League One]
D/HASHSET: values in HashSet object [Premiership]
.....

I need a HashSet without duplicates. Please help me.

Upvotes: 0

Views: 94

Answers (3)

TooManyEduardos
TooManyEduardos

Reputation: 4434

Your loop is creating a new set in every single iteration of the loop:

for(int i=0;i<array.length();i++) {
    HashSet<String> hashSetObject = new HashSet<String>(); // <-- new HashSet every time
    hashSetObject.add(leagueName);
    Log.d("HASHSET","values in HashSet object " + hashSetObject.toString());
}

change your code to this:

HashSet<String> hashSetObject = new HashSet<String>();
for(int i=0;i<array.length();i++) {
    hashSetObject.add(leagueName);
    Log.d("HASHSET","values in HashSet object " + hashSetObject.toString());
}

EDIT to address the logging issue: The second issue you have is the logging. The current loop is adding the element to the set properly, but the log is then outputting what's in the entire set every time time you loop through it.

It would probably be better to just loop through the final set instead.

Change your current code:

HashSet<String> hashSetObject = new HashSet<String>();
for(int i=0;i<array.length();i++) {
    hashSetObject.add(leagueName);
    Log.d("HASHSET","values in HashSet object " + hashSetObject.toString());
}

To something like this:

// Create set
HashSet<String> hashSetObject = new HashSet<String>();

// Add elements to the set
for(int i=0;i<array.length();i++) {
    hashSetObject.add(leagueName);
}

// Read each element from the set
for (String valueInSet : hashSetObject) {
    Log.d("HASHSET","value in HashSet object " + valueInSet);
}

Upvotes: 3

Niraj
Niraj

Reputation: 973

Do it like:

List<String> al = new ArrayList<>();
for(int i=0;i<array.length();i++) {
    al.add(leagueName);
}
Set<String> hashSetObject = new HashSet<>();
hashSetObject.addAll(al);

OR if your array is List<String>, you can directly call

Set<String> hashSetObject = new HashSet<>();
hashSetObject.addAll(array);

Upvotes: 0

Debabrata Das
Debabrata Das

Reputation: 23

You are creating a new instance of hashset each time your loop traverses. You end up with as many hashsets as many array elements.

Upvotes: 1

Related Questions