Skip to content

Apache Lucene open-source search software

Migrate linked list to more efficient datastructre (#15704)

Renato Haeberli7335eb0ad96d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c2cd2bac185..53c5a422df3 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -89,6 +89,8 @@ New Features
Improvements
---------------------
+* GITHUB#15704: Replace LinkedList with more efficient data structure. (Renato Haeberli)
+
* GITHUB#15682: Use ArrayDeque instead of LinkedList in CompoundWordTokenFilterBase.java. (Renato Haeberli)
* GITHUB#15568: Add markdown support for javadocs and modernize highlighting a bit. (Dawid Weiss)
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
index 7a389ca512a..97237664ae6 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
@@ -101,7 +101,7 @@ public abstract class CompoundWordTokenFilterBase extends TokenFilter {
public final boolean incrementToken() throws IOException {
if (!tokens.isEmpty()) {
assert current != null;
- CompoundToken token = tokens.remove();
+ CompoundToken token = tokens.poll();
restoreState(current); // keep all other attributes untouched
termAtt.setEmpty().append(token.txt);
offsetAtt.setOffset(token.startOffset, token.endOffset);
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/ShingleFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/ShingleFilter.java
index e9520815dad..3f2779ce82d 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/ShingleFilter.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/ShingleFilter.java
@@ -17,9 +17,10 @@
package org.apache.lucene.analysis.shingle;
import java.io.IOException;
+import java.util.ArrayDeque;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.Objects;
+import java.util.Queue;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@@ -60,7 +61,7 @@ public final class ShingleFilter extends TokenFilter {
* The sequence of input stream tokens (or filler tokens, if necessary) that will be composed to
* form output shingles.
*/
- private LinkedList<InputWindowToken> inputWindow = new LinkedList<>();
+ private Queue<InputWindowToken> inputWindow = new ArrayDeque<>();
/** The number of input tokens in the next output token. This is the "n" in "token n-grams". */
private CircularSequence gramSize;
@@ -290,7 +291,7 @@ public final class ShingleFilter extends TokenFilter {
}
}
if (!isAllFiller && builtGramSize == gramSize.getValue()) {
- inputWindow.getFirst().attSource.copyTo(this);
+ inputWindow.element().attSource.copyTo(this);
posIncrAtt.setPositionIncrement(isOutputHere ? 0 : 1);
termAtt.setEmpty().append(gramBuilder);
if (gramSize.getValue() > 1) {
@@ -416,7 +417,7 @@ public final class ShingleFilter extends TokenFilter {
private void shiftInputWindow() throws IOException {
InputWindowToken firstToken = null;
if (inputWindow.size() > 0) {
- firstToken = inputWindow.removeFirst();
+ firstToken = inputWindow.remove();
}
while (inputWindow.size() < maxShingleSize) {
if (null != firstToken) { // recycle the firstToken, if available
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymFilter.java
index 357e97480cd..4ec16027f1e 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymFilter.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymFilter.java
@@ -18,8 +18,9 @@
package org.apache.lucene.analysis.synonym.word2vec;
import java.io.IOException;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Queue;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.synonym.SynonymGraphFilter;
@@ -46,7 +47,7 @@ public final class Word2VecSynonymFilter extends TokenFilter {
private final Word2VecSynonymProvider synonymProvider;
private final int maxSynonymsPerTerm;
private final float minAcceptedSimilarity;
- private final LinkedList<TermAndBoost> synonymBuffer = new LinkedList<>();
+ private final Queue<TermAndBoost> synonymBuffer = new ArrayDeque<>();
private State lastState;
/**
@@ -76,7 +77,7 @@ public final class Word2VecSynonymFilter extends TokenFilter {
public boolean incrementToken() throws IOException {
if (!synonymBuffer.isEmpty()) {
- TermAndBoost synonym = synonymBuffer.pollFirst();
+ TermAndBoost synonym = synonymBuffer.poll();
clearAttributes();
restoreState(this.lastState);
termAtt.setEmpty();
@@ -91,9 +92,9 @@ public final class Word2VecSynonymFilter extends TokenFilter {
BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
bytesRefBuilder.copyChars(termAtt.buffer(), 0, termAtt.length());
BytesRef term = bytesRefBuilder.get();
- List<TermAndBoost> synonyms =
+ Collection<TermAndBoost> synonyms =
this.synonymProvider.getSynonyms(term, maxSynonymsPerTerm, minAcceptedSimilarity);
- if (synonyms.size() > 0) {
+ if (!synonyms.isEmpty()) {
this.lastState = captureState();
this.synonymBuffer.addAll(synonyms);
}
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java
index 68ea5ec555e..a0980480dba 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java
@@ -21,8 +21,9 @@ import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_BEAM_WIDTH;
import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_MAX_CONN;
import java.io.IOException;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Deque;
import org.apache.lucene.codecs.hnsw.DefaultFlatVectorScorer;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.KnnCollector;
@@ -35,7 +36,7 @@ import org.apache.lucene.util.hnsw.RandomVectorScorer;
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
/**
- * The Word2VecSynonymProvider generates the list of sysnonyms of a term.
+ * The Word2VecSynonymProvider generates the list of synonyms of a term.
*
* @lucene.experimental
*/
@@ -66,14 +67,14 @@ public class Word2VecSynonymProvider {
this.hnswGraph = builder.build(word2VecModel.size());
}
- public List<TermAndBoost> getSynonyms(
+ public Collection<TermAndBoost> getSynonyms(
BytesRef term, int maxSynonymsPerTerm, float minAcceptedSimilarity) throws IOException {
if (term == null) {
throw new IllegalArgumentException("Term must not be null");
}
- LinkedList<TermAndBoost> result = new LinkedList<>();
+ Deque<TermAndBoost> result = new ArrayDeque<>();
float[] query = word2VecModel.vectorValue(term);
if (query != null) {
RandomVectorScorer scorer =
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestFlattenGraphFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestFlattenGraphFilter.java
index 5f604586fc8..d7346faf95c 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestFlattenGraphFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/core/TestFlattenGraphFilter.java
@@ -20,7 +20,6 @@ package org.apache.lucene.analysis.core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.lucene.analysis.Analyzer;
@@ -728,7 +727,7 @@ public class TestFlattenGraphFilter extends BaseTokenStreamTestCase {
*/
public void testPathsNotLost() throws IOException {
int wordCount = random().nextInt(5) + 5;
- List<BytesRef> acceptStrings = new LinkedList<>();
+ List<BytesRef> acceptStrings = new ArrayList<>();
for (int i = 0; i < wordCount; i++) {
int wordLen = random().nextInt(5) + 5;
BytesRef ref = new BytesRef(wordLen);
@@ -815,167 +814,4 @@ public class TestFlattenGraphFilter extends BaseTokenStreamTestCase {
}
return accept;
}
-
- /*
- * This method checks if strings that lead to the accept state of the not flattened TokenStream
- * also lead to the accept state in the flattened TokenStream. This gets complicated when you
- * factor in holes. The FlattenGraphFilter will remove alternate paths that are made entirely of
- * holes. An alternate path of Holes is indistinguishable from a path that just has long
- * lengths(ex: testStrangelyNumberedNodes). Also alternate paths that end in multiple holes could
- * be interpreted as sequential holes after the branching has converged during flattening. This
- * leads to a lot of weird logic about navigating around holes that may compromise the accuracy of
- * this test.
- *
- * @param flattened flattened TokenStream
- * @param notFlattened not flattened TokenStream
- * @throws IOException on error creating Automata
- */
- /* private void checkGeneralization(TokenStream flattened, TokenStream notFlattened)
- throws IOException {
- TokenStreamToAutomaton tsta = new TokenStreamToAutomaton();
-
- List<LinkedList<Integer>> acceptStrings = getAcceptStrings(tsta.toAutomaton(notFlattened));
- checkAcceptStrings(acceptStrings, tsta.toAutomaton(flattened));
- flattened.close();
- notFlattened.close();
- }*/
-
- /*
- * gets up to 10000 strings that lead to accept state in the given automaton.
- *
- * @param automaton automaton
- * @return list of accept sequences
- */
- /* private List<LinkedList<Integer>> getAcceptStrings(Automaton automaton) {
- List<LinkedList<Integer>> acceptedSequences = new LinkedList<>();
- LinkedList<Integer> prefix = new LinkedList<>();
- // state 0 is always the start node
- // Particularly branching automatons can create lots of possible acceptable strings. limit to
- // the first 10K
- buildAcceptStringRecursive(automaton, 0, prefix, acceptedSequences, 10000);
- return acceptedSequences;
- }*/
-
- /*
- * @param automaton automaton to generate strings from
- * @param state state to start at
- * @param prefix string prefix
- * @param acceptedSequences List of strings build so far.
- * @param limit maximum number of acceptedSequences.
- */
- /*private void buildAcceptStringRecursive(
- Automaton automaton,
- int state,
- LinkedList<Integer> prefix,
- List<LinkedList<Integer>> acceptedSequences,
- int limit) {
- if (acceptedSequences.size() == limit) {
- return;
- }
- if (automaton.isAccept(state)) {
- acceptedSequences.add(new LinkedList<>(prefix));
- return;
- }
- int numTransitions = automaton.getNumTransitions(state);
- Transition transition = new Transition();
- for (int i = 0; i < numTransitions; i++) {
- automaton.getTransition(state, i, transition);
- // min and max are the same transitions made from TokenStreamToAutomaton
- prefix.addLast(transition.min);
- buildAcceptStringRecursive(automaton, transition.dest, prefix, acceptedSequences, limit);
- prefix.removeLast();
- }
- }
-
- private void checkAcceptStrings(List<LinkedList<Integer>> acceptSequence, Automaton automaton) {
- for (LinkedList<Integer> acceptString : acceptSequence) {
- assertTrue(
- "String did not lead to accept state " + acceptString,
- recursivelyValidateWithHoles(acceptString, 0, automaton));
- }
- }
-
- private boolean recursivelyValidateWithHoles(
- LinkedList<Integer> acceptSequence, int state, Automaton automaton) {
- if (acceptSequence.isEmpty()) {
- return automaton.isAccept(state);
- }
-
- Integer curr = acceptSequence.pop();
- int numTransitions = automaton.getNumTransitions(state);
- Transition transition = new Transition();
-
- boolean accept = false;
- // Automaton can be NFA, so we need to check all matching transitions
- for (int i = 0; i < numTransitions; i++) {
- automaton.getTransition(state, i, transition);
- if (transition.min <= curr && transition.max >= curr) {
- accept = recursivelyValidateWithHoles(acceptSequence, transition.dest, automaton);
- // Factoring in flattened graphs the space covered by a hole may be bigger in the flattened
- // graph.
- // Try consuming more steps with holes.
- if (accept == false
- && transition.min == TokenStreamToAutomaton.HOLE
- && transition.max == TokenStreamToAutomaton.HOLE) {
- acceptSequence.push(TokenStreamToAutomaton.HOLE);
- acceptSequence.push(TokenStreamToAutomaton.POS_SEP);
- accept = recursivelyValidateWithHoles(acceptSequence, transition.dest, automaton);
- acceptSequence.pop();
- acceptSequence.pop();
- }
- } else if (transition.min == TokenStreamToAutomaton.HOLE
- && transition.max == TokenStreamToAutomaton.HOLE
- && automaton.getNumTransitions(transition.dest) > 0) {
- //consume multiple holes in the automaton
- // clear POS_INC
- automaton.getTransition(transition.dest, 0, transition);
- acceptSequence.push(curr);
- accept = recursivelyValidateWithHoles(acceptSequence, transition.dest, automaton);
- acceptSequence.pop();
- } else if(curr == TokenStreamToAutomaton.HOLE) {
- //consume non-holes in the automaton with holes
- while (transition.min != TokenStreamToAutomaton.POS_SEP
- && automaton.getNumTransitions(transition.dest) > 0) {
- automaton.getTransition(transition.dest, 0, transition);
- }
- acceptSequence.push(curr);
- accept = recursivelyValidateWithHoles(acceptSequence, transition.dest, automaton);
- acceptSequence.pop();
- }
- if (accept) {
- break;
- }
- }
- // Flatten graph filter will remove side paths that are only Holes. Gaps may also change size as
- // graph is flattened.
- // Traverse over them if curr is a hole to make sure the gap is kept
- if (accept == false && curr == TokenStreamToAutomaton.HOLE && acceptSequence.size() > 0) {
- // get rid of the separator
- acceptSequence.pop();
-
- for (int i = 0; i < numTransitions; i++) {
- automaton.getTransition(state, i, transition);
- //advance to the next POS_SEP in automaton
- while (transition.min != TokenStreamToAutomaton.POS_SEP
- && automaton.getNumTransitions(transition.dest) > 0) {
- automaton.getTransition(transition.dest, 0, transition);
- }
- accept = recursivelyValidateWithHoles(acceptSequence, transition.dest, automaton);
- if (accept) {
- break;
- }
- }
-
- // might be multiple holes squashed under a one step path. Try burning remaining holes
- if (accept == false) {
- accept = recursivelyValidateWithHoles(acceptSequence, state, automaton);
- }
-
- acceptSequence.push(TokenStreamToAutomaton.POS_SEP);
- }
- acceptSequence.push(curr);
- return accept;
- } */
-
- // NOTE: TestSynonymGraphFilter's testRandomSyns also tests FlattenGraphFilter
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java
index 1d36d82a302..cc64f9fde02 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java
@@ -18,7 +18,7 @@
package org.apache.lucene.analysis.synonym.word2vec;
import java.io.IOException;
-import java.util.List;
+import java.util.Collection;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.TermAndVector;
@@ -59,12 +59,15 @@ public class TestWord2VecSynonymProvider extends LuceneTestCase {
BytesRef inputTerm = new BytesRef("a");
String[] expectedSynonyms = {"d", "e", "c", "b"};
- List<TermAndBoost> actualSynonymsResults =
+ Collection<TermAndBoost> actualSynonymsResults =
unit.getSynonyms(inputTerm, MAX_SYNONYMS_PER_TERM, MIN_ACCEPTED_SIMILARITY);
assertEquals(4, actualSynonymsResults.size());
- for (int i = 0; i < expectedSynonyms.length; i++) {
- assertEquals(new BytesRef(expectedSynonyms[i]), actualSynonymsResults.get(i).term());
+
+ int i = 0;
+ for (TermAndBoost actualSynonymsResult : actualSynonymsResults) {
+ assertEquals(new BytesRef(expectedSynonyms[i]), actualSynonymsResult.term());
+ i++;
}
}
@@ -78,13 +81,14 @@ public class TestWord2VecSynonymProvider extends LuceneTestCase {
Word2VecSynonymProvider unit = new Word2VecSynonymProvider(model);
BytesRef inputTerm = new BytesRef("a");
- List<TermAndBoost> actualSynonymsResults =
+ Collection<TermAndBoost> actualSynonymsResults =
unit.getSynonyms(inputTerm, MAX_SYNONYMS_PER_TERM, MIN_ACCEPTED_SIMILARITY);
BytesRef expectedFirstSynonymTerm = new BytesRef("b");
double expectedFirstSynonymBoost = 1.0;
- assertEquals(expectedFirstSynonymTerm, actualSynonymsResults.get(0).term());
- assertEquals(expectedFirstSynonymBoost, actualSynonymsResults.get(0).boost(), 0.001f);
+ TermAndBoost first = actualSynonymsResults.iterator().next();
+ assertEquals(expectedFirstSynonymTerm, first.term());
+ assertEquals(expectedFirstSynonymBoost, first.boost(), 0.001f);
}
@Test
@@ -98,7 +102,7 @@ public class TestWord2VecSynonymProvider extends LuceneTestCase {
Word2VecSynonymProvider unit = new Word2VecSynonymProvider(model);
BytesRef inputTerm = newBytesRef("a");
- List<TermAndBoost> actualSynonymsResults =
+ Collection<TermAndBoost> actualSynonymsResults =
unit.getSynonyms(inputTerm, MAX_SYNONYMS_PER_TERM, MIN_ACCEPTED_SIMILARITY);
assertEquals(0, actualSynonymsResults.size());
}
diff --git a/lucene/analysis/phonetic/src/java/org/apache/lucene/analysis/phonetic/DoubleMetaphoneFilter.java b/lucene/analysis/phonetic/src/java/org/apache/lucene/analysis/phonetic/DoubleMetaphoneFilter.java
index 6a950d84cb4..11f6ccfe7ab 100644
--- a/lucene/analysis/phonetic/src/java/org/apache/lucene/analysis/phonetic/DoubleMetaphoneFilter.java
+++ b/lucene/analysis/phonetic/src/java/org/apache/lucene/analysis/phonetic/DoubleMetaphoneFilter.java
@@ -17,7 +17,8 @@
package org.apache.lucene.analysis.phonetic;
import java.io.IOException;
-import java.util.LinkedList;
+import java.util.ArrayDeque;
+import java.util.Deque;
import org.apache.commons.codec.language.DoubleMetaphone;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
@@ -27,7 +28,7 @@ import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
/** Filter for DoubleMetaphone (supporting secondary codes) */
public final class DoubleMetaphoneFilter extends TokenFilter {
- private final LinkedList<State> remainingTokens = new LinkedList<>();
+ private final Deque<State> remainingTokens = new ArrayDeque<>();
private final DoubleMetaphone encoder = new DoubleMetaphone();
private final boolean inject;
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
@@ -52,7 +53,7 @@ public final class DoubleMetaphoneFilter extends TokenFilter {
if (!remainingTokens.isEmpty()) {
// clearAttributes(); // not currently necessary
- restoreState(remainingTokens.removeFirst());
+ restoreState(remainingTokens.poll());
return true;
}
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/BM25NBClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/BM25NBClassifier.java
index 1f56d19a714..07b2f354af5 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/BM25NBClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/BM25NBClassifier.java
@@ -17,10 +17,10 @@
package org.apache.lucene.classification;
import java.io.IOException;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
@@ -180,7 +180,7 @@ public class BM25NBClassifier implements Classifier<BytesRef> {
* @throws IOException if tokenization fails
*/
private String[] tokenize(String text) throws IOException {
- Collection<String> result = new LinkedList<>();
+ Collection<String> result = new ArrayDeque<>();
for (String textFieldName : textFieldNames) {
try (TokenStream tokenStream = analyzer.tokenStream(textFieldName, text)) {
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
index 9897f95c6a9..44b0478fc43 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
@@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
@@ -192,7 +191,7 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
* @throws IOException if tokenization fails
*/
protected String[] tokenize(String text) throws IOException {
- Collection<String> result = new LinkedList<>();
+ Collection<String> result = new ArrayList<>();
for (String textFieldName : textFieldNames) {
try (TokenStream tokenStream = analyzer.tokenStream(textFieldName, text)) {
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
index 1c34d870935..a2e34563075 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
@@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
@@ -154,7 +153,7 @@ public class SimpleNaiveBayesDocumentClassifier extends SimpleNaiveBayesClassifi
for (int i = 0; i < textFieldNames.length; i++) {
String fieldName = textFieldNames[i];
float boost = 1;
- List<String[]> tokenizedValues = new LinkedList<>();
+ List<String[]> tokenizedValues = new ArrayList<>();
if (fieldName.contains("^")) {
String[] field2boost = fieldName.split("\\^");
fieldName = field2boost[0];
@@ -180,7 +179,7 @@ public class SimpleNaiveBayesDocumentClassifier extends SimpleNaiveBayesClassifi
* @throws java.io.IOException If tokenization fails because there is a low-level I/O error
*/
protected String[] getTokenArray(TokenStream tokenizedText) throws IOException {
- Collection<String> tokens = new LinkedList<>();
+ Collection<String> tokens = new ArrayList<>();
CharTermAttribute charTermAttribute = tokenizedText.addAttribute(CharTermAttribute.class);
tokenizedText.reset();
while (tokenizedText.incrementToken()) {
diff --git a/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java b/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java
index fe28529b272..3ac54e448eb 100644
--- a/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java
+++ b/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java
@@ -23,7 +23,7 @@ import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.LinkedList;
+import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@@ -114,7 +114,7 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {
}
IndexReader reader = null;
- List<Classifier<BytesRef>> classifiers = new LinkedList<>();
+ List<Classifier<BytesRef>> classifiers = new ArrayList<>();
try {
Analyzer analyzer = new StandardAnalyzer();
if (index) {
@@ -316,7 +316,7 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new NamedThreadFactory(getClass().getName()));
- List<Future<String>> futures = new LinkedList<>();
+ List<Future<String>> futures = new ArrayList<>();
for (Classifier<BytesRef> classifier : classifiers) {
testClassifier(reader, testReader, service, futures, classifier);
}
diff --git a/lucene/core/src/java/org/apache/lucene/analysis/AutomatonToTokenStream.java b/lucene/core/src/java/org/apache/lucene/analysis/AutomatonToTokenStream.java
index 28493c168bc..4f3a3aaf768 100644
--- a/lucene/core/src/java/org/apache/lucene/analysis/AutomatonToTokenStream.java
+++ b/lucene/core/src/java/org/apache/lucene/analysis/AutomatonToTokenStream.java
@@ -18,8 +18,9 @@
package org.apache.lucene.analysis;
import java.io.IOException;
+import java.util.ArrayDeque;
import java.util.ArrayList;
-import java.util.LinkedList;
+import java.util.Deque;
import java.util.List;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
@@ -61,10 +62,10 @@ public class AutomatonToTokenStream {
throw new IllegalArgumentException("Start node has incoming edges, creating cycle");
}
- LinkedList<RemapNode> noIncomingEdges = new LinkedList<>();
+ Deque<RemapNode> noIncomingEdges = new ArrayDeque<>();
IntIntHashMap idToPos = new IntIntHashMap();
noIncomingEdges.addLast(new RemapNode(0, 0));
- while (noIncomingEdges.isEmpty() == false) {
+ while (!noIncomingEdges.isEmpty()) {
RemapNode currState = noIncomingEdges.removeFirst();
for (int i = 0; i < transitions[currState.id].length; i++) {
indegree[transitions[currState.id][i].dest] -= 1;
diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java
index df3e6458aed..224ac408e15 100644
--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java
+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java
@@ -17,12 +17,14 @@
package org.apache.lucene.index;
import java.io.Closeable;
+import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
+import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.lucene.store.AlreadyClosedException;
@@ -55,16 +57,16 @@ final class DocumentsWriterFlushControl implements Accountable, Closeable {
// eligible DWPTs and
// mark them as flushable putting them in the flushQueue ready for other threads (ie. indexing
// threads) to help flushing
- private final Queue<DocumentsWriterPerThread> flushQueue = new LinkedList<>();
+ private final Queue<DocumentsWriterPerThread> flushQueue = new ArrayDeque<>();
// only for safety reasons if a DWPT is close to the RAM limit
- private final Queue<DocumentsWriterPerThread> blockedFlushes = new LinkedList<>();
+ private final Queue<DocumentsWriterPerThread> blockedFlushes = new ArrayDeque<>();
// flushingWriters holds all currently flushing writers. There might be writers in this list that
// are also in the flushQueue which means that writers in the flushingWriters list are not
// necessarily
// already actively flushing. They are only in the state of flushing and might be picked up in the
// future by
// polling the flushQueue
- private final List<DocumentsWriterPerThread> flushingWriters = new ArrayList<>();
+ private final Set<DocumentsWriterPerThread> flushingWriters = new HashSet<>();
private double maxConfiguredRamBuffer = 0;
private long peakActiveBytes = 0; // only with assert
diff --git a/lucene/core/src/java/org/apache/lucene/search/NamedMatches.java b/lucene/core/src/java/org/apache/lucene/search/NamedMatches.java
index d0ec5c3a212..47e9e5cfa60 100644
--- a/lucene/core/src/java/org/apache/lucene/search/NamedMatches.java
+++ b/lucene/core/src/java/org/apache/lucene/search/NamedMatches.java
@@ -18,13 +18,14 @@
package org.apache.lucene.search;
import java.io.IOException;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
+import java.util.Queue;
import org.apache.lucene.index.LeafReaderContext;
/**
@@ -73,10 +74,10 @@ public class NamedMatches implements Matches {
/** Finds all {@link NamedMatches} in a {@link Matches} tree */
public static List<NamedMatches> findNamedMatches(Matches matches) {
List<NamedMatches> nm = new ArrayList<>();
- List<Matches> toProcess = new LinkedList<>();
+ Queue<Matches> toProcess = new ArrayDeque<>();
toProcess.add(matches);
while (toProcess.size() > 0) {
- matches = toProcess.remove(0);
+ matches = toProcess.remove();
if (matches instanceof NamedMatches) {
nm.add((NamedMatches) matches);
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
index 012612edce0..13384fcb892 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java
@@ -25,12 +25,12 @@ import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
@@ -54,7 +54,7 @@ public class TestDoc extends LuceneTestCase {
private Path workDir;
private Path indexDir;
- private LinkedList<Path> files;
+ private Collection<Path> files;
/**
* Set the test case. This test case needs a few text files created in the current working
@@ -72,7 +72,7 @@ public class TestDoc extends LuceneTestCase {
Directory directory = newFSDirectory(indexDir);
directory.close();
- files = new LinkedList<>();
+ files = new ArrayList<>();
files.add(createOutput("test.txt", "This is the first test file"));
files.add(createOutput("test2.txt", "This is the second test file"));
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java b/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java
index d8b196e04df..278df627b1d 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java
@@ -25,7 +25,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
@@ -499,7 +498,7 @@ public class TestKnnGraph extends LuceneTestCase {
private static void assertConnected(int[][] graph) {
List<Integer> nodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
- List<Integer> queue = new LinkedList<>();
+ List<Integer> queue = new ArrayList<>();
for (int i = 0; i < graph.length; i++) {
if (graph[i] != null) {
nodes.add(i);
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestMergePolicy.java
index b7b020af092..6c95b1e98cd 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestMergePolicy.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestMergePolicy.java
@@ -19,8 +19,8 @@ package org.apache.lucene.index;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -159,7 +159,7 @@ public class TestMergePolicy extends LuceneTestCase {
.getBytes(StandardCharsets.US_ASCII),
Collections.emptyMap(), // attributes
null /* indexSort */);
- final List<SegmentCommitInfo> segments = new LinkedList<>();
+ final List<SegmentCommitInfo> segments = new ArrayList<>();
segments.add(new SegmentCommitInfo(si, 0, 0, 0, 0, 0, StringHelper.randomId()));
ms.add(new MergePolicy.OneMerge(segments));
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
index f4f907a2b2c..a501caba197 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
@@ -18,8 +18,8 @@ package org.apache.lucene.index;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.lucene.store.Directory;
@@ -139,7 +139,7 @@ public class TestOneMergeWrappingMergePolicy extends LuceneTestCase {
.getBytes(StandardCharsets.US_ASCII),
Collections.emptyMap(), // attributes
null /* indexSort */);
- final List<SegmentCommitInfo> segments = new LinkedList<>();
+ final List<SegmentCommitInfo> segments = new ArrayList<>();
segments.add(new SegmentCommitInfo(si, 0, 0, 0, 0, 0, StringHelper.randomId()));
ms.add(new MergePolicy.OneMerge(segments));
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestConjunctionDISI.java b/lucene/core/src/test/org/apache/lucene/search/TestConjunctionDISI.java
index 3ade91fc60d..143df2172cc 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestConjunctionDISI.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestConjunctionDISI.java
@@ -19,9 +19,9 @@ package org.apache.lucene.search;
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
@@ -343,7 +343,7 @@ public class TestConjunctionDISI extends LuceneTestCase {
final int maxDoc = TestUtil.nextInt(random(), 100, 10000);
final int numIterators = TestUtil.nextInt(random(), 5, 10);
final FixedBitSet[] sets = new FixedBitSet[numIterators];
- final List<Scorer> scorers = new LinkedList<>();
+ final List<Scorer> scorers = new ArrayList<>();
for (int i = 0; i < numIterators; ++i) {
final FixedBitSet set = randomSet(maxDoc);
if (random().nextBoolean()) {
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java
index 622f5ea120d..c2c9ae3bf63 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java
@@ -17,7 +17,8 @@
package org.apache.lucene.search;
import java.io.IOException;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
@@ -61,7 +62,7 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
query1builder.add(new Term("body", "blueberry"));
query2builder.add(new Term("body", "strawberry"));
- LinkedList<Term> termsWithPrefix = new LinkedList<>();
+ List<Term> termsWithPrefix = new ArrayList<>();
// this TermEnum gives "piccadilly", "pie" and "pizza".
String prefix = "pi";
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPhrasePrefixQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestPhrasePrefixQuery.java
index 85d859ff942..53a078559b2 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestPhrasePrefixQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestPhrasePrefixQuery.java
@@ -17,7 +17,8 @@
package org.apache.lucene.search;
import java.io.IOException;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
@@ -63,7 +64,7 @@ public class TestPhrasePrefixQuery extends LuceneTestCase {
query1builder.add(new Term("body", "blueberry"));
query2builder.add(new Term("body", "strawberry"));
- LinkedList<Term> termsWithPrefix = new LinkedList<>();
+ List<Term> termsWithPrefix = new ArrayList<>();
// this TermEnum gives "piccadilly", "pie" and "pizza".
String prefix = "pi";
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestCollectionUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestCollectionUtil.java
index d9a93860551..97e1125e241 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestCollectionUtil.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestCollectionUtil.java
@@ -19,7 +19,6 @@ package org.apache.lucene.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -84,7 +83,7 @@ public class TestCollectionUtil extends LuceneTestCase {
CollectionUtil.timSort(list, Collections.reverseOrder());
// check that empty non-random access lists pass sorting without ex (as sorting is not needed)
- list = new LinkedList<>();
+ list = new ArrayList<>();
CollectionUtil.introSort(list);
CollectionUtil.timSort(list);
CollectionUtil.introSort(list, Collections.reverseOrder());
@@ -94,7 +93,7 @@ public class TestCollectionUtil extends LuceneTestCase {
public void testOneElementListSort() {
// check that one-element non-random access lists pass sorting without ex (as sorting is not
// needed)
- List<Integer> list = new LinkedList<>();
+ List<Integer> list = new ArrayList<>();
list.add(1);
CollectionUtil.introSort(list);
CollectionUtil.timSort(list);
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestTimSorterWorstCase.java b/lucene/core/src/test/org/apache/lucene/util/TestTimSorterWorstCase.java
index fab69c5f686..3f0bf865136 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestTimSorterWorstCase.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestTimSorterWorstCase.java
@@ -17,7 +17,7 @@
package org.apache.lucene.util;
import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
-import java.util.LinkedList;
+import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.LuceneTestCase.Nightly;
@@ -105,7 +105,7 @@ public class TestTimSorterWorstCase extends LuceneTestCase {
* merged (by <code>TimSort.mergeCollapse</code>) into an X_i that violates the invariant.
*/
private static List<Integer> runsWorstCase(int length, int minRun) {
- List<Integer> runs = new LinkedList<>();
+ List<Integer> runs = new ArrayList<>();
int runningTotal = 0, Y = minRun + 4, X = minRun;
diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java
index 2cd78209f28..93efbd4a046 100644
--- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java
+++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java
@@ -19,7 +19,6 @@ package org.apache.lucene.search.grouping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
-import java.util.LinkedList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
@@ -182,7 +181,7 @@ public abstract class GroupFacetCollector extends SimpleCollector {
* @return a list of facet entries to be rendered based on the specified offset and limit
*/
public List<FacetEntry> getFacetEntries(int offset, int limit) {
- List<FacetEntry> entries = new LinkedList<>();
+ List<FacetEntry> entries = new ArrayList<>();
int skipped = 0;
int included = 0;
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java
index 63e77b48657..a3fca3a5781 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java
@@ -19,7 +19,6 @@ package org.apache.lucene.search.vectorhighlight;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.search.vectorhighlight.FieldQuery.QueryPhraseMap;
import org.apache.lucene.search.vectorhighlight.FieldTermStack.TermInfo;
@@ -31,7 +30,7 @@ import org.apache.lucene.util.MergedIterator;
*/
public class FieldPhraseList {
/** List of non-overlapping WeightedPhraseInfo objects. */
- LinkedList<WeightedPhraseInfo> phraseList = new LinkedList<>();
+ List<WeightedPhraseInfo> phraseList = new ArrayList<>();
/**
* create a FieldPhraseList that has no limit on the number of phrases to analyze
@@ -62,7 +61,7 @@ public class FieldPhraseList {
public FieldPhraseList(FieldTermStack fieldTermStack, FieldQuery fieldQuery, int phraseLimit) {
final String field = fieldTermStack.getFieldName();
- LinkedList<TermInfo> phraseCandidate = new LinkedList<>();
+ List<TermInfo> phraseCandidate = new ArrayList<>();
QueryPhraseMap currMap = null;
QueryPhraseMap nextMap = null;
while (!fieldTermStack.isEmpty() && (phraseList.size() < phraseLimit)) {
@@ -136,7 +135,7 @@ public class FieldPhraseList {
}
MergedIterator<WeightedPhraseInfo> itr = new MergedIterator<>(false, allInfos);
// Step 2. Walk the sorted list merging infos that overlap
- phraseList = new LinkedList<>();
+ phraseList = new ArrayList<>();
if (!itr.hasNext()) {
return;
}
@@ -227,11 +226,11 @@ public class FieldPhraseList {
return termsInfos;
}
- public WeightedPhraseInfo(LinkedList<TermInfo> terms, float boost) {
+ public WeightedPhraseInfo(List<TermInfo> terms, float boost) {
this(terms, boost, 0);
}
- public WeightedPhraseInfo(LinkedList<TermInfo> terms, float boost, int seqnum) {
+ public WeightedPhraseInfo(List<TermInfo> terms, float boost, int seqnum) {
this.boost = boost;
this.seqnum = seqnum;
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/TestFieldPhraseList.java b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/TestFieldPhraseList.java
index f2b76a6e722..516c28389e5 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/TestFieldPhraseList.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/TestFieldPhraseList.java
@@ -16,7 +16,8 @@
*/
package org.apache.lucene.search.vectorhighlight;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseInfo;
@@ -269,7 +270,7 @@ public class TestFieldPhraseList extends AbstractTestCase {
}
private WeightedPhraseInfo newInfo(int startOffset, int endOffset, float boost) {
- LinkedList<TermInfo> infos = new LinkedList<>();
+ List<TermInfo> infos = new ArrayList<>();
infos.add(new TermInfo(TestUtil.randomUnicodeString(random()), startOffset, endOffset, 0, 0));
return new WeightedPhraseInfo(infos, boost);
}
@@ -291,12 +292,12 @@ public class TestFieldPhraseList extends AbstractTestCase {
}
public void testMergeOverlappingWeightedPhraseInfoAccumulateBoost() {
- LinkedList<TermInfo> infos1 = new LinkedList<>();
+ List<TermInfo> infos1 = new ArrayList<>();
infos1.add(new TermInfo("中国", 0, 2, 0, 0));
infos1.add(new TermInfo("经济", 3, 5, 1, 1));
WeightedPhraseInfo wpi1 = new WeightedPhraseInfo(infos1, 2.0f);
- LinkedList<TermInfo> infos2 = new LinkedList<>();
+ List<TermInfo> infos2 = new ArrayList<>();
infos2.add(new TermInfo("经济", 3, 5, 1, 1));
infos2.add(new TermInfo("发展", 6, 8, 2, 2));
WeightedPhraseInfo wpi2 = new WeightedPhraseInfo(infos2, 3.0f);
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/spans/SpanNearQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/spans/SpanNearQuery.java
index 9e7f4a049ed..f38c45ab2c3 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/spans/SpanNearQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/spans/SpanNearQuery.java
@@ -18,8 +18,8 @@ package org.apache.lucene.queries.spans;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -45,7 +45,7 @@ public class SpanNearQuery extends SpanQuery implements Cloneable {
public static class Builder {
private final boolean ordered;
private final String field;
- private final List<SpanQuery> clauses = new LinkedList<>();
+ private final Collection<SpanQuery> clauses = new ArrayList<>();
private int slop;
/**
@@ -61,7 +61,7 @@ public class SpanNearQuery extends SpanQuery implements Cloneable {
/** Add a new clause */
public Builder addClause(SpanQuery clause) {
- if (Objects.equals(clause.getField(), field) == false)
+ if (!Objects.equals(clause.getField(), field))
throw new IllegalArgumentException(
"Cannot add clause " + clause + " to SpanNearQuery for field " + field);
this.clauses.add(clause);
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/config/QueryConfigHandler.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/config/QueryConfigHandler.java
index c194d73286c..89d32ed18b0 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/config/QueryConfigHandler.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/config/QueryConfigHandler.java
@@ -16,7 +16,8 @@
*/
package org.apache.lucene.queryparser.flexible.core.config;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Collection;
import org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor;
import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
@@ -37,7 +38,7 @@ import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
*/
public abstract class QueryConfigHandler extends AbstractQueryConfig {
- private final LinkedList<FieldConfigListener> listeners = new LinkedList<>();
+ private final Collection<FieldConfigListener> listeners = new ArrayList<>();
/**
* Returns an implementation of {@link FieldConfig} for a specific field name. If the implemented
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/processors/QueryNodeProcessorPipeline.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/processors/QueryNodeProcessorPipeline.java
index dcd2b8f5e03..ad0acd9b687 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/processors/QueryNodeProcessorPipeline.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/processors/QueryNodeProcessorPipeline.java
@@ -16,9 +16,9 @@
*/
package org.apache.lucene.queryparser.flexible.core.processors;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
@@ -38,7 +38,7 @@ import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode;
*/
public class QueryNodeProcessorPipeline implements QueryNodeProcessor, List<QueryNodeProcessor> {
- private LinkedList<QueryNodeProcessor> processors = new LinkedList<>();
+ private List<QueryNodeProcessor> processors = new ArrayList<>();
private QueryConfigHandler queryConfig;
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MultiPhraseQueryNodeBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MultiPhraseQueryNodeBuilder.java
index 06e7c8ad9b2..4c62dc784fe 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MultiPhraseQueryNodeBuilder.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MultiPhraseQueryNodeBuilder.java
@@ -16,7 +16,7 @@
*/
package org.apache.lucene.queryparser.flexible.standard.builders;
-import java.util.LinkedList;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -51,19 +51,17 @@ public class MultiPhraseQueryNodeBuilder implements StandardQueryBuilder {
FieldQueryNode termNode = (FieldQueryNode) child;
TermQuery termQuery =
(TermQuery) termNode.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
- List<Term> termList = positionTermMap.get(termNode.getPositionIncrement());
- if (termList == null) {
- termList = new LinkedList<>();
- positionTermMap.put(termNode.getPositionIncrement(), termList);
- }
+ List<Term> termList =
+ positionTermMap.computeIfAbsent(
+ termNode.getPositionIncrement(), _ -> new ArrayList<>());
termList.add(termQuery.getTerm());
}
for (Map.Entry<Integer, List<Term>> entry : positionTermMap.entrySet()) {
List<Term> termList = entry.getValue();
- phraseQueryBuilder.add(termList.toArray(new Term[termList.size()]), entry.getKey());
+ phraseQueryBuilder.add(termList.toArray(new Term[0]), entry.getKey());
}
}
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java
index 26746a6b0ed..db47097e362 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java
@@ -19,7 +19,6 @@ package org.apache.lucene.queryparser.flexible.standard.processors;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CachingTokenFilter;
@@ -187,7 +186,7 @@ public class AnalyzerQueryNodeProcessor extends QueryNodeProcessorImpl {
if (positionCount == 1) {
// simple case: only one position, with synonyms
- LinkedList<QueryNode> children = new LinkedList<>();
+ List<QueryNode> children = new ArrayList<>();
for (int i = 0; i < numTokens; i++) {
String term = null;
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/RemoveEmptyNonLeafQueryNodeProcessor.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/RemoveEmptyNonLeafQueryNodeProcessor.java
index 775b42a5bc8..1572ee6a578 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/RemoveEmptyNonLeafQueryNodeProcessor.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/RemoveEmptyNonLeafQueryNodeProcessor.java
@@ -16,7 +16,8 @@
*/
package org.apache.lucene.queryparser.flexible.standard.processors;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
import org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode;
@@ -37,7 +38,7 @@ import org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor
*/
public class RemoveEmptyNonLeafQueryNodeProcessor extends QueryNodeProcessorImpl {
- private LinkedList<QueryNode> childrenBuffer = new LinkedList<>();
+ private final Collection<QueryNode> childrenBuffer = new ArrayList<>();
public RemoveEmptyNonLeafQueryNodeProcessor() {
// empty constructor
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/ValidatingTokenFilter.java b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/ValidatingTokenFilter.java
index 3523b69c2d6..01bc40bc560 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/ValidatingTokenFilter.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/ValidatingTokenFilter.java
@@ -18,11 +18,11 @@ package org.apache.lucene.tests.analysis;
import java.io.IOException;
import java.io.PrintStream;
+import java.util.ArrayDeque;
import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Queue;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@@ -60,7 +60,7 @@ public final class ValidatingTokenFilter extends TokenFilter {
private final CharTermAttribute termAtt = getAttribute(CharTermAttribute.class);
// record the last MAX_DEBUG_TOKENS tokens seen so they can be dumped on failure
- private final List<Token> tokens = new LinkedList<>();
+ private final Queue<Token> tokens = new ArrayDeque<>();
private final String name;
@@ -200,7 +200,7 @@ public final class ValidatingTokenFilter extends TokenFilter {
private void addToken(int startOffset, int endOffset, int posInc) {
if (tokens.size() == MAX_DEBUG_TOKENS) {
- tokens.remove(0);
+ tokens.remove();
}
tokens.add(new Token(termAtt.toString(), posInc, startOffset, endOffset));
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/automaton/AutomatonTestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/automaton/AutomatonTestUtil.java
index 58eef3db36e..eda1d7a9fd4 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/automaton/AutomatonTestUtil.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/automaton/AutomatonTestUtil.java
@@ -21,9 +21,9 @@ import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Queue;
import java.util.Random;
import java.util.Set;
import org.apache.lucene.tests.util.TestUtil;
@@ -189,7 +189,7 @@ public class AutomatonTestUtil extends Assert {
leadsToAccept = new HashMap<>();
final Map<Integer, List<ArrivingTransition>> allArriving = new HashMap<>();
- final LinkedList<Integer> q = new LinkedList<>();
+ final Queue<Integer> q = new ArrayDeque<>();
final Set<Integer> seen = new HashSet<>();
// reverse map the transitions, so we can quickly look
@@ -213,7 +213,7 @@ public class AutomatonTestUtil extends Assert {
// Breadth-first search, from accept states,
// backwards:
while (q.isEmpty() == false) {
- final int s = q.removeFirst();
+ final int s = q.remove();
List<ArrivingTransition> arriving = allArriving.get(s);
if (arriving != null) {
for (ArrivingTransition at : arriving) {
@@ -448,7 +448,7 @@ public class AutomatonTestUtil extends Assert {
int[] points = a.getStartPoints();
// subset construction
Map<Set<Integer>, Set<Integer>> sets = new HashMap<>();
- LinkedList<Set<Integer>> worklist = new LinkedList<>();
+ Queue<Set<Integer>> worklist = new ArrayDeque<>();
Map<Set<Integer>, Integer> newstate = new HashMap<>();
sets.put(initialset, initialset);
worklist.add(initialset);
@@ -457,7 +457,7 @@ public class AutomatonTestUtil extends Assert {
newstate.put(initialset, 0);
Transition t = new Transition();
while (worklist.size() > 0) {
- Set<Integer> s = worklist.removeFirst();
+ Set<Integer> s = worklist.remove();
int r = newstate.get(s);
for (int q : s) {
if (a.isAccept(q)) {