Last active
August 17, 2018 17:07
-
-
Save why168/e4fc79a08c01082e0ee312b00de41af0 to your computer and use it in GitHub Desktop.
简单的分组算法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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