English 中文(简体)
1. 草原中压倒一切的方法
原标题:Methods override in hashset
  • 时间:2011-10-17 16:51:49
  •  标签:
  • java

我开发了一个能够使用洗衣概念的申请。 我必须放弃平等、传统和习惯做法。 我完全知道为什么要凌驾既定方法之上。 愿有人告诉我,在不凌驾于上述方法的情况下发生了什么。

最佳回答

For example read this: http://www.javamex.com/tutorials/collections/hash_code_equals.shtml

There is plenty of discussions about the subject elsewhere (I recommend Effective Java book).

你们不需要超越规范,而是奖金。

Basically if you don t override equals, you will not be able to get things from the collections the way you would expect. E.g. if your String class didn t have equals implemented in a meaningful way, collection.get("abc") and collection.get(new String("abc")) would give you different results.

问题回答

如何

public class MyHashSet<E> extends HashSet<E> {
    @Override public int hashCode() { ... }
    @Override public String toString() { ... }
    @Override public boolean equals(Object o) { ... }
}

对于希望存放在哈希特的物体,也适用同样的办法。

public class MyObject {
    @Override public int hashCode() { ... }
    @Override public String toString() { return "MyObject"; }
    @Override public boolean equals(Object o) { return this.equals(o); }
}

// Sample usage
HashSet<MyObject> set = ...
set.add(new MyObject());

假设你想要在而不是在上推翻你将使用的物体。 这样做的理由本身是,你将获得在上放置物体的预期成果。 <代码>hashCode(,特别是对于HashSet的正常运作至关重要, Equals>。 推翻<代码>至String()的主要原因是,你获得一些代表,对这个物体而言是有意义的,而不是默认的<代码>。 目标方法版本。

Essentially, if you don t override the methods, HashSet won t do what you expect. For example, say I have a class Foo where I haven t overriden the methods.

public class Foo {
    private int number;

    public Foo(int newNumber) {
       number = newNumber;
    }

    public int getNumber() {
        return number;
    }
}

I create two objects Foo1 and Foo2 that are equal in all respects, but I haven t overriden equals() or hashCode().

Foo foo1 = new Foo(10);
Foo foo2 = new Foo(10);

Java将不认为这些是平等的,尽管我可以认为这些是平等的,因为我没有解释如何通过凌驾于<条码>上的不平等()来对其进行比较。 如果我勾画了原样的价值,我就看到分配给不同物体的不同分类值,例如:

foo1 = 1671711
foo2 = 11394033

因此,如果在<代码>HashSet上添加这些内容的话,如果我真的只想把这两个物体放在一起,那将令人高兴。

Here,是关于使用这些方法的面谈问题的类似短语。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签