哈希表比较器问题
大家好,我从来没有写过比较器 b4,我遇到了一个真正的问题。我创建了一个哈希表。
Hashtable <String, Objects> ht;
有人可以展示如何为哈希表编写比较器吗?我见过的例子超越了等于和一切,但我根本不知道。下面的代码不是我的,而是我发现的一个例子,哈希表中的关键部分意味着我不能像这样做,我猜。
public class Comparator implements Comparable<Name> {
private final String firstName, lastName;
public void Name(String firstName, String lastName) {
if (firstName == null || lastName == null)
throw new NullPointerException();
this.firstName = firstName;
this.lastName = lastName;
}
public String firstName() { return firstName; }
public String lastName() { return lastName; }
public boolean equals(Object o) {
if (!(o instanceof Name))
return false;
Name n = (Name)o;
return n.firstName.equals(firstName) &&
n.lastName.equals(lastName);
}
public int hashCode() {
return 31*firstName.hashCode() + lastName.hashCode();
}
public String toString() {
return firstName + " " + lastName;
}
public int compareTo(Name n) {
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp :
firstName.compareTo(n.firstName));
}
}
Hi guys i've never written a comparator b4 and im having a real problem. I've created a hashtable.
Hashtable <String, Objects> ht;
Could someone show how you'd write a comparator for a Hashtable? the examples i've seen overide equals and everything but i simply dont have a clue. The code below is not mine but an example i found, the key thing in hashtables means i cant do it like this i guess.
public class Comparator implements Comparable<Name> {
private final String firstName, lastName;
public void Name(String firstName, String lastName) {
if (firstName == null || lastName == null)
throw new NullPointerException();
this.firstName = firstName;
this.lastName = lastName;
}
public String firstName() { return firstName; }
public String lastName() { return lastName; }
public boolean equals(Object o) {
if (!(o instanceof Name))
return false;
Name n = (Name)o;
return n.firstName.equals(firstName) &&
n.lastName.equals(lastName);
}
public int hashCode() {
return 31*firstName.hashCode() + lastName.hashCode();
}
public String toString() {
return firstName + " " + lastName;
}
public int compareTo(Name n) {
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp :
firstName.compareTo(n.firstName));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
比较器会告诉您两个项目中哪一个更大。如果这对您的哈希表有意义,那么只有您可以说出其含义是什么。以这种方式比较两个哈希表是非常不寻常的。
A Comparator will tell you which of two items is larger. If this has meaning for your HashTable, only you can say what the meaning is. It would be very unusual to want to compare two HashTables in this way.
那不是 Comparator 类。这是一个实现 Comparable 的 Name 类。
Hashtable 和 Hashmap 不使用 Comparator 或 Comparable。如果您想要排序键,请使用 TreeMap。
That's not a Comparator class. That's a Name class that implements Comparable.
Hashtable and Hashmap don't use either Comparator or Comparable. If you want sorted keys use a TreeMap.
比较器用于对列表进行排序。
Hashtable
(注意大小写)不按其元素排序。您可以通过迭代表的键来对表进行排序(我猜想是在您想要对其键进行排序的情况下)并将它们放入List
中。接下来要做的就是对List
进行排序并迭代List
,并使用Hashtable
中的get
code> 获取其关联值。下面是一个示例(使用
HashMap
,因为它与 Java 集合的其余部分集成得更好。HashMap
本质上与Hashtable
相同。 ):列表
keys
的顺序由匿名Comparator
类排序。它将按字母顺序排序,这是字符串的默认值。您可以使用自己的密钥对象,就像您提到的那样。如果您没有在此关键对象中实现Comparator,那么您可以提供,如上面的示例所示。否则,您可以通过调用以下命令来使用默认的 Comparator:这将使用 Comparator 的类实现。如果它没有实现
Comparator
,那么它将抛出异常(因为它将转换为Comparator
)Comparators are used to sort a list. A
Hashtable
(note the case) is not ordered by its elements. You can order a table by iterating over its keys (in the case you'd want to order on its keys, I presume) and put them in aList
. The next thing to do is to sort theList
and iterate over theList
, and use aget
out of theHashtable
to get its associated value.Here is an example (using
HashMap
, since it's more integrated with the rest of the Java Collections. AHashMap
is essentially the same asHashtable
.):The order of the list
keys
is sorted by the anonymousComparator
class. It will sort alphabetically, as is the default for Strings. You can use your own key object, like you mentioned. If you don't implementComparator
in this key object, then you can supply, as in the above example. Else you can use the defaultComparator
by calling:Which will use the classes implementation of
Comparator
. If it does not implementComparator
, then it will throw an exception (since it will cast to aComparator
)