-2

I have a custom class called alphabets.

public class Alphabets {
    String alp;

    public Alphabets(String alp) {
        this.alp = alp;
    }

    public String getAlp() {
        return alp;
    }
}

I have two ArrayLists containing custom objects or type (String & Alphabets) each.

ArrayList<Object> ar = new ArrayList<>();
ArrayList<Object> br = new ArrayList<>();

Alphabets a = new Alphabets("A");
Alphabets b = new Alphabets("B");
Alphabets b_clone = new Alphabets("B");

ar.add(a);
ar.add(b);

br.add(b_clone);

System.out.println(ar.contains(br.get(0)));

The output is false which technically correct but the actual value contained in the objects are equal ("B" in this case).This of course is just a snippet of the code.I need to check two entire ArrayLists to see if one lists contains objects from the other list and therefore cannot use the inbuilt .contains function. Is there a quick method or any solution actually for solving this?

Balduz
  • 3,450
  • 15
  • 32
Jude Fernandes
  • 6,969
  • 9
  • 43
  • 82

2 Answers2

1

Reading from the doc of the List.contains method

Returns true if this list contains the specified element. More formally,
returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

so basicly all you are in need to do is overriding the equals and hashcode method for the Alphabets class.

public class Alphabets {
    String alp;

    public Alphabets(String alp) {
        this.alp = alp;
    }

    public String getAlp() {
        return alp;
    }

    // Override the equals method as you need it and contains will be able to use it.
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Alphabets) {
            Alphabets aObj = (Alphabets) obj;
            //
            return aObj.getAlp().equals(getAlp());

        } else {
            return false;
        }
    }

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

Now you could build an intersection of both ArrayLists and could simply check the size of the ArrayList like this:

public static void main(String args[]){  
    ArrayList<Object> ar = new ArrayList<>();
    ArrayList<Object> br = new ArrayList<>();



    Alphabets a = new Alphabets("A");
    Alphabets b = new Alphabets("B");
    Alphabets b_clone = new Alphabets("B");

    ar.add(a);
    ar.add(b);

    br.add(b_clone);

    ArrayList<Object> ar2 = (ArrayList<Object>) ar.clone();

    ar2.retainAll(br);

    // Size 0 = no elements are equal
    // Size > 0 = equal elemts found
    System.out.println(ar2.size());

}

Edit: on a side node, don´t use rawtypes

SomeJavaGuy
  • 7,137
  • 2
  • 18
  • 33
  • The code worked and i got the output i wanted but could you explain what exactly is happening in the hashcode and equals function here since i have never overriden these methods before. – Jude Fernandes Sep 01 '15 at 15:06
  • @JudeFernandes rather read through [this](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java). I guess it´s better written as i could. if you want it in short words, the equal method is the standard method to compare two objects(it usualy checks the hashcode of these two in the normal object implementation), if you overwrite it, you can compare two objects on your own behavior. The hascode method is there because if `A.equals(B)`, then `A.getHashcode == B.Hashcode` in terms of logic – SomeJavaGuy Sep 01 '15 at 17:03
1

Override hashCode and equal method

public class Alphabets {
    String alp;

    public Alphabets(String alp) {
    this.alp = alp;
    }

    public String getAlp() {
    return alp;
    }

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

Now

public class TestProgram {

    public static void main(String[] args){
        ArrayList<Object> ar = new ArrayList<>();
        ArrayList<Object> br = new ArrayList<>();

        Alphabets a = new Alphabets("A");
        Alphabets b = new Alphabets("B");
        Alphabets b_clone = new Alphabets("B");

        ar.add(a);
        ar.add(b);

        br.add(b_clone);

        System.out.println(ar.contains(br.get(0))); // Prints true
    }
}
Syam S
  • 8,071
  • 1
  • 22
  • 35
  • 1
    Or at the end of the `equals` method, just do `return alp==null ? other.alp==null : alp.equals(other.alp)`, or even just [`return Objects.equals(alp, other.alp)`](http://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object-) – Andy Brown Sep 01 '15 at 16:36
  • @Andy: Good Point, but that was just an auto-generated equals & hashCode by eclipse. I just wanted to convey the idea.. ;-) – Syam S Sep 01 '15 at 16:42