我正在尝试将此代码从answer重写为Kotlin中的通用ArrayList扩展。
public boolean equalLists(List<String> one, List<String> two){
if (one == null && two == null){
return true;
}
if((one == null && two != null)
|| one != null && two == null
|| one.size() != two.size()){
return false;
}
//to avoid messing the order of the lists we will use a copy
//as noted in comments by A. R. S.
one = new ArrayList<String>(one);
two = new ArrayList<String>(two);
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
我想出了下面的代码,但得到了Unresolved reference: T
fun ArrayList<T>.equalLists(one: ArrayList<T>?, two: ArrayList<T>?): Boolean {
if (one == null && two == null) {
return true
}
if (one == null && two != null || one != null && two == null || one?.size != two?.size) {
return false
}
val oneCopy = ArrayList(one)
val twoCopy = ArrayList(two)
oneCopy.sort()
twoCopy.sort()
return one == two
}
如何在Kotlin中正确实现这一点?
答案 0 :(得分:2)
我看到您找到了一个可行的解决方案,但我相信,更Kotlin惯用的解决方案是在List
接口本身上定义此定义,而不是专门在ArrayList
上定义,并且也可以声明为作为可空List
引用的扩展,等效于原始Java代码(它支持两个空列表作为参数):
fun <T : Comparable<T>> List<T>?.containsSameElementsAs(other: List<T>?) = when {
(this == null) xor (other == null) -> false // if only one is null
(this?.size != other?.size) -> false // if both null, or have different sizes
(this?.size == 1) -> this == other // avoid allocations for single element
else -> this?.sorted() == other?.sorted()
}
如果顺序不重要且重复项没有意义,也可能值得考虑使用Set
。
答案 1 :(得分:0)
下面是kotlin中的泛型示例
首先创建一个名为Product的自定义pojo类:
public class Product implements Serializable {
int id;
String mName;
public Product(int id, String mName) {
this.id = id;
this.mName = mName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return id == product.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
然后在onCreate()方法中添加以下代码:
val p1 = Product(1, "abc")
val p2 = Product(2, "abc")
val p3 = Product(3, "abc")
val p4 = Product(4, "abc")
val p5 = Product(5, "abc")
val p11 = Product(1, "abc")
val p22 = Product(12, "sdg")
val p33 = Product(3, "xfg")
val p44 = Product(14, "pqr")
val p55 = Product(5, "fhtryt")
val mProduList1 = ArrayList<Product>()
val mProduList2 = ArrayList<Product>()
mProduList1.add(p1)
mProduList1.add(p2)
mProduList1.add(p3)
mProduList1.add(p4)
mProduList1.add(p5)
mProduList2.add(p11)
mProduList2.add(p22)
mProduList2.add(p33)
mProduList2.add(p44)
mProduList2.add(p55)
equalLists(mProduList1, mProduList2)
然后创建名为“ equalLists”的通用函数:
public fun equalLists(one: ArrayList<*>?, two: ArrayList<*>?): Boolean {
if (one == null && two == null) {
return true
}
if (one == null && two != null || one != null && two == null || one?.size != two?.size) {
return false
}
val oneCopy = ArrayList(one)
val twoCopy = ArrayList(two)
sortProduct(oneCopy)
sortProduct(twoCopy)
return one == two
}
然后创建新函数,将泛型转换为产品,然后对其进行排序:
private fun sortProduct(dummyCopy: ArrayList<Any>) {
(dummyCopy as ArrayList<Product>).sortWith(object: Comparator<Product>{
override fun compare(p1: Product, p2: Product): Int = when {
p1.id > p2.id -> 1
p1.id == p2.id -> 0
else -> -1
}
})
for(i in dummyCopy){
Log.e("tagMine", "i.id is " + i.id + " i.mName " + i.mName)
}
}
要定义泛型函数,我们可以选择以下一种,例如使用“ <*>
”,也可以使用“ fun<T>
”,如下所示:
1)public fun equalLists(one: ArrayList<*>?, two: ArrayList<*>?): Boolean {
2)fun<T> equalLists(one: ArrayList<T>?, two: ArrayList<T>?): Boolean {
答案 2 :(得分:0)
最后为此提出了通用ArrayList扩展。
fun <T: Comparable<T>> ArrayList<T>.equalLists(other: ArrayList<T>?): Boolean {
if (other == null) {
return true
}
if ( size != other.size ) {
return false
}
val thisCopy = ArrayList(this)
val otherCopy = ArrayList(other)
thisCopy.sort()
otherCopy.sort()
return thisCopy == otherCopy
}
用法:
categories.equalLists(otherCategories)