Remove Duplicates from ArrayList filled with POJOs

Crayl

I have an ArrayList filled POJOs and I want to remove all POJOs, which have a duplicate variable. This is the POJO:

public static class UrlStore {

    public String url;
    public String data;

    public UrlStore(String url) {
        this.url = url;
    }

    public void setData(String data) {
        this.data = data;
    }
}   

My way to remove duplicate "url"-variables in the ArrayList<UrlStore> is to iterate through the list and to remove those duplicates. It was said to me that I could simply use a Set to do this, but I can't figure out how to use it with an ArrayList containing POJOs. Or do you have even a better way?

Thanks for any suggestions!

Shashi
package test.urlstore;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class DuplicateDemo{

    public static void main(String[] args) throws Exception {
        List<UrlStore> urlStores = new ArrayList<UrlStore>();
        UrlStore usg = new UrlStore("google");
        UrlStore usy = new UrlStore("yahoo");
        UrlStore usb = new UrlStore("bing");
        UrlStore usa = new UrlStore("ask");
        UrlStore usd = new UrlStore("duckduckgo");


        usg.setData("mail");
        urlStores.add(usg);
        usg = new UrlStore("google");
        usg.setData("search");
        urlStores.add(usg);
        usg = new UrlStore("google");
        usg.setData("doc");
        urlStores.add(usg);
        usg = new UrlStore("google");
        usg.setData("search");
        urlStores.add(usg);
        usg = new UrlStore("google");
        usy.setData("search");
        urlStores.add(usy);
        usy.setData("search");
        urlStores.add(usy);
        usb.setData("search");
        urlStores.add(usb);
        usb.setData("search");
        urlStores.add(usb);
        usa.setData("search");
        urlStores.add(usa);
        usd.setData("search");
        urlStores.add(usd);

        System.out.println("before removing duplicates");
        // before removing duplicates
        for (Iterator iterator = urlStores.iterator(); iterator.hasNext();) {
            UrlStore urlStore = (UrlStore) iterator.next();
            System.out.println(urlStore.toString());
        }

        System.out.println("\n\nafter removing duplicates");
        //removing duplicates
         Set<UrlStore> uniqueUrlStores = new HashSet<UrlStore>(urlStores);

         //After removing duplicates
         for (Iterator iterator = uniqueUrlStores.iterator(); iterator.hasNext();) {
            UrlStore urlStore = (UrlStore) iterator.next();
            System.out.println(urlStore.toString());

        }

    }



    static class UrlStore {

        public String url;
        public String data;

        public UrlStore(String url) {
            this.url = url;
        }

        public void setData(String data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "UrlStore [url=" + url + ", data=" + data + "]";
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((data == null) ? 0 : data.hashCode());
            result = prime * result + ((url == null) ? 0 : url.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            UrlStore other = (UrlStore) obj;
            if (data == null) {
                if (other.data != null)
                    return false;
            } else if (!data.equals(other.data))
                return false;
            if (url == null) {
                if (other.url != null)
                    return false;
            } else if (!url.equals(other.url))
                return false;
            return true;
        }


    }   
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How would I remove duplicates from an Arraylist<Integer[]>? (Java)

How to delete duplicates from an arraylist

Remove duplicates (both values) - duplicate values from an ArrayList

Remove duplicates from ArrayLists

Remove duplicates items from arraylist and hashmap

Remove duplicates ArrayList custom object

Java how to remove duplicates from ArrayList

How to remove shapes from filled SVG path

Remove reverse duplicates from dataframe

Remove duplicates from ArrayList<String[]> - java

Algorithm for removing duplicates from ArrayList

Remove duplicates from ArrayList<JsonJavaObject> or how to find out if a similar item already exists in arraylist?

Remove ArrayList duplicates in a text file - Java

How to implement hashCode and equals method to remove duplicates from ArrayList

remove duplicates from json

Remove Duplicates From A Row

Duplicates elements not removed from ArrayList

Remove specific duplicates in ArrayList in Java

How to remove duplicates from ArrayList<Object> with duplicate values

remove background or mark filled areas from imfill

How to remove duplicates value from ArrayList<CustomObject> in java

Remove Duplicates Data(Model name and price) from Arraylist and add model with Price(adding) in android

Remove duplicates from ArrayList issue

Remove elements from ArrayList while retaining the duplicates if any present

Remove duplicates from select

addedprogram in android to remove duplicates in ArrayList<obj>

Remove duplicates from a CardArray

Remove label from filled DesktopDatePicker Material UI

Remove Duplicates from csv

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive