hi forum. I have a code to return an arrayList with the duplicates of an ArrayList that has the methods get1(), get2(), get3(), get4(),
which are the criteria to see if the object is duplicated, but seems it´s not working, I am comparing all items in the array...

I have:

public ArrayList<ObjectList> duplicates(ArrayList<ObjectList> someObjectsList) {

    ArrayList<ObjectList> ret = new ArrayList<ObjectList>();
    for ( ObjectList aSomeObjectsList: someObjectsList) {

        String field1 = aSomeObjectsList.get1();
        String field2 = aSomeObjectsList.get2();
        String field3 = aSomeObjectsList.get3();
        String field4 = aSomeObjectsList.get4();
       if (
                field1.trim().equals(someObject.get1())&& 
                field2.trim().equals(someObject.get2())&&
                field3.trim().equals(someObject.get3())&&
                field4.trim().equals(someObject.get4())     
         ){
              
            //What else do I have to do
           // ret.add(aSomeObjectsList);
       }
        

    }
    return ret;
}

Recommended Answers

All 4 Replies

someObject isn't defined. So can't really figure what're you to trying to compare to.
Also you're trimming one and not the other

I have a class

public class ObjectList {
	private String consec;
	private String field1;
	private String field2;
	private String field3;
	private String field4;

..the getters and setters

and I have an arrayList of this objects, I want to get the duplicates of the arrayList and save them to another arrayList

public ArrayList<ObjectList> duplicates(ArrayList<ObjectList> someObjectsList) {

    ArrayList<ObjectList> ret = new ArrayList<ObjectList>();
    for ( ObjectList aSomeObjectsList: someObjectsList) {

        String field1 = aSomeObjectsList.getfield1();
        String field2 = aSomeObjectsList.getfield2();
        String field3 = aSomeObjectsList.getfield3();
        String field4 = aSomeObjectsList.getfield4();
       if (
                field1.equals(someObject.getfield1())&& 
                field2.equals(someObject.getfield2())&&
                field3.equals(someObject.getfield3())&&
                field4.equals(someObject.getfield4())     
         ){
              
            //What else do I have to do
           // ret.add(aSomeObjectsList);
       }
        

    }
    return ret;
}

I was thinking in calling this method inside a for loop, that pass the method the complete array and a second parameter, the object to compare to.

What do you think ???

public ArrayList<ObjectList> duplicates(ArrayList<ObjectList> someObjectsList, String compareTo) {

    ArrayList<ObjectList> ret = new ArrayList<ObjectList>();
    for ( ObjectList aSomeObjectsList: someObjectsList) {

        String field1 = aSomeObjectsList.get1();
        String field2 = aSomeObjectsList.get2();
        String field3 = aSomeObjectsList.get3();
        String field4 = aSomeObjectsList.get4();
        String  field5 = field1+field2+field3+field4;
       if ( field5.equals(compareTo) ){
               ret.add(compareTo);
       }
        

    }
    return ret;
}

but once I have the arrayList of the duplicates, how do I get all the object back to field1, field2, field3,field4....

Also how could i recover the consec field ???

From OOD point of view, the responsibility of providing equals() method should be within the class whoes instance is being compared. I.e. ObjectList in this case.
Somethign like this:

public class ObjectList {
	private String consec;
	private String field1;
	private String field2;
	private String field3;
	private String field4;

        public boolean equals(Object obj) {
                if (obj == this)
                        return true;
                if (!(obj instanceof ObjectList))
                        return false;

                ObjectList o1 = (ObjectList) obj ;
		return (super.equals(obj) && consec.equals(o1.consec) && field1.equals(o1.field1) && ... ) ;
        }
}

public ArrayList<ObjectList> duplicates(ArrayList<ObjectList> someObjectsList) {
     // Now iterate over the list
     // for each item, ensure it exists only once in this list
     //     if (indexOf(item) == lastIndexOf(item))
     // If not, there is a duplicate, so add this item to return list.
}

Catch is you can't use indexOf & lastIndexOf unless you implement equals. If you don't implement equals (as your original code) you need to implement indexOf & lastIndexOf by yourself..

PS: You should implement equals in ObjectList irrespective as it belongs there as per OOD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.