I have a collection of 10,000 objects that have two properties: a string
and a string[]
class Product
{
String name;
String[] aliases;
}
Each object is roughly 64 bytes, so we're looking at under a megabyte in memory for the whole collection, which seems manageable for Android to search in memory.
I want an exact match to either the single string, or any string in the array, and to then return the name
property of the object.
However, android has some restrictions on Lambdas and .stream()
that exclude many of the popular search solutions without forcing users to have OS 7.0 or above, which rules out roughly one third of users. I'd like to avoid that.
Currently, the following code is working, but seems terrible considering some of options I've seen out there which take advantage of streams and lambdas.
public String isProductOrAlias(String lowertext) {
//products is instantiated in the parent class
//as a List<Product> loaded into memory from a local file
try {
for (Product p: products) {
if(lowertext.equals(p.name.toLowerCase()))
return p.name;
if(p.aliases != null) {
for (String s: p.aliases) {
if(lowertext.equals(s.toLowerCase()))
return p.name;
}
}
}
} catch (Exception ex) {
return "";
}
return "";
}
What's the best way to achieve the same goal, given the restrictions of the environment? Ideally, we'd like to keep minSDK to 23 (6.0) and above.