
walkmc.extensions.collections.Sets.kt Maven / Gradle / Ivy
@file:Suppress("NOTHING_TO_INLINE")
package walkmc.extensions.collections
import java.util.*
/**
* Creates a new empty mutable set
*/
inline fun emptyMutableSet(): MutableSet = mutableSetOf()
/**
* Creates a new mutable set with the specified elements.
*/
inline fun newMutableSet(vararg elements: T): MutableSet = mutableSetOf(*elements)
/**
* Creates a new set with the specified elements.
*/
inline fun newSet(vararg elements: T): Set = setOf(*elements)
/**
* Utilizes a map structure to work with [set]. This utilizes [Collections] tool to create the set.
* This can be useful when you need a set that don't have a implementation of, and only has with maps.
*
* For example:
* ```
* val weakSet: MutableSet = newSetFromSet(WeakHashSet())
* val concurrentSet: MutableSet = newSetFromSet(ConcurrentHashSet())
* ```
*/
inline fun newSetFromMap(map: Map): MutableSet = Collections.newSetFromMap(map)
/**
* Creates a new empty tree set with a optional [comparator].
*/
fun treeSetOf(comparator: Comparator? = null) = TreeSet(comparator)
/**
* Creates a new empty tree set with the specified [comparer] with comparator.
*/
fun treeSetOf(comparer: (K) -> Comparable<*>) = TreeSet(compareBy(comparer))
/**
* Creates a new empty tree map with a optional [comparator].
*/
fun treeSetOf(vararg elements: K, comparator: Comparator? = null) =
TreeSet(comparator).also { it.addAll(elements.toSet()) }
/**
* Converts this map to a tree map.
*/
fun > Iterable.toTreeSet() = TreeSet(toSet())
/**
* Converts this map to a tree map with the specified [comparator].
*/
fun Iterable.toTreeSet(comparator: Comparator) =
TreeSet(comparator).also { it.addAll(this) }
/**
* Adds all specified [values] to this set.
*/
fun MutableSet.add(vararg values: T) = addAll(values.toList())
© 2015 - 2025 Weber Informatics LLC | Privacy Policy