Apple.java
package com.jue.test;
public class Apple {
int id;
String name;
public Apple(int newid, String newname) {
id = newid;
name = newname;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.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;
Apple other = (Apple) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
MainTest.java
package com.jue.test;
import java.util.ArrayList;
import java.util.List;
public class MainTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Apple> lists = new ArrayList<Apple>();
lists.add(new Apple(1, "a"));
lists.add(new Apple(2, "b"));
lists.add(new Apple(3, "c"));
System.out.print(lists.contains(new Apple(1, "a")));
}
}
result:
true.