Efficiently determine whether a string is in List in Java (ignoring upper and lower case)
For situations where frequent calls are required and data volume is large, there are several optimization solutions to choose from:
Solution 1: Use HashSet to store lowercase versions (recommended)
import .*; public class CaseInsensitiveLookup { private final Set<String> lowerCaseSet; public CaseInsensitiveLookup(List<String> originalList) { // Preprocessing: convert all strings to lowercase and save them into HashSet = new HashSet<>(()); for (String s : originalList) { (()); } } public boolean containsIgnoreCase(String target) { return (()); } }
advantage:
- Preprocessing time complexity O(n), and after that time complexity O(1)
- HashSet's search efficiency is extremely high
- Memory usage is relatively reasonable
shortcoming:
- The initial build requires traversing the entire list
- If the original list changes frequently, you need to rebuild the Set
Scheme 2: Use TreeSet to customize the comparator
import .*; public class CaseInsensitiveLookup { private final Set<String> treeSet; public CaseInsensitiveLookup(List<String> originalList) { = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); (originalList); } public boolean containsIgnoreCase(String target) { return (target); } }
advantage:
- Query time complexity O(log n)
- Keep elements in order
shortcoming:
- Slightly slower than HashSet
- Preprocessing is also required
Solution 3: Parallel stream processing (suitable for super large lists and infrequent calls)
import .*; public class CaseInsensitiveLookup { private final List<String> originalList; public CaseInsensitiveLookup(List<String> originalList) { = originalList; } public boolean containsIgnoreCase(String target) { return () .anyMatch(s -> (target)); } }
advantage:
- No preprocessing is required
- Multi-core CPUs can be used
shortcoming:
- Each query requires traversal (although parallel)
- Not suitable for frequent call scenarios
Best Practice Recommendations
- If the list does not change frequently: Use scheme 1 (HashSet), which is the most efficient method of querying
- If necessary, keep the insertion order: Consider LinkedHashSet
- If the list changes frequently: Consider scheme 3 or use ConcurrentHashMap to implement functions similar to scheme 1
- If memory is very tight: Scheme 3 can be considered, but the performance will decrease
Example of usage
List<String> largeList = ("Apple", "Banana", "Orange", ...); CaseInsensitiveLookup lookup = new CaseInsensitiveLookup(largeList); // Frequently calledboolean contains = ("apple"); // returntrue
Which solution to choose depends on your specific scenario: data size, query frequency, list change frequency and memory limitations.
This is the article about efficiently judging whether strings are in List in Java (ignoring uppercase and lowercase) that ends with this article. For more related java strings to determine whether they are in List, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!