由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个java List的问题
相关主题
问个java hashcode的题弱问一道G题
leetcode Parlindrome Partition run time error刚刚结束的linkedIn电面
text justification 有人ac吗saleforce 店面,攒人品吧。
隔壁讨论FB变态面试官,请教一下leetcode 301题怎么解最优?Linked电面分享,挺好的题 应该已挂
Interleave Strings那个题目有O(n)时间 O(1)空间算法么?FB Onsite新题,有人能看看吗?
一道电面题,分享下, 这个题应该用哪几个data structure?问一个面试题
Permutation leetcode-两道题,祝大家新年快乐!
新鲜Amazon面经(附参考答案) 顺便求各种大公司refer那道H2O的题
相关话题的讨论汇总
话题: foo话题: string话题: equals话题: null话题: boolean
进入JobHunting版参与讨论
1 (共1页)
k*******t
发帖数: 202
1
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Foo {
String name;
int number;

List friends;
public boolean equals(Object obj)
{
System.out.println(friends);
System.out.println(test.friends);
System.out.println(test.name);
System.out.println(this.name);

return number == test.number
&& (name == test.name
|| (name != null && name.equals(test.name)))
&& ((friends !=null) && friends.equals(test.friends));
}
}
public static void main(String args[])
{
Foo a = new Foo();
Foo b = new Foo();
a.number =10;
b.number = 10;
a.name = "test";
b.name = "testme";

boolean test = a.equals(b);
a.friends =new ArrayList();
b.friends =new ArrayList();
a.friends.add("Jack");
a.friends.add("Paul");
b.friends.add("Jack");
b.friends.add("Paul");
System.out.println(a.friends.equals(b.friends));
System.out.println(test);
}
想override Objects的equals 函数,但是跑了一下程序,发现调用a.equals(b)的时候
, a.friends 和b.friends的值都没有传进去。高手请指点,多谢了!
d**e
发帖数: 6098
2
大致上,如果要override Object.equals函数,可以是
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!(obj instanceof Foo)) {
return false;
}
Foo other = (Foo) obj;
boolean result = this.number == other.number;
if(!result) {
return false;
} else {
if(this.name != null) {
result = this.name.equals(other.name);
} else {
result = (other.name == null);
}
if(!result) {
return false;
} else {
// other object members..
....
}
}
// 如果可以调用其它library的话就简单很多
// 我比较喜欢用apache commns的EuqalsBuilder
// http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/builder/EqualsBuilder.html
}
下面code里还有些回复。

test是什么?
对String name,== 是identity equal,我觉得这里用不好。
但equals没有重新call,所以这个是friends.add之前的值。

【在 k*******t 的大作中提到】
: import java.util.ArrayList;
: import java.util.HashMap;
: import java.util.List;
: public class Foo {
: String name;
: int number;
:
: List friends;
: public boolean equals(Object obj)
: {

k*******t
发帖数: 202
3
谢谢楼上的答复。
请问下,用instanceof有什么弊端吗?
w**z
发帖数: 8232
4
No static checking, compiler and tools cannot help you, and it's slow.

【在 k*******t 的大作中提到】
: 谢谢楼上的答复。
: 请问下,用instanceof有什么弊端吗?

k*******t
发帖数: 202
5

相比之下,用if(this == null || this.getClass()!= obj.getClass()) 会安全一些
吗?

【在 w**z 的大作中提到】
: No static checking, compiler and tools cannot help you, and it's slow.
w**z
发帖数: 8232
6
I meant in general, you should avoid using instanceof, try to use override
method. But implementing equals is the exception. Sorry about the confusion.

【在 k*******t 的大作中提到】
:
: 相比之下,用if(this == null || this.getClass()!= obj.getClass()) 会安全一些
: 吗?

1 (共1页)
进入JobHunting版参与讨论
相关主题
那道H2O的题Interleave Strings那个题目有O(n)时间 O(1)空间算法么?
问2道面试题一道电面题,分享下, 这个题应该用哪几个data structure?
这段代码啥意思?看了半天没看懂。郁闷中~~~~~~~~~~Permutation leetcode-
java没有指针真麻烦新鲜Amazon面经(附参考答案) 顺便求各种大公司refer
问个java hashcode的题弱问一道G题
leetcode Parlindrome Partition run time error刚刚结束的linkedIn电面
text justification 有人ac吗saleforce 店面,攒人品吧。
隔壁讨论FB变态面试官,请教一下leetcode 301题怎么解最优?Linked电面分享,挺好的题 应该已挂
相关话题的讨论汇总
话题: foo话题: string话题: equals话题: null话题: boolean