如何从kotlin中的列表中删除一些元素

时间:2018-02-23 11:18:19

标签: java arrays json kotlin gson

我遇到了如何删除与某些参数不匹配的元素的问题。例如,我有两个数据类:第一个和第二个 第一个包含第二个属性,即城市,价格等:

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)

Item的第二个数据类:

data class Product(var title: String, var price: String, var photoId : String)

我正在使用json:

中的代码解析Products的数据
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) }

这是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"
    }
]

所以,我想过滤它。如果用户匹配“纽约”,那么列表中的项目必须只包含“城市”:“纽约”。是的,这只是例如,不要注意我在那里写的所有愚蠢:D

或者我可能会在添加项目时过滤项目?但如果是这样,那该怎么做呢?

2 个答案:

答案 0 :(得分:2)

这是一个适合您的解决方案。请注意,在您的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))
}

答案 1 :(得分:0)

这里是一个示例,用于从列表中删除具有某些条件的项目。此示例从整数列表中删除偶数。

var myLists = mutableListOf(1,2,3,4,5,6,7,8,9)
myLists.removeAll{ it % 2 == 0 }
println(myLists)

打印:

[1, 3, 5, 7, 9]