{"id":363402,"date":"2024-05-21T01:55:09","date_gmt":"2024-05-21T01:55:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=363402"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=363402","title":{"rendered":"<span>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 enum \u0432 kotlin\/jvm \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0410 \u0432\u044b \u0437\u043d\u0430\u043b\u0438, \u0447\u0442\u043e HashMap \u0434\u043b\u044f enum \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043f\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 EnumMap? \u0418\u043b\u0438 \u0447\u0442\u043e EnumSet \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 long?  \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0446\u0435\u043f\u0442\u043e\u0432 \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440.<\/p>\n<hr\/>\n<details class=\"spoiler\">\n<summary>\u041a\u043b\u0430\u0441\u0441\u044b \u043d\u0430\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">    enum class RoleType {         ACTOR,         COMMENTATOR,         VOICE_ACTOR,         DIRECTOR,         PRODUCER,         SINGER,         COMPOSER,     }      data class Person(         val name: String,         val type: RoleType,         val age: Int,     )<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 EnumSet<\/h2>\n<p>\u0422\u0438\u043f\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 (add, remove, contains, next) &#8212; \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0431\u0438\u0442\u043e\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043e\u043d\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0437\u0430 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0435\u043d\u0443\u043c\u043e\u0432 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u043c\u0435\u043d\u044c\u0448\u0435 64, \u0442\u0438\u043f\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; long<\/p>\n<p>\u0414\u043b\u044f \u0435\u043d\u0443\u043c\u043e\u0432 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0435 64, \u0442\u0438\u043f\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; \u043c\u0430\u0441\u0441\u0438\u0432 long<\/p>\n<p>\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 HashSet \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<h2>\u041d\u0435\u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 EnumSet<\/h2>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e EnumSet \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b  \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 EnumSet. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f EnumSet.copyOf(otherCollection), otherCollection \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u043e\u0439, \u0438\u043d\u0430\u0447\u0435 \u0431\u0440\u043e\u0441\u0438\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 EnumSet, \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e EnumSet.noneOf(RoleType::class.java).<\/p>\n<h2>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0435\u043d\u0443\u043c\u043e\u0432 \u0432 EnumSet<\/h2>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;reified T : Enum&lt;T>> Collection&lt;T>.toEnumSet(): EnumSet&lt;T> {     return if (this.isEmpty())         EnumSet.noneOf(T::class.java)     else         EnumSet.copyOf(this) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `map any collection to enumSet`() {     val rolesList = createPersons().map { it.type }     val allPersonRoles: Set&lt;RoleType> = rolesList.toEnumSet() }<\/code><\/pre>\n<h2>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432 EnumSet<\/h2>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0441\u043e\u043d\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 List&lt;PersonType>, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0431\u0440\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0448\u0430\u0433<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.mapToEnumSet(   crossinline transform: (T) -> R ): EnumSet&lt;R> = mapTo(EnumSet.noneOf(R::class.java), transform)<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `get all unique roles`() {     val team = createPersons()     val allPersonRoles: Set&lt;RoleType> = team         .filter { it.age > 40 }         .mapToEnumSet { it.type } }<\/code><\/pre>\n<h3>\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap<\/h3>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.groupByToEnumMap(     crossinline selector: (T) -> R ): EnumMap&lt;R, MutableList&lt;T>> {     return groupByTo(EnumMap(R::class.java), selector) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `group persons by role`() {     val team = createPersons()     val personsByRole: Map&lt;RoleType, List&lt;Person>> = team         .groupByToEnumMap { it.type } }<\/code><\/pre>\n<h4>\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap \u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c<\/h4>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>, U> Iterable&lt;T>.groupByToEnumMap(     crossinline selector: (T) -> R,     crossinline transform: (T) -> U, ): EnumMap&lt;R, MutableList&lt;U>> {     return groupByTo(EnumMap(R::class.java), selector, transform) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `group person names by role`() {   val team = createPersons()   val namesByRole: Map&lt;RoleType, List&lt;String>> = team     .groupByToEnumMap({ it.type }) { it.name } }<\/code><\/pre>\n<h4>\u0410\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap<\/h4>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.associateByToEnumMap(     crossinline selector: (T) -> R ): EnumMap&lt;R, T> {     return associateByTo(EnumMap(R::class.java), selector) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `associate by role`() {   val team = createPersons()   val roleToPersonMap: Map&lt;RoleType, Person> = team     .associateByToEnumMap { it.type } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781730\/\"> https:\/\/habr.com\/ru\/articles\/781730\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0410 \u0432\u044b \u0437\u043d\u0430\u043b\u0438, \u0447\u0442\u043e HashMap \u0434\u043b\u044f enum \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043f\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 EnumMap? \u0418\u043b\u0438 \u0447\u0442\u043e EnumSet \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 long?  \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0446\u0435\u043f\u0442\u043e\u0432 \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440.<\/p>\n<hr\/>\n<details class=\"spoiler\">\n<summary>\u041a\u043b\u0430\u0441\u0441\u044b \u043d\u0430\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">    enum class RoleType {         ACTOR,         COMMENTATOR,         VOICE_ACTOR,         DIRECTOR,         PRODUCER,         SINGER,         COMPOSER,     }      data class Person(         val name: String,         val type: RoleType,         val age: Int,     )<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 EnumSet<\/h2>\n<p>\u0422\u0438\u043f\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 (add, remove, contains, next) &#8212; \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0431\u0438\u0442\u043e\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043e\u043d\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0437\u0430 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0435\u043d\u0443\u043c\u043e\u0432 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u043c\u0435\u043d\u044c\u0448\u0435 64, \u0442\u0438\u043f\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; long<\/p>\n<p>\u0414\u043b\u044f \u0435\u043d\u0443\u043c\u043e\u0432 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0435 64, \u0442\u0438\u043f\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; \u043c\u0430\u0441\u0441\u0438\u0432 long<\/p>\n<p>\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 HashSet \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<h2>\u041d\u0435\u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 EnumSet<\/h2>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e EnumSet \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b  \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 EnumSet. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f EnumSet.copyOf(otherCollection), otherCollection \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u043e\u0439, \u0438\u043d\u0430\u0447\u0435 \u0431\u0440\u043e\u0441\u0438\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 EnumSet, \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e EnumSet.noneOf(RoleType::class.java).<\/p>\n<h2>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0435\u043d\u0443\u043c\u043e\u0432 \u0432 EnumSet<\/h2>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;reified T : Enum&lt;T>> Collection&lt;T>.toEnumSet(): EnumSet&lt;T> {     return if (this.isEmpty())         EnumSet.noneOf(T::class.java)     else         EnumSet.copyOf(this) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `map any collection to enumSet`() {     val rolesList = createPersons().map { it.type }     val allPersonRoles: Set&lt;RoleType> = rolesList.toEnumSet() }<\/code><\/pre>\n<h2>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432 EnumSet<\/h2>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0441\u043e\u043d\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 List&lt;PersonType>, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0431\u0440\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0448\u0430\u0433<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.mapToEnumSet(   crossinline transform: (T) -> R ): EnumSet&lt;R> = mapTo(EnumSet.noneOf(R::class.java), transform)<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `get all unique roles`() {     val team = createPersons()     val allPersonRoles: Set&lt;RoleType> = team         .filter { it.age > 40 }         .mapToEnumSet { it.type } }<\/code><\/pre>\n<h3>\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap<\/h3>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.groupByToEnumMap(     crossinline selector: (T) -> R ): EnumMap&lt;R, MutableList&lt;T>> {     return groupByTo(EnumMap(R::class.java), selector) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `group persons by role`() {     val team = createPersons()     val personsByRole: Map&lt;RoleType, List&lt;Person>> = team         .groupByToEnumMap { it.type } }<\/code><\/pre>\n<h4>\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap \u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c<\/h4>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>, U> Iterable&lt;T>.groupByToEnumMap(     crossinline selector: (T) -> R,     crossinline transform: (T) -> U, ): EnumMap&lt;R, MutableList&lt;U>> {     return groupByTo(EnumMap(R::class.java), selector, transform) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `group person names by role`() {   val team = createPersons()   val namesByRole: Map&lt;RoleType, List&lt;String>> = team     .groupByToEnumMap({ it.type }) { it.name } }<\/code><\/pre>\n<h4>\u0410\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0442\u0438\u043f\u0443 \u0432 EnumMap<\/h4>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;T, reified R : Enum&lt;R>> Iterable&lt;T>.associateByToEnumMap(     crossinline selector: (T) -> R ): EnumMap&lt;R, T> {     return associateByTo(EnumMap(R::class.java), selector) }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">@Test fun `associate by role`() {   val team = createPersons()   val roleToPersonMap: Map&lt;RoleType, Person> = team     .associateByToEnumMap { it.type } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781730\/\"> https:\/\/habr.com\/ru\/articles\/781730\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-363402","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=363402"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363402\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}