Skip to content

Instantly share code, notes, and snippets.

@why168
Last active August 17, 2018 17:07
Show Gist options
  • Save why168/e4fc79a08c01082e0ee312b00de41af0 to your computer and use it in GitHub Desktop.
Save why168/e4fc79a08c01082e0ee312b00de41af0 to your computer and use it in GitHub Desktop.
简单的分组算法
internal object MapTest {
@JvmStatic
fun main(args: Array<String>) {
/*1、准备数据**/
val sku1 = Student(1L, "p1", 100L)
val sku2 = Student(2L, "p2", 101L)
val sku5 = Student(2L, "p5", 100L)
val sku3 = Student(3L, "p3", 102L)
val sku4 = Student(3L, "p3", 102L)
val sku6 = Student(5L, "p6", 100L)
val studentLists = Arrays.asList<Student>(sku1, sku2, sku3, sku4, sku5, sku6)
/*2、分组算法**/
val map = LinkedHashMap<Long, ArrayList<Student>>()
for (Student in studentLists) {
var tempList: ArrayList<Student>? = map[Student.age]
if (tempList == null) {
tempList = ArrayList()
tempList.add(Student)
map[Student.age] = tempList
} else {
// 如果Student.age重复,可以利用HashSet的特性进行去重
tempList.add(Student)
}
}
/*3、遍历map,验证结果**/
for (skuId in map.keys) {
println(map[skuId])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment