编写我自己的 'TreeMap' class - 扩展什么 and/or 实现?
Writing my own 'TreeMap' class - what to extend and/or implement?
对于作业,我被要求 "Create a class called TreeMap
that uses the BinarySearchTree
class to implement a map"。我们得到了一组接口和 classes 来扩展和实现。
作业的第一步要求"Create an inner class called TreeEntry
to store the entries. Include a constructor and any mandatory methods"。
我们得到的接口包括BinaryTree<T>
、Entry<K, V>
、Map<K, V>
等等。
我们得到的 class 是 BinarySearchTree<T extends Comparable<T>> implements Iterable<T>
等等。
我正在努力确定如何申报我的 TreeMap
。它应该扩展 BinarySearchTree
,还是实施 Map
,或两者兼而有之?
到目前为止,我想出的是 public class TreeMap<K, V> implements Map<Comparable<K>, V>
然后将创建 TreeMap
嵌套 class,但我不知道我会在哪里 "use the BinarySearchTree
class to implement the map"?我是否在我的 TreeMap
class 中创建了一个新的 BinarySearchTree<TreeEntry<K, V>>
?
编辑
这是我到目前为止写的:
public class TreeMap<K, V> implements Map<Comparable<K>, V> {
private class TreeEntry<K, V> implements Entry<K, V>, Comparable<K> {
private K key;
private V value;
public TreeEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(K otherKey) {
if (this.key.equals(otherKey)) {
return 0;
}
else if (this.key > otherKey) {
return 1;
}
else {
return -1;
}
}
@Override
public K getKey() {
// TODO Auto-generated method stub
return null;
}
@Override
public V getValue() {
// TODO Auto-generated method stub
return null;
}
@Override
public V setValue(V value) {
// TODO Auto-generated method stub
return null;
}
}
}
我认为我没有正确实施 Comparable<K>
,因为我收到 else if (this.key > otherKey)
行的错误,表示 The operator > is undefined for the argument type(s) K, K
使用 BinarySearchTree 作为实现目前是一项任务。它可能是您当前的想法或某人关于如何做的建议。如果您从它继承并且在以后当成千上万的客户使用您的 TreeMap 并且您认为 BinarySearchTree 不是完美的实现时,那么您将无法再更改它。
这是因为有人可能将其用作
BinarySearcTree<X,Y> map = new TreeMap<>();
现在呢?您受困于 BinarySearchTree 的继承。但是,如果您 "only" 实现了 Map,那么您可以随时更改 class 的实现而不会给客户带来问题。
对于作业,我被要求 "Create a class called TreeMap
that uses the BinarySearchTree
class to implement a map"。我们得到了一组接口和 classes 来扩展和实现。
作业的第一步要求"Create an inner class called TreeEntry
to store the entries. Include a constructor and any mandatory methods"。
我们得到的接口包括BinaryTree<T>
、Entry<K, V>
、Map<K, V>
等等。
我们得到的 class 是 BinarySearchTree<T extends Comparable<T>> implements Iterable<T>
等等。
我正在努力确定如何申报我的 TreeMap
。它应该扩展 BinarySearchTree
,还是实施 Map
,或两者兼而有之?
到目前为止,我想出的是 public class TreeMap<K, V> implements Map<Comparable<K>, V>
然后将创建 TreeMap
嵌套 class,但我不知道我会在哪里 "use the BinarySearchTree
class to implement the map"?我是否在我的 TreeMap
class 中创建了一个新的 BinarySearchTree<TreeEntry<K, V>>
?
编辑
这是我到目前为止写的:
public class TreeMap<K, V> implements Map<Comparable<K>, V> {
private class TreeEntry<K, V> implements Entry<K, V>, Comparable<K> {
private K key;
private V value;
public TreeEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(K otherKey) {
if (this.key.equals(otherKey)) {
return 0;
}
else if (this.key > otherKey) {
return 1;
}
else {
return -1;
}
}
@Override
public K getKey() {
// TODO Auto-generated method stub
return null;
}
@Override
public V getValue() {
// TODO Auto-generated method stub
return null;
}
@Override
public V setValue(V value) {
// TODO Auto-generated method stub
return null;
}
}
}
我认为我没有正确实施 Comparable<K>
,因为我收到 else if (this.key > otherKey)
行的错误,表示 The operator > is undefined for the argument type(s) K, K
使用 BinarySearchTree 作为实现目前是一项任务。它可能是您当前的想法或某人关于如何做的建议。如果您从它继承并且在以后当成千上万的客户使用您的 TreeMap 并且您认为 BinarySearchTree 不是完美的实现时,那么您将无法再更改它。
这是因为有人可能将其用作
BinarySearcTree<X,Y> map = new TreeMap<>();
现在呢?您受困于 BinarySearchTree 的继承。但是,如果您 "only" 实现了 Map,那么您可以随时更改 class 的实现而不会给客户带来问题。