0

I am new to android I am writing a program to remove duplicates in ArrayList

The program is as follows

while(iterator.hasNext()) {
                        it = iterator;
                        //it.next();
                        while (it.hasNext()) {
                            it.next();
                            if (it.equals(iterator)) {
                                it.remove();
                                //it.next();
                            }
                        }
                        iterator.next();
                    }

but it crashes. Can someone give mesomeinput?

error log->

FATAL EXCEPTION: main Process: com.example.root.securityalert, PID: 23735 java.util.NoSuchElementException at java.util.ArrayList$Itr.next(ArrayList.java:834) at com.example.root.securityalert.DeviceScanActivity$1.run(DeviceScanActivity.java:113) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6165) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

I also used

    List newList = new ArrayList(new LinkedHashSet(arrayOfUsers2));
    Iterator<ViewHolder> it=newList.iterator();
                        while(it.hasNext()) {
                            ViewHolder currentX = it.next();
                            adapter.add(currentX);
                        }
public class ViewHolder {
        String deviceName;
        String deviceAddress;

        public ViewHolder(String device, String __address) {
            this.deviceName =device;
            this.deviceAddress= __address;
        }
    }

but list has duplicates

I added the following code to ViewHolder class

@Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            ViewHolder a = (ViewHolder) o;
            return Objects.equals(deviceAddress, a.deviceAddress);
        }

and added the code

if(!arrayOfUsers2.contains(newUser))
  arrayOfUsers2.add(newUser);

when new device is pushed into arraylist and now it is fine

mithun
  • 121
  • 11

2 Answers2

0

To remove duplicate in array you can use this kind of trick :

List newList = new ArrayList(new LinkedHashSet(yourArrayList));

The trick is to use a set, since it can't have duplicate values it will ommit them. Then you can create a new array from it

Using LinkedHashSet will keep the order

An-droid
  • 6,433
  • 9
  • 48
  • 93
  • It did not remove duplicates.I have pasted the code – mithun Jun 22 '18 at 12:54
  • Are you sure it's really duplicate, it has to be exactly the same. If it's complex objects a small diff makes all the differents. It should work – An-droid Jun 22 '18 at 13:20
  • If you have ViewHolder duplicate I think you should try to fix it before. You should not have duplicate view holder in the first place. It's against the patern ^^ Also what is the point of having to manipulate an array of viewholder ? – An-droid Jun 22 '18 at 13:31
  • it is a bluetoothscanner and scans samedevice multiple times – mithun Jun 22 '18 at 13:41
  • Ok but the viewholder pattern is a pattern strongly binded to the ListView or any view you are using. You should clean and sort your data before passing it to the View. The Viewholder list should just have to be displayed – An-droid Jun 22 '18 at 13:45
0

You don't have to run loop iterator inside another loop of iterator for this. And in your code you are checking for equality two iteratos if (it.equals(iterator)) Why? :)

Anyway, I think such solution could be done with this:

Iterator<Object> iterator = list.iterator();
while (iterator.hasNext()) {
    Object i = iterator.next();
    if (list.contains(i)) {
        int index = list.indexOf(i);
        int lastIndex = list.lastIndexOf(i);

        if (index != lastIndex) {
            iterator.remove();
        }
    }
}

In order to make this solution work, your objects should implement equals method. For example, if you need to check only deviceAddress in your ViewHolder class, you should implement equals method like this:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    A a = (A) o;
    return Objects.equals(deviceAddress, a.deviceAddress);
}
Demigod
  • 5,073
  • 3
  • 31
  • 49