java - 自定义哈希表删除功能
问题描述
所以我有这个哈希表自定义实现。我的自定义哈希表类中的删除方法有问题。另外我是 SOF 的新手,所以如果我可以为我的问题做出任何改进,请告诉我。
它的表有一个数组列表
private ArrayList<BST<Song>> Table;
另一个哈希表,我使用这个哈希表中的整数作为表
private Hashtable<String, Integer> map;
hashtable.java 类中的 remove 方法。由于某种原因,它不会删除所有适用的 Song obj,这是我遇到问题的部分。
public boolean remove(Song song) throws NullPointerException {
if (song == null)
throw new NullPointerException("Value is null, unable to remove!");
else if (!this.contains(song)) {
System.out.println("\nSong not found in the hashtable. Unable to remove!\n");
return false;
}
Set<String> keys = map.keySet();
for (String key : keys) {
try {
Song song1 = Table.get(map.get(key)).search(song, false, new NameComparator());
if (song1.compareTo(song) == 0) {
Table.get(map.get(key)).remove(song, new NameComparator());
songsDeleted++;
numElements--;
System.out.println("DELETED");
}
}
catch(Exception e) {
}
}
return true;
}
BST.java 的一部分
/**
* Removes a value from the BST
*
* @param data the value to remove
* @param c the Comparator indicating how data in the tree is organized Note:
* updates nothing when the element is not in the tree
*/
public void remove(T data, Comparator<T> c) throws NoSuchElementException {
if ( isEmpty())
throw new NoSuchElementException("Unable to remove data, data does not exist");
else if ( getSize() == 1)
root = null;
else
remove(data, root, c);
}
private Node remove(T data, Node root, Comparator<T> c) {
/* Base Case: If the tree is empty */
if (root == null)
return root;
/* Otherwise, recur down the tree */
if (c.compare(data, root.data) < 0)
root.left = remove(data, root.left, c);
else if (c.compare(data, root.data) > 0)
root.right = remove(data, root.right, c);
// if key is same as root's
// key, then This is the
// node to be deleted
else {
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node with two children: Get the inorder
// successor (smallest in the right subtree)
root.data = findMin(root.right);
// Delete the inorder successor
root.right = remove(root.data, root.right, c);
}
return root;
BST 的比较器
public class NameComparator implements Comparator<Song> {
@Override
public int compare(Song song1, Song song2) {
if (song1.hashCode() == song2.hashCode())
return 0;
else if (song1.hashCode() > song2.hashCode())
return 1;
else
return -1;
}
}
Song.java 的一部分
public class Song implements Comparable<Song> {
private String name;
private String composer;
private int songLength;
private int releaseYear;
private String lyrics;
//getterr setter
@Override
public int hashCode() {
String key = name.toLowerCase() + composer.toLowerCase();
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += (int) key.charAt(i);
}
return sum;
}
// used to compare song obj to song obj
@Override
public int compareTo(Song o) {
if (o == this) {
return 0;
} else {
if (this.hashCode() == o.hashCode())
return 0;
else if (this.hashCode() > o.hashCode())
return 1;
else
return -1;
}
}
}
解决方案
推荐阅读
- javascript - 在 this.props.dispatch(push(path)) 上没有发生 React 转换
- javascript - 替代粘性位置?
- azure-devops - 为什么我在 Azure DevOps Build Pipeline 中的测试会运行两次?
- google-cloud-firestore - 是否可以在 Airflow 中使用 datastore_export_operator 导出 Firestore 备份?
- java - 打印具有相同值的变量名称
- css - 即使文本区域没有焦点,也能指示选定的文本
- github - 为分支中的每个提交在没有 vm 的情况下在 codeship 中运行命令
- java - 在Java中,一个类可以引用另一个包含它的类的实例吗?
- chat - Hangouts Chat:获取线程中的所有消息
- angular - 角度 6:在同一个组件中使用相同的 html 标记两次