fun main() {
// Adding messages to arrayList
val myMsg = arrayListOf(
Message(1, "A", "10-02-2022"),
Message(2, "B", "10-02-2022"),
Message(3, "C", "11-02-2022"),
Message(4, "D", "11-02-2022"),
Message(5, "E", "11-02-2022"),
Message(6, "F", "12-02-2022"),
Message(7, "G", "12-02-2022"),
Message(8, "H", "12-02-2022"),
Message(9, "I", "14-02-2022"),
Message(10, "J", "14-02-2022")
)
// Getting date wise messages
val dateWiseMessageList = myMsg.groupBy { it.date }.values
println("Total sections are ${dateWiseMessageList.size}")
val myMessageMap = HashMap<Int, List<Message>>()
// Storing date wise messages to HashMap with Key and Value as multiple messages
for ((index, value) in dateWiseMessageList.withIndex()) {
println("the element at $index is $value")
myMessageMap[index] = value
}
// Retrieve messages in date wise section
for ((key, value) in myMessageMap) {
println("Section $key data are")
for (i in value.indices) {
println(" Id = ${value[i].id} Message = ${value[i].msg} Date = ${value[i].date}")
}
}
}
消息数据类
data class Message(val id: Int, val msg: String, val date: String)