我运行了跟随脚本(java),它给了我奇怪的结果.有没有人可以帮忙解释一下?
import java.util.Objects;
import org.apache.log4j.Logger;
public class CacheTester {
private static final Logger log = Logger.getLogger(CacheTester.class);
@Test
public void hashCodeTest() {
for (int i = 0; i < 50; i++) {
// if I remove the third parameter, it works fine
log.info(Objects.hash("getDemoCache", "1", new int[]{1, 2}));
}
}
}
Run Code Online (Sandbox Code Playgroud)
日志结果(它们彼此不同):
//...
2015-04-29 17:43:20 INFO CacheTester:42 - 1431904540
2015-04-29 17:43:20 INFO CacheTester:42 - 1859187447
2015-04-29 17:43:20 INFO CacheTester:42 - -2146933580
2015-04-29 17:43:20 INFO CacheTester:42 - -2074242201
2015-04-29 17:43:20 INFO CacheTester:42 - …Run Code Online (Sandbox Code Playgroud) 对于以下课程,当我进行深复制时,将代码编写为
this.id = original.getId();
Run Code Online (Sandbox Code Playgroud)
在我的测试中,似乎没问题,因为当我想更新 UUID 字段时,我总是为它分配一个新的 UUID 实例(我找不到任何可以修改现有 UUID 实例的函数)。在这种情况下,这个复制的实例永远不会对原始实例产生副作用。
public class Container {
private Type type;
private UUID id;
private Container c;
// public constructors here
protected Container(Container original, boolean deepCopy) {
this.type = original.getType();
this.id = original.getId();
// for deep copy of id field, should I write as:
// this.id = new UUID(original.getId().getMostSignificantBits(), original.getId().getLeastSignificantBits());
if (original.getC() != null) {
this.c = deepCopy ? original.getC().deepCopy() : original.getC();
}
}
// getter and setter here
public Container deepCopy() { …Run Code Online (Sandbox Code Playgroud)