第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何刪除類的arraylist中的重復(fù)項(xiàng)

如何刪除類的arraylist中的重復(fù)項(xiàng)

慕森王 2023-06-04 14:57:36
我正在嘗試使用 RecyclerView 和 volley 將服務(wù)器中的數(shù)據(jù)放入我的應(yīng)用程序中,現(xiàn)在正因?yàn)槿绱耍艺谑褂靡粋€(gè)適配器,這是我的適配器類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}現(xiàn)在我想從我的列表中刪除重復(fù)的項(xiàng)目,我想按 car_type 和類型和型號(hào)排序,如果該項(xiàng)目重復(fù),我想刪除它
查看完整描述

3 回答

?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊

您應(yīng)該使用數(shù)據(jù)類。它將equals()在內(nèi)部包含覆蓋方法:


data class TypeItems(

    val car_typetype: String,

    val typetype: String,

    val modeletype: String,

    val phtype: String

)

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

總結(jié),Set不會(huì)幫助你。要解決您的問(wèn)題,您只需要調(diào)用防御檢查:


val typeItems = TypeItems(

                product.getString("car_type"),

                product.getString("type"),

                product.getString("model"),

                url2

        )

        if(!list.contains(typeItems)) {

            list.add(typeItems)

        }

因此,還有另一種方法可以解決此問(wèn)題:不是

val adapter = TypeAdapter(this.applicationContext, list)

調(diào)用

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

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


查看完整回答
反對(duì) 回復(fù) 2023-06-04
?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


因此, ATreeSet可能更適合這個(gè)特定的用例——正如@Rogue 和@taha 所建議的那樣——因?yàn)樗詣?dòng)防止重復(fù),它在插入時(shí)具有 O(log(n)) 的復(fù)雜性,并且它強(qiáng)制執(zhí)行某種排序。


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


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)

    }

}

那會(huì)打?。?/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)


查看完整回答
反對(duì) 回復(fù) 2023-06-04
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

替換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 必須實(shí)現(xiàn)Comparable https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html


查看完整回答
反對(duì) 回復(fù) 2023-06-04
  • 3 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)