Search code examples
listkotlin

Kotlin - get unique list values for only certain filter from list


I have this piece of code:

/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */

class Person(val firstName: String, val lastName: String, var age: Int, var isEmployed: Boolean, var companyName: String)
{
    override fun toString(): String = firstName + " " + lastName + ", age:" + 
    age + ", isEmployed: " + isEmployed + ", companyName:" + companyName + "\n"
}


fun main() {
    
    var alist = emptyList<Person>()
    alist += (Person("Alan", "Walker", 23, true, "Google"))
    alist += (Person("Bee", "dog", 24, true, "Google"))
    alist += (Person("John", "Cena", 25, true, "Google"))
    alist += (Person("See", "S", 26, true, "Stackoverflow"))
    alist += (Person("Soya", "A", 27, true, "Stackoverflow"))
    alist += (Person("Zander", "Cage", 28, true, "Stackoverflow"))
    
    
    println(alist)
    
    var uniqueGoogle = alist.filter{ it.companyName.equals("Google") }.first()
    var newList = alist.filter{ !it.companyName.equals("Google") }
    newList = uniqueGoogle + newList
    
    println(newList)
}

I need list of Person from above list which are unique by company name only for Google. I am really bad at using list aggregations, needed some help here.

Expected output from println(newList):

[Alan Walker, age:23, isEmployed: true, companyName:Google
, See S, age:23, isEmployed: true, companyName:Stackoverflow
, Soya A, age:23, isEmployed: true, companyName:Stackoverflow
, Zander Cage, age:23, isEmployed: true, companyName:Stackoverflow
]

because from Google I only want only the first element. My solution does that but it doesn't seem very efficient or good to me and it is giving me some errors don't know why...


Solution

  • I think overall your code is not super bad, but it could be improved a bit. Many things your IDE probably already will suggest indicated by yellow squiggly underlines, you can hover your mouse over it to see what it is.

    This is an improvement to your code:

    fun main() {
    
        val alist = listOf(
            Person("Alan", "Walker", 23, true, "Google"),
            Person("Bee", "dog", 24, true, "Google"),
            Person("John", "Cena", 25, true, "Google"),
            Person("See", "S", 26, true, "Stackoverflow"),
            Person("Soya", "A", 27, true, "Stackoverflow"),
            Person("Zander", "Cage", 28, true, "Stackoverflow")
        )
    
        println(alist)
    
        val uniqueGoogle = alist.first { it.companyName == "Google" }
        val newList = listOf(uniqueGoogle) + alist.filter { it.companyName != "Google" }
    
        println(newList)
    }
    

    EDIT:
    A one-liner that would work in your case is:

    val newList = alist.distinctBy { if (it.companyName == "Google") true else it.hashCode() }
    

    Note that this won't work if your Person class was a data class and you also have duplicates in your list. Or any other case where multiple objects in the list have the same hashCode()