3 回答

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ù)類。

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)

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
添加回答
舉報(bào)