0

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.

deluxan
  • 372
  • 1
  • 5
  • 22
  • 1
    You are doing wrong. Inside the loop, you are initializing and adding every time in each iteration. Move your initializing of hash set outside the loop. – Rajnish suryavanshi Dec 25 '19 at 06:51
  • Now the output is like `D/HASHSET: values in HashSet object [Pro League, Championship] D/HASHSET: values in HashSet object [Pro League, Championship, Premier League] D/HASHSET: values in HashSet object [Pro League, Championship, Premiership, Premier League] D/HASHSET: values in HashSet object [Pro League, Championship, Premiership, Premier League].............` – deluxan Dec 25 '19 at 09:52

3 Answers3

3

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);
}
TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • After moving the instantiation my output is like `D/HASHSET: values in HashSet object [Pro League, Championship] D/HASHSET: values in HashSet object [Pro League, Championship, Premier League] D/HASHSET: values in HashSet object [Pro League, Championship, Premiership, Premier League] D/HASHSET: values in HashSet object [Pro League, Championship, Premiership, Premier League].......` – deluxan Dec 25 '19 at 09:53
  • 1
    edited the answer to also address the logging "problem" – TooManyEduardos Dec 26 '19 at 01:08
  • Inside the for loop there is a volley request where I have to pas this hashset value. I can't make it out of the loop. Because volley request needs to be looped – deluxan Dec 26 '19 at 05:59
  • 1
    you're making a network request for each individual element you have in the array??? why?? if your array has 100 items, you're going to make individual 100 calls – TooManyEduardos Dec 27 '19 at 06:03
  • Yes I knew it. But in my case I have to make another API call inside the loop because data from first API call is coming as id. Also we can not use the response value out of the response method. What is the proper solution – deluxan Dec 27 '19 at 06:08
  • You may want to look into rx Java so you can chain the calls a bit easier. But even then, you'll still make as many calls as what you have in the loop. You should look for a network call that accepts a large set with multiple values instead of a call that seems to only accept a set with 1 value. If you continue the current path, your application will have awful performance – TooManyEduardos Dec 27 '19 at 18:27
1

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

0

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);
Niraj
  • 903
  • 8
  • 23