Kotlin之集合

list

//不可變集合
val intList: List<Int> = listOf(1, 2, 3, 4)
println(intList)
//輸出[1, 2, 3, 4]


//可變集合
val intList2: MutableList<Int> = mutableListOf(1, 2, 3, 4)
intList2.add(5);
for (item in intList2.indices) {
    if (item == 3) {
        intList2.remove(item)
    }
}
println(intList2)
//輸出[1, 2, 4, 5]

//集合倒序排列
intList2.sortDescending()
println(intList2)
//輸出[5, 4, 2, 1]

//集合正序排列
intList2.sort()
println(intList2)
//輸出[1, 2, 4, 5]


data class Student(var name: String, var score: Int)

val studentList: MutableList<Student> = mutableListOf()
studentList.add(Student("小a",100))
studentList.add(Student("小d",90))
studentList.add(Student("小e",95))
studentList.add(Student("小b",90))
//條件排序
studentList.sortBy { it.score }
println(studentList)
//輸出[Student(name=小d, score=90), Student(name=小b, score=90), Student(name=小e, score=95), Student(name=小a, score=100)]
//多重條件排序
studentList.sortWith(compareBy({it.score},{it.name}))
println(studentList)
//輸出[Student(name=小b, score=90), Student(name=小d, score=90), Student(name=小e, score=95), Student(name=小a, score=100)]

Set

val setStr:MutableSet<String> = mutableSetOf("h","e","l","l","o")//set會自動過濾重復(fù)元素
setStr.add("w")
setStr.remove("h")
println(setStr)
//輸出 [e, l, o, w]

//set的加減操作
setStr += mutableSetOf<String>("w","o","r","l","d")
println(setStr)
//輸出 [e, l, o, w, r, d]

setStr -= mutableSetOf<String>("w","d","A")
println(setStr)
//輸出[e, l, o, r] 

Map

val mapString:MutableMap<Int,String> = mutableMapOf();

mapString[1] = "a"
mapString[2] = "b"
mapString.put(3,"c")//編譯器建議使用mapString[3]= "c"的方式
println(mapString)
//輸出{1=a, 2=b, 3=c}
println(mapString.keys)
//輸出[1, 2, 3]
println(mapString.values)
//輸出[a, b, c]
if(mapString.containsKey(2)){
    println("key-2 exist")
}
//輸出 key-2 exist
if(mapString.containsValue("b")){
    println("Value-b exist");
}
//輸出 Value-b exist


Q1:鍵值相同的兩個map相等么

val mapString2:MutableMap<Int,String> = mutableMapOf();
mapString2[1] = "a"
mapString2[2] = "b"
mapString2.put(3,"c")
println("mapString = mapString2舒萎?${mapString == mapString2},${mapString.equals(mapString2)}")
//輸出 mapString = mapString2?true,true

看看Java的比較結(jié)果為

HashMap<Integer,String> map1 = new HashMap<>();
map1.put(1,"a");

HashMap<Integer,String> map2 = new HashMap<>();
map2.put(1,"a");
System.out.println("map1=map2? "+(map1==map2)+" "+map1.equals(map2));
輸出 map1=map2? false true

Java的結(jié)果是我們熟悉的昔馋,Java中“==”比較的實際上是“地址”,只有“地址”相同才可以認(rèn)為相同。而equals比較的是值侯勉。但是Kotlin是為什么呢捶闸?
查看Kotlin中的“==”可以發(fā)現(xiàn)原來其重載了equals方法夜畴,所以不只是map,Kotlin中所有其它對象的==也等同于equals了

public open class Any {
    /**
     * Indicates whether some other object is "equal to" this one. Implementations must fulfil the following
     * requirements:
     *
     * * Reflexive: for any non-null reference value x, x.equals(x) should return true.
     * * Symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
     * * Transitive:  for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true
     * * Consistent:  for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
     *
     * Note that the `==` operator in Kotlin code is translated into a call to [equals] when objects on both sides of the
     * operator are not null.
     */
    public open operator fun equals(other: Any?): Boolean

    /**
     * Returns a hash code value for the object.  The general contract of hashCode is:
     *
     * * Whenever it is invoked on the same object more than once, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
     * * If two objects are equal according to the equals() method, then calling the hashCode method on each of the two objects must produce the same integer result.
     */
    public open fun hashCode(): Int

    /**
     * Returns a string representation of the object.
     */
    public open fun toString(): String
}

debug “==” 查看到實際調(diào)用的方法為
Intrinsis.java中的

public static boolean areEqual(Object first, Object second) {
        return first == null ? second == null : first.equals(second);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末删壮,一起剝皮案震驚了整個濱河市贪绘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌醉锅,老刑警劉巖兔簇,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異硬耍,居然都是意外死亡垄琐,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門经柴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狸窘,“玉大人,你說我怎么就攤上這事坯认》埽” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵牛哺,是天一觀的道長陋气。 經(jīng)常有香客問我,道長引润,這世上最難降的妖魔是什么巩趁? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮淳附,結(jié)果婚禮上议慰,老公的妹妹穿的比我還像新娘。我一直安慰自己奴曙,他們只是感情好别凹,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著洽糟,像睡著了一般炉菲。 火紅的嫁衣襯著肌膚如雪堕战。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天颁督,我揣著相機與錄音践啄,去河邊找鬼。 笑死沉御,一個胖子當(dāng)著我的面吹牛屿讽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吠裆,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼伐谈,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了试疙?” 一聲冷哼從身側(cè)響起诵棵,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎祝旷,沒想到半個月后履澳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡怀跛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年距贷,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吻谋。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡忠蝗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出漓拾,到底是詐尸還是另有隱情阁最,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布骇两,位于F島的核電站速种,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏低千。R本人自食惡果不足惜配阵,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望栋操。 院中可真熱鬧,春花似錦饱亮、人聲如沸矾芙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剔宪。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間葱绒,已是汗流浹背感帅。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留地淀,地道東北人失球。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像帮毁,于是被迫代替她去往敵國和親实苞。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355