如何删除类的arraylist中的重复项

我正在尝试使用 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_typetype

var typetype:String = typetype

var modeltype:String = modeletype

var photype:String = phtype


}

现在我想从我的列表中删除重复的项目,我想按 car_type 和类型和型号排序,如果该项目重复,我想删除它



慕森王
浏览 140回答 3
3回答

一只名叫tom的猫

您应该使用数据类。它将equals()在内部包含覆盖方法:data class TypeItems(&nbsp; &nbsp; val car_typetype: String,&nbsp; &nbsp; val typetype: String,&nbsp; &nbsp; val modeletype: String,&nbsp; &nbsp; 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(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product.getString("car_type"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product.getString("type"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product.getString("model"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url2&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; if(!list.contains(typeItems)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(typeItems)&nbsp; &nbsp; &nbsp; &nbsp; }因此,还有另一种方法可以解决此问题:不是val adapter = TypeAdapter(this.applicationContext, list)调用val adapter = TypeAdapter(this.applicationContext, list.distinct())方法,而是distinct()以相同的顺序返回列表的唯一值。不要忘记让它成为数据类。

有只小跳蛙

请注意,接受的答案建议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(&nbsp; &nbsp; val car: String,&nbsp; &nbsp; val type: String,&nbsp; &nbsp; val model: String,&nbsp; &nbsp; val ph: String)fun main() {&nbsp; &nbsp; // simulates a response from the server&nbsp; &nbsp; val response = listOf(&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carBBB", "typeBBB", "modelBBB", "phBBB"),&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carAAA", "typeAAA", "modelAAA", "phAAA"),&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carAAA", "typeAAA", "modelAAA", "phAAA"),&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carCCC", "typeZZZ", "modelYYY", "phCCC"),&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carCCC", "typeXXX", "modelWWW", "phCCC"),&nbsp; &nbsp; &nbsp; &nbsp; TypeItems("carCCC", "typeXXX", "modelVVV", "phCCC")&nbsp; &nbsp; )&nbsp; &nbsp; // creates an empty TreeSet with the desired sorting&nbsp; &nbsp; val set = TreeSet<TypeItems>(&nbsp; &nbsp; &nbsp; &nbsp; Comparator.comparing(TypeItems::car)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenComparing(TypeItems::type)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenComparing(TypeItems::model)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenComparing(TypeItems::ph) // needed for "consistency with equals"&nbsp; &nbsp; )&nbsp; &nbsp; // add each element to the set, it'll handle duplicates&nbsp; &nbsp; response.forEach { set.add(it) }&nbsp; &nbsp; // do something with the resulting set: print each item on a new line&nbsp; &nbsp; set.forEach {&nbsp; &nbsp; &nbsp; &nbsp; println(it)&nbsp; &nbsp; }}那会打印:TypeItems(car=carAAA, type=typeAAA, model=modelAAA, ph=phAAA) // duplicate removedTypeItems(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)

摇曳的蔷薇

替换val list= ArrayList<TypeItems>()为val set = SortedSet<TypeItems>()并覆盖 TypeItems 的 equals 方法:override fun equals(other: Any?): Boolean {&nbsp; &nbsp; if (other is TypeItems) {&nbsp; &nbsp; &nbsp; &nbsp; other.cartype == this.cartype && ... // replace TypeItems fields&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; false&nbsp; &nbsp; }}另外如果你想排序,TypeItems 必须实现Comparable https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java