亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除類的arraylist中的重復項

如何刪除類的arraylist中的重復項

慕森王 2023-06-04 14:57:36
我正在嘗試使用 RecyclerView 和 volley 將服務器中的數據放入我的應用程序中,現在正因為如此,我正在使用一個適配器,這是我的適配器類class TypeAdapter(var con: Context, var list: ArrayList<TypeItems>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {    (p0 as ItemView).bind(list[p1].cartype, list[p1].typetype, list[p1].modeltype, list[p1].photype)}override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {    val v= LayoutInflater.from(con).inflate(R.layout.car_model_item, p0, false)    return ItemView(v)}override fun getItemCount(): Int {    return list.size}class ItemView(itemVeiw: View) : RecyclerView.ViewHolder(itemVeiw) {    fun bind(car_type: String, type: String, modele: String, ph: String) {        Picasso.with(itemView.context).load(ph).into(itemView.type)        itemView.name.text= "$car_type $type"        itemView.model.text= modele    }}}這是我的 TypeItems 類class TypeItems(car_typetype: String, typetype: String, modeletype: String, phtype: String) {var cartype:String = car_typetypevar typetype:String = typetypevar modeltype:String = modeletypevar photype:String = phtype}現在我想從我的列表中刪除重復的項目,我想按 car_type 和類型和型號排序,如果該項目重復,我想刪除它
查看完整描述

3 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

您應該使用數據類。它將equals()在內部包含覆蓋方法:


data class TypeItems(

    val car_typetype: String,

    val typetype: String,

    val modeletype: String,

    val phtype: String

)

在深入研究這個問題之后,我發現,你不能在集合get()上調用方法Set。所以,這段代碼不起作用: (p0 as ItemView).bind(list[p1].cartype, list[p1].typetype, list[p1].modeltype, list[p1].photype)

總結,Set不會幫助你。要解決您的問題,您只需要調用防御檢查:


val typeItems = TypeItems(

                product.getString("car_type"),

                product.getString("type"),

                product.getString("model"),

                url2

        )

        if(!list.contains(typeItems)) {

            list.add(typeItems)

        }

因此,還有另一種方法可以解決此問題:不是

val adapter = TypeAdapter(this.applicationContext, list)

調用

val adapter = TypeAdapter(this.applicationContext, list.distinct())

方法,而是distinct()以相同的順序返回列表的唯一值。不要忘記讓它成為數據類。


查看完整回答
反對 回復 2023-06-04
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

請注意,接受的答案建議list.contains(x)在可能將元素添加到列表之前使用。但是,contains需要 O(n) 并且您正在對響應中得到的每個項目進行檢查,因此您的總復雜度為 O(n^2)。如果你有很多項目,性能可能會很差,這意味著你的應用程序可能沒有響應,用戶體驗可能會受到影響。此外,項目按插入順序排序,因此我們需要按所需順序對其進行顯式排序——至少添加另一個 O(n*log(n)),盡管它由之前的 O(n^2) 支配。


因此, ATreeSet可能更適合這個特定的用例——正如@Rogue 和@taha 所建議的那樣——因為它自動防止重復,它在插入時具有 O(log(n)) 的復雜性,并且它強制執行某種排序。


以下是如何使用它的示例:


data class TypeItems(

    val car: String,

    val type: String,

    val model: String,

    val ph: String

)


fun main() {

    // simulates a response from the server

    val response = listOf(

        TypeItems("carBBB", "typeBBB", "modelBBB", "phBBB"),

        TypeItems("carAAA", "typeAAA", "modelAAA", "phAAA"),

        TypeItems("carAAA", "typeAAA", "modelAAA", "phAAA"),

        TypeItems("carCCC", "typeZZZ", "modelYYY", "phCCC"),

        TypeItems("carCCC", "typeXXX", "modelWWW", "phCCC"),

        TypeItems("carCCC", "typeXXX", "modelVVV", "phCCC")

    )


    // creates an empty TreeSet with the desired sorting

    val set = TreeSet<TypeItems>(

        Comparator.comparing(TypeItems::car)

            .thenComparing(TypeItems::type)

            .thenComparing(TypeItems::model)

            .thenComparing(TypeItems::ph) // needed for "consistency with equals"

    )


    // add each element to the set, it'll handle duplicates

    response.forEach { set.add(it) }


    // do something with the resulting set: print each item on a new line

    set.forEach {

        println(it)

    }

}

那會打?。?/p>


TypeItems(car=carAAA, type=typeAAA, model=modelAAA, ph=phAAA) // duplicate removed

TypeItems(car=carBBB, type=typeBBB, model=modelBBB, ph=phBBB) // car order enforced (as B > A)

TypeItems(car=carCCC, type=typeXXX, model=modelVVV, ph=phCCC) // type order enforced (as X > B)

TypeItems(car=carCCC, type=typeXXX, model=modelWWW, ph=phCCC) // model order enforced (as W > V)

TypeItems(car=carCCC, type=typeZZZ, model=modelYYY, ph=phCCC)


查看完整回答
反對 回復 2023-06-04
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

替換val list= ArrayList<TypeItems>()為val set = SortedSet<TypeItems>()


并覆蓋 TypeItems 的 equals 方法:


override fun equals(other: Any?): Boolean {

    if (other is TypeItems) {

        other.cartype == this.cartype && ... // replace TypeItems fields

    } else {

        false

    }

}

另外如果你想排序,TypeItems 必須實現Comparable https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html


查看完整回答
反對 回復 2023-06-04
  • 3 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號