Ars*_*yan 5 java arrays json gson kotlin
我在如何删除与某些参数不匹配的元素方面遇到了问题。例如,我有两个数据类:第一个和第二个第一个包含第二个的属性,即城市、价格等:
data class Properties(val city: String, val day: Int, val month: Int, val yearProp: Int, val dayEnd: Int, val monthEnd: Int, val yearEndProp: Int, val priceFrom: Int, val priceTo: Int)
Run Code Online (Sandbox Code Playgroud)
Item 的第二个数据类:
data class Product(var title: String, var price: String, var photoId : String)
Run Code Online (Sandbox Code Playgroud)
我正在通过 json 中的此代码解析产品的数据:
val gson = GsonBuilder().setPrettyPrinting().create()
val inputStream : Reader = context.getResources().openRawResource(R.raw.products).reader()
var productList: ArrayList<Product> = gson.fromJson(inputStream, object : TypeToken<List<Product>>() {}.type)
productList.forEach { println(it) }
Run Code Online (Sandbox Code Playgroud)
这是 JSON 文件:
[
{
"city": "New York",
"title": "Banana",
"price": "$1,99"
"photoId": "someurl"
},
{
"city": "New York",
"title": "Strawberry",
"price": "$1,99"
"photoId": "someurl"
},
{
"city": "Philadelphia",
"title": "Banana",
"price": "$4,99"
"photoId": "someurl"
}
]
Run Code Online (Sandbox Code Playgroud)
所以,我想过滤它。如果用户匹配“纽约”,则列表中必须只有“城市”:“纽约”的项目。是的,这只是举例,不要注意我在那里写的所有愚蠢:D
或者我应该在添加项目时过滤项目?但如果是这样,那该怎么办呢?
s-h*_*ter 11
这是一个示例,用于从适当的列表中删除具有特定条件的项目。此示例从整数列表中删除偶数。
var myLists = mutableListOf(1,2,3,4,5,6,7,8,9)
myLists.removeAll{ it % 2 == 0 }
println(myLists)
Run Code Online (Sandbox Code Playgroud)
印刷:
[1, 3, 5, 7, 9]
Run Code Online (Sandbox Code Playgroud)
这是为您提供的解决方案。请注意,在您的 OP 中,您实际上根本没有从 Product 类中引用 Properties 类(因此您无法按属性过滤产品,因为它们在代码中没有任何关系)。
另外,您正在使用 var. 带有 var 的属性是可变的(意味着它们可以在对象创建后更改)。通常最好使用 val,如果您需要更改对象的属性,请使用更新的属性创建一个新实例(对于多线程和异步代码更好)。
@Test
fun testFilter() {
// Properties of a product
data class Properties(val city: String, val day: Int, val month: Int, val yearProp: Int, val dayEnd: Int, val monthEnd: Int, val yearEndProp: Int, val priceFrom: Int, val priceTo: Int)
// A product (notice it references Properties)
data class Product(val productProperties: Properties, val title: String, val price: String, val photoId : String)
// Let's pretend these came from the server (we parsed them using Gson converter)
val productA = Product(Properties("New York", 0, 0, 0, 0, 0, 0, 0, 0), "Sweets", "$1", "1")
val productB = Product(Properties("Boston", 0, 0, 0, 0, 0, 0, 0, 0), "Eggs", "$2", "1")
val productC = Product(Properties("New York", 0, 0, 0, 0, 0, 0, 0, 0), "Flour", "$1", "1")
// Create a list of the products
val listOfProducts = mutableListOf(productA, productB, productC)
// Filter the products which have new york as the city in their properties
val filteredNewYorkProducts = listOfProducts.filter { it.productProperties.city == "New York" }
// Assert that the filtered products contains 2 elements
Assert.assertTrue(filteredNewYorkProducts.size == 2)
// Assert that the filtered products contains both product A and B
Assert.assertTrue(filteredNewYorkProducts.contains(productA))
Assert.assertTrue(filteredNewYorkProducts.contains(productB))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16859 次 |
| 最近记录: |