val persons = listOf( Person("Mary Hanes","Marketing"), Person("Steve Folley","Customer Service"), Person("John Ramsy","IT Help Desk"), Person("Erlick Foyes","Customer Service"), Person("Erin James","Marketing"), Person("Jacob Mays","IT Help Desk"), Person("Larry Cable","Customer Service") ) data class Department(val name: String) // Create Department objects for the departments by getting distinct values from Person.department val map = persons.groupBy { it.department } class MyView1 : View() { override val root = treeview(TreeItem(Person("Departments", ""))) { // Make sure the text in each TreeItem is the name of the Person test(map){ text = when (it) { is String -> it is Department -> it.name is Person -> it.name else -> throw IllegalArgumentException("Invalid value type") } } } } private class KeyVal(val key: K, val value: V) private fun TreeView.test(map: Map, root: TreeItem = this.root, formatter: TreeCell.(Any) -> Unit) { populate { parent -> val value = parent.value when { parent == root -> map.toList().mapEach { KeyVal(first, second) } value is KeyVal<*, *> -> value.value.let { when (it) { null -> null is List<*> -> it.filterNotNull().map { KeyVal(it, null) } is Map<*, *> -> it.toList().mapNotNull { (first, second) -> first?.let { KeyVal(it, second) } } else -> sequenceOf(KeyVal(it, null)).asIterable() } } else -> null } } cellFormat { it -> val value = (it as? KeyVal<*, *>)?.key ?: it formatter(this, value) } }