0

I have an enum class thats something like this:

enum class SomeType(val id: String) {
    TYPE1("A"),
    TYPE2("B"),
    TYPE3("C"),
    TYPE4("D")
}

Now, I need to filter a list of Something which has a String that's stated in SomeType enum. So Basically I have something like this:

class Something(val id: String)
// where the value of id is one of the value of the SomeType's id

I have a list of Something like so:

val somethingList = arrayListOf<Something>(
    Something("A"),
    Something("B"),
    Something("A"),
    Something("C"),
    Something("D"),
    Something("A"),
    Something("D")
)

Now I need to filter that somethingList to by the given EnumSet<SomeType>.

So if I have a:

val someTypeSet = EnumSet.of(SomeType.Type3, SomeType.Type2)

the resulting filtered List should be,

val filteredList = arrayListOf<Something>(
    Something("B"),
    Something("C")
)

My idea is to convert the someTypeSet to a Set<String> and just do something like:

Set<String> setOfSomeTypeIds = convertToSet(someTypeSet)
val filteredList = somethingList.filter { something ->
    setOfSomeTypeIds.contains(something.id)
}

Can someone guide me how to convert an EnumSet to a Set of its value?

I also explained the whole process just in case there is a better solution to the problem above.

Anything will be appreciated.

Thanks in advance.

Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107
  • 1
    You can use `map` on any collection to transform it to a new collection with the desired values... i.e. `someTypeSet.map { it.id }` will already return you a list of string. If you really want to have a `Set` you can also use something like `mapTo`. Regarding the `filter` that might also be simplifiable using the `in`-keyword, e.g.: `somethingList.filter { it.id in setOfSomeTypeIds }` – Roland Apr 23 '19 at 09:08
  • Wow thanks! Can you post that as an answer so I could mark it. Also, is there a better Idea that doing what I did? – Archie G. Quiñones Apr 23 '19 at 09:12
  • 1
    the only better idea that comes to my mind is to reuse the enum type `SomeType` on the class `Something` itself... that way you could just write something like: `somethingList.filter { it.type in someTypeSet }` – Roland Apr 23 '19 at 09:20

2 Answers2

1

You can use the map function after you filter the relevant types.

val filteredSomethings:List<Something> = someTypeSet.filter { something ->
            setOfSomeTypeIds.contains(something.id) }.map { Something(it.id) }

It will return a List of Something with the relevant Ids.

Nizan Ifrach
  • 131
  • 3
1

You can use map on any collection to transform it to a new collection with the desired values... i.e. someTypeSet.map { it.id } will already return you a list of string. If you really want to have a Set you can also use something like mapTo. Regarding the filter that might also be simplifiable using the in-keyword, e.g.: somethingList.filter { it.id in setOfSomeTypeIds }.

So summarized:

val setOfSomeTypeIds = someTypeSet.map { it.id }

val filteredList = somethingList.filter { it.id in setOfSomeTypeIds }
Roland
  • 22,259
  • 4
  • 57
  • 84