1

Class:

public class Variant
{
    private String variant;
    private String quantity;
    //getters and setters
}

ArrayList:

ArrayList<Variant> variantList = getVariantsList();

Now I want to check whether variantList contains a duplicate entry of variant or not? Please note that variant having two entries with different quantity are to be considered as duplicates.

3
  • @Downvoter reason for downvoting will be appreciated. Commented Sep 16, 2015 at 9:58
  • It might be due to the lack of description what you already tried. It's actually not that hard a problem. Commented Sep 16, 2015 at 10:00
  • Why is quantity String type and not int/long ?? Commented Oct 5, 2015 at 5:59

9 Answers 9

3

You can simply ovveride your equals method in your Variant class and provide all the rules for equality in that method.

 @Override
    public boolean equals(Object obj) {
      ..

Then you can use contains method or just pass it to a Set, that eliminates all your duplicates.

If you want variant having two entries with different quantity also considered as dup, then you can add that condition in your equals.

Sign up to request clarification or add additional context in comments.

1 Comment

And if you use a LinkedHashSet you can still keep variant order. :)
1

Override equals(Object obj) method and try to compare the object on variant and quantity.

Try to loop thru the variantList and do check for duplicity using variantList.contains(variant).

Comments

1

There are two things you need to do:

  • Override the equals() in your Variant class(minimal code below): Please note that the below code only checks for quantity and not the variant prop. Your IDE might help you to generate the equals() as well.

    @Override
    public boolean equals(Object object) {
       boolean isEqual = (this == object);
       if(object instanceof Variant){
            Variant variant = (Variant) object;
            isEqual = this.quantity.equals(variant.quantity);
       }else{
           isEqual = false;
       }
    
       return isEqual;
    }
    
  • Check if the List contains the object - which will use the equals() to check if both are equal.

    for (Variant variant : variantList) {
       if (variantList.contains(variant)) {
          //do logic if its present
       }
    }
    

Comments

1

Just check one object with other objects of list

Override equals method in Variant class

@Override
    public boolean equals(Object obj) {
        if (obj != null) {

            if (obj instanceof Variant) {
                Variant temp = (Variant) obj;
                return this.quantity.equals(temp.quantity); //for different quantity
            } else {
                return false;
            }
        }
        return false;
    }

Then check :

   for (int i = 0; i < variantList.size(); i++) {
            for (int j = 0; j < variantList.size(); j++) {
                if (i != j) {
                    if (iList.get(i).equals(iList.get(j))) {
                        //logic when duplicate
                        break;
                    }
                }
            }
        }

2 Comments

Nope, won't work since v will always be contained by variantList, even if there is only one. Besides that you'd still have to override equals() which then should be part of the answer.
@developerbhuwan Apart from comment by @Thomas, Please note that variant having two entries with different quantity are to be considered as duplicates.
1

Follow the below guidelines:

  1. Your Class Variant must override the equals method, since you define a duplicate condition based on quality hence in the equals method check for quality attribute value i.e.

    public class Variant {
        private String variant;
        private String quantity;
    
    
    public Variant(String variant, String quantity) {
        this.variant = variant;
        this.quantity = quantity;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((quantity == null) ? 0 : quantity.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;
        Variant other = (Variant) obj;
        if (quantity == null) {
            if (other.quantity != null)
                return false;
        } else if (!quantity.equals(other.quantity))
            return false;
        return true;
    }
    
    }
  2. Create a method which basically checking whether your list contains the duplicate entries(Variant) or not and return true and false accordingly:

private static boolean isListContainsDuplicateEntries(
            ArrayList variantList) {
        final List setToReturn = new ArrayList();
        for (Variant v : variantList) {
            if (!setToReturn.contains(v)) {
                setToReturn.add(v);
            } else {
                return true;
            }
        }
        return false;
    }
  1. Now, test the functionality:

    public static void main(String[] args) {
            Variant variant1 = new Variant("1", "100");
            Variant variant2 = new Variant("2", "200");
            Variant variant3 = new Variant("3", "200");

    ArrayList<Variant> variantList = new ArrayList<>(); variantList.add(variant1); variantList.add(variant2); variantList.add(variant3); System.out.println(Variant.isListContainsDuplicateEntries(variantList));

Output: true

1 Comment

This should work, although I'd use a structure backed up by some form of a hashtable over a list (which cuts worst-case runtime from O(n^2) to O(n)).
0

You can use contains():

if (variantList.contains(**<some other Variant object>**)){ 
...
}

Comments

0

You can simply override your equals method in your Variant and try like this

 List<Varient> list =getVariantsList();
        System.out.println("here list size"+list.size());
        Set<Varient> set = new HashSet<Varient>(list);
        System.out.println("here"+set.size());

Comments

0

Create a varient Object:

  public class Varient {

    private String variant;
    private String quantity;


public String getVariant() {
    return variant;
}

public void setVariant(String variant) {
    this.variant = variant;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

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

    Varient varient = (Varient) o;

    if (!quantity.equals(varient.quantity)) return false;
    if (!variant.equals(varient.variant)) return false;

    return true;
}

@Override
public int hashCode() {
    int result = variant.hashCode();
    result = 31 * result + quantity.hashCode();
    return result;
}

}

Here is your main Program;

public class Test {
        public static void main (String [] args){
// getVariantsList() here your list
            List<Varient> list =getVariantsList();
            Set<Varient> set = new LinkedHashSet<Varient>(list);
        }
}

Comments

0
public class Variant {

    private String variant;
    private String quantity;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((variant == null) ? 0 : variant.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;
        Variant other = (Variant) obj;
        if (variant == null) {
            if (other.variant != null)
                return false;
        } else if (!variant.equals(other.variant))
            return false;
        return true;
    }

    public String getVariant() {
        return variant;
    }

    public void setVariant(String variant) {
        this.variant = variant;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public static void main(String[] args) {
        // HashSet<Variant> set = new HashSet<>();


        // LinkedHashSet<Variant> linkedSet = new LinkedHashSet<>(); // stores
        // in input order

        /*
         * You can use treeset to store data in custom order, in this case
         * lexicographically
         */
        TreeSet<Variant> treeSet = new TreeSet<>(new VariantComparator());
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.