通过匹配谓词集合中的属性,特定于Android的查找对象 [英] Android specific find object by matching properties in a collection on predicate

查看:239
本文介绍了通过匹配谓词集合中的属性,特定于Android的查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10,000个对象的集合,这些对象具有两个属性:stringstring[]

I have a collection of 10,000 objects that have two properties: a string and a string[]

class Product
{
  String name;
  String[] aliases;
}

每个对象大约有64个字节,因此我们正在查看整个集合的内存不足1兆字节,对于Android搜索内存而言,这似乎是可管理的.

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.

我想要与单个字符串或数组中的任何字符串完全匹配,然后返回对象的name属性.

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.

但是,android在Lambdas和.stream()上有一些限制,这些限制排除了许多流行的搜索解决方案,而没有强迫用户使用OS 7.0或更高版本,从而排除了大约三分之一的用户.我想避免这种情况.

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.

当前,以下代码正在运行,但考虑到我在那里看到的一些利用流和lambda的选项,这似乎很糟糕.

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 "";
}

鉴于环境的限制,实现同一目标的最佳方法是什么?理想情况下,我们希望将minSDK保持在23(6.0)以上.

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.

推荐答案

规范的Kotlin,它可以在任何版本的Android上运行,而与Java Streams无关,因为它不使用Java Streams的是:

Canonical Kotlin, which would work on any version of Android regardless of Java Streams since it doesn't use them would be:

data class Product(val name: String, val aliases: Array<String>)

// written as an extension function, but doesn't have to be...
fun List<Product>.isProductOrAlias(text: String): String {
   return this.firstOrNull { product -> 
       product.name.equals(text, ignoreCase = true) ||
       product.aliases.any { alias -> 
           alias.equals(text, ignoreCase = true)
       }
   }?.name ?: ""
}

fun test() {
   // assume somewhere this was created
   val products = listOf<Product>(...)

   println(products.isProductOrAlias("something")) // prints name of Product or ""
}

有关标准库,请参阅Kotlin API参考:

See Kotlin API reference for the standard library:

数组扩展功能 any()

array extension function any()

字符串扩展功能 equals()

string extension function equals()

科特琳猫王操作员?:

科特琳安全呼叫?.

这篇关于通过匹配谓词集合中的属性,特定于Android的查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆