LinkedOverHashSet – Enhanced LinkedHashSet Implementation in Kotlin

An open-source Kotlin collection that maintains insertion order and repositions re-inserted elements

LinkedOverHashSet Kotlin Android Example
Introduction

LinkedOverHashSet is a Kotlin implementation of the LinkedHashSet class with an added property of maintaining the order of insertion of elements. In addition, when an element is re-inserted, it is stored at the last position of the set instead of its original position.LoggerUtil is a lightweight and developer-friendly logging utility built with Kotlin for Android applications. It helps manage logs efficiently, making debugging smoother and more structured.

Features

Preserves Insertion Order – Elements are stored in the order they are added.

Repositioning on Re-insertion – Re-inserting an existing element moves it to the end of the set.

Easy Integration – Seamlessly integrates into existing Kotlin projects with minimal setup.

Lightweight and Efficient – Designed for optimal performance with minimal overhead.

How to use

To use LinkedOverHashSet, you can simply create an instance of the LinkedOverHashSet class:

            val linkedOverHashSet = LinkedOverHashSet<Int>()
        

You can then add elements to the set using the add function:

            linkedOverHashSet.add(4)
linkedOverHashSet.add(3)
linkedOverHashSet.add(1)
linkedOverHashSet.add(2)
        

If an element is re-inserted, it will be removed from its original position and added to the last position of the set:

            linkedOverHashSet.add(3) // returns false
linkedOverHashSet.add(1) // returns false
linkedOverHashSet.add(4) // returns false
linkedOverHashSet.add(2) // returns false
        

You can iterate over the elements in the set using a for loop:

            for (x in linkedOverHashSet){
  println(x)
}
        

You can also use the popLastElement function to get and remove the last element of the set:

            val lastElement = linkedOverHashSet.popLastElement()
        
Crafted In
Repository

https://github.com/thesarangal/LinkedOverHashSet

Let’s Improve Together

Your reactions guide development. Please share your thoughts on new features or recommendations. Unitedly we can improve to help all.