0

I have 2 classes.

public class klass1 {

String bir;
String myID;

klass1(String bir, String myID)
{
    this.bir=bir;
    this.myID=myID;
}

}

.

import java.util.*;

public class dd {

public static void main(String[] args) {

    ArrayList<Object> ar=new ArrayList();

    ar.add(new klass1("wer","32"));
    ar.add(new klass1("das","23"));
    ar.add(new klass1("vz","45"));
    ar.add(new klass1("yte","12"));
    ar.add(new klass1("rwwer","43"));

    ar.remove(new klass1("vz","45"));//it's not worked!!!

    System.out.println(ar.size());

}

}

What I want is removing or getting an object from array list with object's second attribute. How can I do that? Is there an easy way for it?

4
  • 2
    Make your klass1 implements both equals and hashCode methods. For this case, the equals method should resemble how to detect if two different object instances are equals, i.e., which fields when compared together mean both instances have the same value. Commented Nov 11, 2013 at 14:25
  • Isn't it index based remove enough for your case? Commented Nov 11, 2013 at 14:26
  • 1
    And please use a capital first letter on class names! So Klass1 or Dd are more conventional. Commented Nov 11, 2013 at 14:27
  • @MGPJ Unfortunately no. There is a string ID in my class and I must use it. Commented Nov 11, 2013 at 14:28

3 Answers 3

2

Just implement the equals method in the class Klass1.

public class Klass1 {

    String bir;
    String myID;

    Klass1(String bir, String myID)
    {
        this.bir=bir;
        this.myID=myID;
    }

    public boolean equals(Object o){
        if(o instanceof Klass1)
            return ((Klass1)o).myID.equals(myID);
        else
            return false;
    }

}

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

1 Comment

You can skip hashcode but you should not. This will however cause issues if you use this object in something like a hashmap, where then hashcode will not be equal on otherwise equal objects.
0

Its because you are trying to delete a new object which isnt in the arraylist. When you use new klass1("vz","45") you are creating a new instance of this class which isnt in the arraylist.

1 Comment

yes I know why its not working but I have to do it with a way.
0

What the system does internally is to compare those classes using equals. Why this doesn't work is explained in the following code:

Object o1 = new Object();
Object o2 = new Object();
System.out.println(o1 == o2); // false, obviously
System.out.println(o1.equals(o2)); // false
System.out.println(o1); // java.lang.Object@17046822
System.out.println(o2); // java.lang.Object@22509bfc

You can tell by the number following the @ that these objects have a different hash values, and this is what the equals function of Object does check.

This is relevant for your klass, because unless you overwrite equals, you will use the equals of Object. And if you implement equals you should always implement hashcode as well. Because both tell you something about whether or not two objects are the "same", and if the one says something else than the other, some part of your code might get confused.

How to properly implement equals for your class:

@Override
public int hashCode() {
  int hash = 7;
  hash = 17 * hash + Objects.hashCode(this.bir);
  hash = 17 * hash + Objects.hashCode(this.myID);
  return hash;
}

@Override
public boolean equals(Object obj) {
  if (obj == null) {
    return false;
  }
  if (getClass() != obj.getClass()) {
    return false;
  }
  final klass1 other = (klass1) obj;
  if (!Objects.equals(this.bir, other.bir)) {
    return false;
  }
  if (!Objects.equals(this.myID, other.myID)) {
    return false;
  }
  return true;
}

This can be done in most IDEs btw with a shortcut (i.E. alt-insert in Netbeans). Note that I did this in Java 7 using Objects. If you are in Java 6, you need to manually type
(a == b) || (a != null && a.equals(b)); with the appropriate objects to compare.

Creating a proper hashcode is not always trivial, for more complex objects you might want to read a bit about hashcodes first. For simple objects: multiply primes with something.

The equals method is usually trivial, it is just important to first check for null and for class equality. This is often forgotten by programmers and a common source for NullPointerExceptions and ClassCastExceptions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.