0

I have a list of an Object and I want to detect whether Object Id is duplicate or not. Here is the object:

public class data{
private String id;
private String value;
private String status;
}

All duplicate data will have "invalid" status except the first one.

What is the most effective way for this?

Quân Trần
  • 31
  • 1
  • 6
  • 3
    Without much code and hard-work...I would call it a Christmas Miracle!! – Mathews Mathai Dec 28 '15 at 03:55
  • Have a look at this [question](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) – user7 Dec 28 '15 at 03:58
  • I think his issue is with `private String id;` being `private`, and not related to `Object`'s real ID...But I could be wrong... – XenoRo Dec 28 '15 at 04:02
  • Look at this post http://stackoverflow.com/questions/6737212/how-to-find-duplicates-in-an-arraylistobject – cagryInside Dec 28 '15 at 04:02

4 Answers4

0

use java ConcurrentHashMap instead of arraylist.

ConcurrentHashMap<yourid, YourBean> chp = new ConcurrentHashMap<>();
chp.putIfAbsent(yourid, YourBean);

and to list all your id do something like this

  for (Entry<yourid, YourBean> e : chp.entrySet()) 
{
 YourBean object = (YourBean )chp.get(e.getKey());
//do what u want with your object, guess that helps
}
NemugaM
  • 11
  • 5
0

You could consider overriding the .equals() method of the data class. Doing so would allow you to do the following to check for duplicate elements:

ArrayList<data> array_list = new ArrayList<data>();

// add some elements to array list

// check for duplicates
for(int i =0; i < array_list.size(); i++){
    for(int j=0; j<array_list.size(); j++){
        // compare for equality if it is not the same element
        if(i != j){
            if(array_list.get(i).equals(arrayList.get(j))){
                // than we know there is a duplicate at index i,j
                System.out.println("duplicate indexes: " + i + ", " + "j");
            }
        }
    }
}

Here is an example of how you would override the .equals method of the data class.

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof data)){ return false; }
    if (obj == this) { return true; }

    // compare strings to see if they are equal
    data other_data = (data)obj;
    boolean id_equal = other_data.id.equals(this.id);
    boolean value_equal = other_data.value.equals(this.value);
    boolean status_equal = other_data.status.equals(this.status);

    return id_equal && value_equal && status_equal
}

Edit If you only want to know whether the id's are equal or not you don't need to override .equals() of the data class.

In this case you need only need to use the first loop and compare the id stings instead of the data objects.

So instead of array_list.get(i).equals(arrayList.get(j), you would do (assuming you have getter methods for the private members of data): array_list.get(i).get(id).equals(array_list.get(j).get(id));

Alternatively you could use a method similar to the first one and override .equals() to only compare the id strings.

0

Try like this first you should override equals method to check duplicates

private  class data{
        private String id;
        private String value;
        private String status;

        public data(String id, String value, String status) {
            this.id = id;
            this.value = value;
            this.status = status;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        @Override
        public String toString() {
            return "data{" +
                    "id='" + id + '\'' +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof data)) return false;

            data data = (data) o;

            return !(id != null ? !id.equals(data.id) : data.id != null);

        }

        @Override
        public int hashCode() {
            return id != null ? id.hashCode() : 0;
        }
    }

Then test like this

public class Test {

        public static void main(String[] args ) {

            List<data> dataList=new ArrayList<>();
            dataList.add(new data("1","somevalue","somestatus"));
            dataList.add(new data("1","somevalue","somestatus"));
            dataList.add(new data("1","somevalue","somestatus"));
            dataList.add(new data("2","somevalue","somestatus"));
            dataList.add(new data("3","somevalue","somestatus"));
            List<data>validList=new ArrayList<>();
            List<data>duplicateList=new ArrayList<>();

            for (data data:dataList){
                if (!(validList.contains(data))){
                    validList.add(data);
                    System.out.println(validList);
                }else{
                    duplicateList.add(data);
                    System.out.println(duplicateList);
                }
            }

        }
soorapadman
  • 4,123
  • 6
  • 31
  • 42
-1

Make a list of the id of objects. Loop over the list of objects. See if the id of each object is already in the list. If the id is already present, then change the status of the object. Otherwise, add the id of the object to the list.

Anonymous
  • 409
  • 1
  • 5
  • 15
  • No need to make a separate list. Just use a `Getter` and iterate+compare with that. -1 (till fixed). – XenoRo Dec 28 '15 at 04:05
  • We need a list so that for the lists of object only the first objects which have unique id does not have an invalid status. I am not checking only with one object hence I need a list. – Anonymous Dec 28 '15 at 04:09
  • You *don't* need a second list to know if the first list has duplicates within it. You can check multiple objects with a simple `loop`. – XenoRo Dec 28 '15 at 04:20