【发布时间】:2018-03-09 21:42:01
【问题描述】:
我(曾经)试图创建一个在插入过程中删除重复项的 LinkedList。这是我的代码:
LinkedList<WordInDocument> list = new LinkedList<WordInDocument>();
/**
* Insert a word.
*
* @param insertWord The word that'll be inserted for, the word and it's Part Of Speech.
* If you print "insertWord.word" you'll get the String of the word.
*/
public void insert (Word insertWord) {
ListIterator<WordInDocument> listIt = list.listIterator();
WordInDocument entry = new WordInDocument(insertWord);
if (list.isEmpty()) {
listIt.add(entry);
}
else {
while (listIt.hasNext()) {
//the iterator iterate with this if-statment,
//if the iterator finds a equal word, then break
if (listIt.next().wordInEntry.word.equalsIgnoreCase(insertWord.word)) {
break;
}
}
//only true if an equal word wasn't found, then ad the word at the end
if (!listIt.hasNext()) {
listIt.add (entry);
}
}
}
但如果输入很多,则执行此操作需要很长时间(大约 1 分钟)。他们有更好的方法在插入期间删除重复值吗?
编辑:
谢谢您的帮助。我通过使用我所说的“二进制插入”来解决它。这样,它也会在每次插入之后进行排序,这是我在最后一次插入之后要做的。这是我的代码:
WordInDocument[] list = new WordInDocument[MAX_INDEX];
int currentMaxIndex = 0;
/**
* Insert a word, it uses binary search to find where to put it, if the
* current word dosn't exists, then insert it, this can bee called "Binary insert".
* If the current word already exists, then ignore.
*
* @param insertword The word that'll be inserted for, the word and it's Part Of Speech.
* If you print "insertWord.word" you'll get the String of the word.
*/
public void insert(Word insertword) { // put element into array
WordInDocument entry = new WordInDocument(insertword);
//First element
if (list[0] == null) {
list[0] = entry;
currentMaxIndex++;
return;
}
int inputIndex = binaryInsert(insertword);
//It's at the end
if (list[inputIndex] == null) {
list[inputIndex] = entry;
currentMaxIndex++;
return;
}
//It's equal to another word
if (list[inputIndex].wordInEntry.word.equalsIgnoreCase(word.word)) {
return;
}
//It's between two entries
for (int i = currentMaxIndex; i > inputIndex; i--) { // move bigger ones one up.
list[i] = list[i - 1];
}
list[inputIndex] = entry;
currentMaxIndex++;
}
private int binaryInsert(Word word) {
int lowerBound = 0;
int upperBound = currentMaxIndex - 1;
int compareStrings = list[mid].wordInEntry.word.compareToIgnoreCase(word.word);
while (true) {
int mid = (upperBound + lowerBound) / 2;
if (lowerBound == mid) {
if (compareStrings > 0) {
return mid;
}
}
if (compareStrings < 0) {
lowerBound = mid + 1; // its in the upper
if (lowerBound > upperBound) {
return mid += 1;
}
} else if (lowerBound > upperBound) {
return mid;
} else {
upperBound = mid - 1; // its in the lower
}
}
}
现在需要 2 秒而不是 45 秒。
【问题讨论】:
-
为什么不直接使用
Set? -
只需使用
LinkedHashSet。 -
使用 set 或其他数据结构不要忘记为非原始类型实现 hashcode 和 equals
-
insertWord定义在哪里? -
如果重复最后一个元素,您的逻辑将失败。你应该
return而不是break,并在末尾无条件插入。
标签: java sorting linked-list insert duplicates