Skip to content

A simple collection holder (eager or lazy) made to have the functionality of other languages

License

Notifications You must be signed in to change notification settings

joooKiwi/collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collection

This project has the sole purpose to have methods in a collection (list, array, set enumerable) with similar methods (or extension method).

It retrieves notions from Javascript, Java, Kotlin, PHP and C#.

Also, all the features are based on immutability.

(Maybe other languages will be integrated, only the future will tell it)

Table of content

Publication location

This project has only the Javascript / Typescript version uploaded.

But other languages will have different features based on how they can handle it.

Language Published
Javascript
Typescript
version
downloads
Java Maven
Kotlin Maven
PHP Composer
C# Nuget

Related projects

Here is a list of the related projects made by me

Contribution

You can contribute to my projects in 2 different ways

Equivalence depending on the language

Every method used in the project has a reference in one or another language. They are used differently across different languages. But they should result to the same thing in the end. They are meant to give a preview, but not always the most efficient.

The structure can sometime have an equivalent on other languages:

variable in a template Javascript Java Kotlin PHP C#
iterable Object with a [Symbol.iterator] Iterable Iterable / MutableIterable IteratorAggregate IEnumerable / IEnumerable / Enumerable
iterator Iterator Iterator Iterator / MutableIterator Iterator IEnumerator / IEnumerator
array Array Array Array Array ImmutableArray / Array
collection N/A Collection Collection / MutableCollection Collection IReadOnlyCollection / ICollection
list N/A List List / MutableList N/A IImmutableList / IReadOnlyList / IList
set Set / WeakSet Set Set / MutableSet Set IImmutableSet / IReadOnlySet / ISet

Quick note: For the C#, no external library other than the .NET is used (even though MoreLINQ is a fantastic library) .

Another note: Some parts may be incomplete due to some research that has to be made.

Size methods

The methods directly associated to a size

  • size|length|count()
  • isEmpty()
  • isNotEmpty()
get size()
get length()
get count()
Language Equivalent
Javascript Array.length
Set.size
Java array.length
Collection.size()
Kotlin array.size
Collection.size
PHP sizeof($array)
C# IEnumerable.Count()
ICollection.Count

get isEmpty()
Language Equivalent
Javascript array.length === 0
set.size === 0
Java array.length == 0
Collection.isEmpty()
Kotlin Array.isEmpty()
Collection.isEmpty()
PHP empty($array)
C# IEnumerable.Any()
get isNotEmpty()
Language Equivalent
Javascript array.length !== 0
set.size !== 0
Java array.length != 0
!collection.isEmpty()
Kotlin Array.isNotEmpty()
Collection.isNotEmpty()
PHP !empty($array)
C# !iEnumerable.Any()

Research methods

The methods are made to find an element or giving a value

  • get|at|elementAt(index)
  • getOrElse|atOrElse|elementAtOrElse(index, defaultValue)
  • getOrDefault|atOrDefault|elementAtOrDefault(index)
  • getOrNull|atOrNull|elementAtOrNull(index)
  • first(predicate?)
  • firstOrDefault(predicate?)
  • firstOrNull(predicate?)
  • last(predicate?)
  • lastOrDefault(predicate?)
  • lastOrNull(predicate?)
  • find(predicate?)
  • findIndexed(predicate?)
  • findLast(predicate?)
  • findLastIndexed(predicate?)
get(index)
Language Equivalent
Javascript
Java
Kotlin
PHP $array[$index]
C#
getOrElse(index, defaultValue)
Language Equivalent
Javascript index in array ? array[index] : defaultValue()
Java index < size ? array[index] : defaultValue()
Kotlin
PHP array_key_exists($index, $array) ? $array[$index] : defaultValue()
C# enumerable.ElementAtOrDefault(index) ?? defaultValue()
getOrDefault(index)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
getOrNull(index)
Language Equivalent
Javascript index in array ? array[index] : null
Java index <= size ? array[index] : null
Kotlin
PHP $array?[$index]
C# enumerable.ElementAtOrDefault(index) ?? null

first()
Language Equivalent
Javascript 0 in array ? array[0] : throw
Java collection.stream().findFirst().orElseThrow()
Kotlin
PHP
C#
first(predicate)
Language Equivalent
Javascript array.find(predicate,) ?? throw
Java collection.stream().findFirst().or(predicate).orElseThrow()
Kotlin
PHP
C#
firstOrDefault()
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
firstOrDefault(predicate)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
firstOrNull()
Language Equivalent
Javascript 0 in array ? array[0] : null
Java collection.stream().findFirst().orElse(null)
Kotlin
PHP
C#
firstOrNull(predicate)
Language Equivalent
Javascript array.find(predicate,) ?? null
Java collection.stream().findFirst().or(predicate).orElse(null)
Kotlin
PHP
C#

last()
Language Equivalent
Javascript size - 1 in array[size - 1] : throw
Java collection.stream().reduce((_, it) -> it).orElseThrow()
Kotlin
PHP
C#
last(predicate)
Language Equivalent
Javascript array.findLast(predicate,) ?? throw
Java collection.stream().reduce((_, it) -> it).or(predicate).orElseThrow()
Kotlin
PHP
C#
lastOrDefault()
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
lastOrDefault(predicate)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
lastOrNull()
Language Equivalent
Javascript size - 1 in array ? array[size - 1] : null
Java collection.stream().reduce((_, it) -> it).orElse(null)
Kotlin
PHP
C#
lastOrNull(predicate)
Language Equivalent
Javascript array.findLast(predicate,) ?? null
Java collection.stream().reduce((_, it) -> it).or(predicate).orElse(null)
Kotlin
PHP
C#

find(predicate)
findIndexed(predicate)
Language Equivalent
Javascript
Java collection.stream().filter(predicate).findFirst().orElse(null)
Kotlin
PHP
C#
findLast(predicate)
findLastIndexed(predicate)
Language Equivalent
Javascript
Java collection.stream().filter(predicate).reduce((_, it) -> it).orElse(null)
Kotlin
PHP
C#

Index methods

The methods are made to find an index

  • indexOf(element, fromIndex?, toIndex?)
  • lastIndexOf(element, fromIndex?, toIndex?)
  • indexOfFirst|findIndex(predicate, fromIndex?, toIndex?)
  • indexOfFirstIndexed|findIndexIndexed(predicate, fromIndex?, toIndex?)
  • indexOfLast|findLastIndex(predicate, fromIndex?, toIndex?)
  • indexOfLastIndexed|findLastIndexIndexed(predicate, fromIndex?, toIndex?)
indexOf(element)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
indexOf(element, fromIndex)
LanguageEquivalent
Javascript
Java
final var startingIndex = calculate-starting-index(fromIndex);
var index = startingIndex - 1;
while (++index < size)
    if (array[index] == element)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
var index = startingIndex - 1
while (++index < size)
    if (element in array[index])
        return index
return null
PHP
C#
indexOf(element, fromIndex, toIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex)
const endingIndex = calculate-ending-index(toIndex)
let index = startingIndex - 1
while (++index <= endingIndex)
    if (array[index] === element)
        return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var endingIndex = calculate-ending-index(toIndex);
var index = startingIndex - 1;
while (++index <= endingIndex)
    if (array[index] == element)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
val endingIndex = calculate-ending-index(toIndex)
var index = startingIndex - 1
while (++index <= endingIndex)
    if (element in array[index])
        return index
return null
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var endingIndex = calculate-ending-index(toIndex);
var index = startingIndex - 1;
while (++index <= endingIndex)
    if (array[index] == element)
        return index;
return null;

lastIndexOf(element)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
lastIndexOf(element, fromIndex)
LanguageEquivalent
Javascript
Java
final var startingIndex = calculate-starting-index(fromIndex);
var index = size;
while (--index >= startingIndex)
    if (array[index] == element)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
var index = size
while (--index >= startingIndex)
    if (element in array[index])
        return index
return null
PHP
C#
lastIndexOf(element, fromIndex, toIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex)
const endingIndex = calculate-ending-index(toIndex)
var index = endingIndex + 1
while (--index >= startingIndex)
    if (array[index] == element)
        return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var endingIndex = calculate-ending-index(toIndex);
var index = endingIndex + 1;
while (--index >= startingIndex)
    if (array[index] == element)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
val endingIndex = calculate-ending-index(toIndex)
var index = endingIndex + 1
while (--index >= startingIndex)
    if (element in array[index])
        return index
return null
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var endingIndex = calculate-ending-index(toIndex);
var index = endingIndex + 1;
while (--index >= startingIndex)
    if (array[index] == element)
        return index;
return null;

indexOfFirst(predicate)
indexOfFirstIndexed(predicate)
LanguageEquivalent
Javascript
Java
var index = -1;
while (++index < size)
    if (predicate)
        return index;
return null;
Kotlin
PHP
C#
indexOfFirst(predicate, fromIndex)
indexOfFirstIndexed(predicate, fromIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex)
let index = startingIndex - 1
while (++index < size)
    if (predicate)
        return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
var index = startingIndex - 1;
while (++index < size)
    if (predicate)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
var index = startingIndex - 1
while (++index < size)
    if (predicate)
        return index
return null
PHP
C#
indexOfFirst(predicate, fromIndex, toIndex)
indexOfFirstIndexed(predicate, fromIndex, toIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex,)
const endingIndex = calculate-ending-index(toIndex,)
let index = startingIndex - 1
while (++index <= endingIndex)
    if (predicate)
        return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var endingIndex = calculate-ending-index(toIndex);
var index = startingIndex - 1;
while (++index <= endingIndex)
    if (predicate)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
val endingIndex = calculate-ending-index(toIndex)
var index = startingIndex - 1
while (++index <= endingIndex)
    if (predicate)
        return index
return null
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var endingIndex = calculate-ending-index(toIndex);
var index = startingIndex - 1;
while (++index <= endingIndex)
    if (predicate)
        return index;
return null;

indexOfLast(predicate)
indexOfLastIndexed(predicate)
LanguageEquivalent
Javascript
Java
var index = size;
while (--index > 0)
    if (predicate)
        return index;
return null;
Kotlin
PHP
C#
indexOfLast(predicate, fromIndex)
indexOfLastIndexed(predicate, fromIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex,)
let index = size
while (--index >= startingIndex)
    if (predicate)
            return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
var index = size;
while (--index >= startingIndex)
    if (predicate)
            return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
var index = size
while (--index >= startingIndex)
    if (predicate)
            return index
return null
PHP
C#
indexOfLast(predicate, fromIndex, toIndex)
indexOfLastIndexed(predicate, fromIndex, toIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex,)
const endingIndex = calculate-ending-index(toIndex,)
let index = endingIndex + 1
while (--index >= startingIndex)
    if (predicate)
        return index
return null
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var endingIndex = calculate-ending-index(toIndex);
var index = endingIndex + 1;
while (--index >= startingIndex)
    if (predicate)
        return index;
return null;
Kotlin
val startingIndex = calculate-starting-index(fromIndex)
val endingIndex = calculate-ending-index(toIndex)
var index = endingIndex + 1
while (--index >= startingIndex)
    if (predicate)
        return index
return null
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var endingIndex = calculate-ending-index(toIndex);
var index = endingIndex + 1;
while (--index >= startingIndex)
    if (predicate)
        return index;
return null;

Validation methods

The methods are made to do validation on type, value or comparison

  • all()
  • any(predicate?)
  • none(predicate?)
  • hasNull|containsNull|includesNull()
  • hasDuplicate|containsDuplicate|includesDuplicate()
  • has|contains|includes(value)
  • hasOne|containsOne|includesOne(values)
  • hasAll|containsAll|includesAll(values)
  • requireNoNulls()
all(predicate)
Language Equivalent
Javascript
Java collection.stream().allMatch(predicate)
Kotlin
PHP
C#
any()
Language Equivalent
Javascript
  • array.length !== 0
  • set.size !== 0
Java
  • array.length == 0
  • collection.isEmpty()
Kotlin
PHP
C#
any(predicate)
Language Equivalent
Javascript
Java collection.stream().anyMatch(predicate)
Kotlin
PHP
C#
none()
Language Equivalent
Javascript
  • array.length === 0
  • set.size === 0
Java
  • array.length == 0
  • !collection.isEmpty()
Kotlin
PHP
C#
  • !enumerable.Any()
none(predicate)
Language Equivalent
Javascript array.some(!predicate)
Java collection.stream().noneMatch(predicate)
Kotlin
PHP
C# enumerable.Any(!predicate)

hasNull()
Language Equivalent
Javascript array.includes(null,)
Java collection.contains(null)
Kotlin N/A
PHP in_array(null, $array, true)
C# enumerable.Contains(null)
list.Contains(null)
set.Contains(null)
hasDuplicate()
LanguageEquivalent
Javascript
const temporaryArray = new Array(size,)
temporaryArray[0] = array[0]
let amountOfItemAdded = 1
let index = 0
while (++index < size) {
    const value = array[index]
    let index2 = -1
    while (++index2 < amountOfItemAdded)
        if (temporaryArray[index2] === value)
            return true
  temporaryArray[amountOfItemAdded++] = value
}
return false
Java
final var temporaryArray = (T[]) new Object[size]
temporaryArray[0] = array[0]
var amountOfItemAdded = 1
var index = 0
while (++index < size) {
    final var value = array[index]
    var index2 = -1
    while (++index2 < amountOfItemAdded)
        if (temporaryArray[index2] == value)
            return true
  temporaryArray[amountOfItemAdded++] = value
}
return false
Kotlin
val temporaryArray = arrayOfNulls<T>(size,)
temporaryArray[0] = array[0]
var amountOfItemAdded = 1
var index = 0
while (++index < size) {
    val value = array[index]
    var index2 = -1
    while (++index2 < amountOfItemAdded)
        if (temporaryArray[index2] == value)
            return true
  temporaryArray[amountOfItemAdded++] = value
}
return false
PHP
C#
var temporaryArray = new T[size]
temporaryArray[0] = array[0]
var amountOfItemAdded = 1
var index = 0
while (++index < size) {
    var value = array[index]
    var index2 = -1
    while (++index2 < amountOfItemAdded)
        if (temporaryArray[index2] == value)
            return true
  temporaryArray[amountOfItemAdded++] = value
}
return false

has(value)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
hasOne(values)
Language Equivalent
Javascript array.some(it => values.includes(it,),)
Java list.stream().anyMatch(values::contains)
Kotlin array.any { it in values }
PHP
C# enumerable.Any(it => values.Contains(it))
hasAll(values)
Language Equivalent
Javascript array.every(it => values.includes(it,),)
Java
Kotlin
PHP
C# enumerable.All(it => values.Contains(it))

requireNoNulls()
LanguageEquivalent
Javascript
let index = -1
while (++index < size)
    if (array[index] == null)
        throw forbidden-exception
Java
var index = -1;
while (++index < size)
    if (array[index] == null)
        throw forbidden-exception;
Kotlin
PHP
$index = -1;
while (++$index < $size)
    if ($array[$index] == null)
        throw forbidden-exception
C#
var index = -1;
while (++index < size)
    if (array[index] == null)
        throw

Transformation methods

The methods are made to transform the structure by different type or size

  • filter(predicate)
  • filterIndexed(predicate)
  • filterNot(predicate)
  • filterNotIndexed(predicate)
  • filterNotNull()
  • slice(indices)
  • slice(fromIndex?, toIndex?)
  • take(n)
  • takeWhile(predicate)
  • takeWhileIndexed(predicate)
  • takeLast(n)
  • takeLastWhile(predicate)
  • takeLastWhileIndexed(predicate)
  • drop(n)
  • dropWhile(predicate)
  • dropWhileIndexed(predicate)
  • dropLast(n)
  • dropLastWhile(predicate)
  • dropLastWhileIndexed(predicate)
  • map(transform)
  • mapIndexed(transform)
  • mapNotNull(transform)
  • mapNotNullIndexed(transform)
filter(predicate)
Language Equivalent
Javascript
Java collection.stream().filter(predicate).toList()
Kotlin
PHP
C#
filterIndexed(predicate)
Language Equivalent
Javascript
Java collection.stream().filter(predicate).toList()
Kotlin
PHP
C#
filterNot(predicate)
filterNotIndexed(predicate)
Language Equivalent
Javascript array.filter(!predicate)
Java collection.stream().filter(!predicate).toList()
Kotlin
PHP array_filter($array, !$predicate)
C# enumerable.Where(!predicate)
filterNotNull()
Language Equivalent
Javascript array.filter(it => it != null,)
Java collection.stream().filter(it -> it != null).toList()
Kotlin
PHP array_filter($array, fn(it) => $array[$it] != null,)
C# enumerable.Where(it => it != null)

slice(indices)
LanguageEquivalent
Javascript
const newArray = new Array(indiceSize,)
let index = indiceSize
while (index-- > 0)
    newArray[index] = array[indices[index]]
return newArray
Java
final var newArray = (T[]) new Object[indiceSize];
var index = indiceSize;
while (index-- > 0)
    newArray[index] = array[indices[index]];
return newArray;
Kotlin
PHP
C#
var newArray = new T[indiceSize];
var index = indiceSize;
while (index-- > 0)
    newArray[index] = array[indices[index]];
return newArray;
slice()
Language Equivalent
Javascript Array.slice()
Java array
Kotlin array
PHP
C# array
slice(fromIndex)
Language Equivalent
Javascript Array.slice(fromIndex)
Java Arrays.copyOfRange(array, fromIndex, size - 1)
Kotlin array[fromIndex..]
array.slice(fromIndex..)
PHP
C# array[fromIndex..^0]
slice(fromIndex, toIndex)
Language Equivalent
Javascript Array.slice(fromIndex, toIndex)
Java Arrays.copyOfRange(array, fromIndex, toIndex)
Kotlin array[fromIndex..toIndex]
array.slice(fromIndex..toIndex)
PHP
C# array[fromIndex..toIndex]

take(n)
Language Equivalent
Javascript array.slice(0, n)
Java
Kotlin
PHP
C#
takeWhile(predicate)
takeWhileIndexed(predicate)
Language Equivalent
Javascript array.filter(predicate)
Java
Kotlin
PHP
C#
takeLast(n)
Language Equivalent
Javascript array.slice(-n)
Java
Kotlin
PHP
C#
takeLastWhile(predicate)
takeLastWhileIndexed(predicate)
LanguageEquivalent
Javascript
let index = size
while (--index >= 0)
    if (!predicate) {
        const newArrayFromIndexToLast = new Array(size - index - 1,)
        let indexAdded = 0
        while (++index < size)
            newArrayFromIndexToLast[indexAdded++] = array[index]
        return newArrayFromIndexToLast
    }
return newArray
Java
Kotlin
PHP
C#

drop(n)
Language Equivalent
Javascript array.slice(n)
Java
Kotlin Array.drop(n)
List.drop(n)
PHP
C#
dropWhile(predicate)
dropWhileIndexed(predicate)
Language Equivalent
Javascript array.filter(!predicate)
Java
Kotlin Array.dropWhile(predicate)
List.dropWhile(predicate)
PHP
C#
dropLast(n)
Language Equivalent
Javascript array.slice(0, -n)
Java
Kotlin Array.dropLast(n)
List.dropLast(n)
PHP
C#
dropLastWhile(predicate)
dropLastWhileIndexed(predicate)
LanguageEquivalent
Javascript
let index = size
while (--index >= 0)
    if (!predicate) {
        const newArrayFrom0ToIndex = new Array(index + 1,)
        let index2 = -1
        while (++index2 <= index)
            newArrayFrom0ToIndex[index2] = array[index2]
        return newArrayFrom0ToIndex
    }
return []
Java
Kotlin
PHP
C#

map(transform)
Language Equivalent
Javascript
Java collection.stream().map(transform).toList()
Kotlin
PHP
C#
mapIndexed(transform)
Language Equivalent
Javascript
Java collection.stream().map(transform).toList()
Kotlin
PHP
C#
mapNotNull(transform)
Language Equivalent
Javascript array.map(it => it != null,)
Java collection.stream().map(it -> it != null).toList()
Kotlin
PHP array_map(fn(it,) => it != null, $array)
C# enumerable.Select(it it != null)
mapNotNullIndexed(transform)
Language Equivalent
Javascript array.map(it => it != null,)
Java collection.stream().map(it -> it != null).toList()
Kotlin
PHP array_map(fn(it,) => it != null, $array)
C# enumerable.Select(it it != null)

Loop methods

The methods are basically an embedded loop

  • forEach(action)
  • forEachIndexed(action)
  • onEach(action)
  • onEachIndexed(action)
forEach(action)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#
forEachIndexed(action)
Language Equivalent
Javascript
Java
Kotlin
PHP
C#

onEach(action)
Language Equivalent
Javascript array.forEach(action,)
return array
Java array.forEach(action);
return array;
Kotlin
PHP array_walk(&$array, $action,)
return $array;
C# list.forEach(action);
return list;
onEachIndexed(action)
Language Equivalent
Javascript array.forEach(action,)
return array
Java array.forEach(action);
return array;
Kotlin
PHP array_walk(&$array, $action,)
return $array;
C# list.forEach(action);
return list;

Reordering methods

The methods are there to reorder the values

  • toReverse|toReversed|reversed(fromIndex?, toIndex?)
toReverse()
Language Equivalent
Javascript
Java Collections.reverse(list)
Kotlin
PHP
C#
toReverse(fromIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex,)
const newArray = new Array(size - 1 - startingIndex,)
let indexAdded = -1
let index = size
while (--index >= startingIndex)
    newArray[++indexAdded] = collection.get(index,)
return newArray
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var newArray = (T[]) new Object[size - 1 - startingIndex];
var indexAdded = -1;
var index = size;
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index];
return newArray;
Kotlin
val startingIndex = calculate-starting-index(fromIndex,)
val newArray = arrayOfNulls<T>(size - 1 - startingIndex,)
var indexAdded = -1
var index = size
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index]
return newArray as T[]
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var newArray = new T[size - 1 - startingIndex];
var indexAdded = -1;
var index = size;
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index];
return newArray;
toReverse(fromIndex, toIndex)
LanguageEquivalent
Javascript
const startingIndex = calculate-starting-index(fromIndex,)
const endingIndex = calculate-ending-index(toIndex,)
const newArray = new Array(endingIndex - startingIndex,)
let indexAdded = -1
let index = endingIndex + 1
while (--index >= startingIndex)
    newArray[++indexAdded] = collection.get(index,)
return newArray
Java
final var startingIndex = calculate-starting-index(fromIndex);
final var endingIndex = calculate-ending-index(toIndex);
final var newArray = (T[]) new Object[endingIndex - startingIndex];
var indexAdded = -1;
var index = endingIndex + 1;
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index];
return newArray;
Kotlin
val startingIndex = calculate-starting-index(fromIndex,)
val endingIndex = calculate-ending-index(toIndex,)
val newArray = arrayOfNulls<T>(endingIndex - startingIndex,)
var indexAdded = -1
var index = endingIndex + 1
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index]
return newArray as T[]
PHP
C#
var startingIndex = calculate-starting-index(fromIndex);
var endingIndex = calculate-ending-index(toIndex);
var newArray = new T[endingIndex - startingIndex];
var indexAdded = -1;
var index = endingIndex + 1;
while (--index >= startingIndex)
    newArray[++indexAdded] = array[index];
return newArray;

Conversion methods

The methods are here to convert the structure to another kind depending on the language

To iterator/enumerator

This is for the structure that can use the language looping mechanism

Language Equivalence
Javascript
Java
Kotlin
PHP
C#

To (mutable) array

This is the most bare-bones structure

  • Javascript Array (toArray / toMutableArray)
  • Java T[] (toArray)
  • Kotlin Array (toArray)
  • PHP Array
  • C# T[] (toArray)
Language Equivalence
toArray
Equivalence
toMutableArray
Javascript Object.freeze([...iterable,],) [...iterable,]
Java N/A
Kotlin N/A
PHP
C# new ReadOnlyCollection<T>(enumerable)

To (mutable) collection
Language Equivalent
toCollection
Equivalent
toMutableCollection
Javascript
Java
Kotlin mutableArrayOf(*iterable)
mutableSetOf(*iterable)
PHP
C# new ReadOnlyCollection<T>(enumerable) enumerable.toList()
enumerable.toHashSet()
To (mutable) list
Language Equivalent
toList
Equivalent
toMutableList
Javascript
Java new ArrayList<>(collection)
new CopyOnWriteArrayList<>(collection)
new LinkedList<>(collection)
new Vector<>(collection)
Kotlin
PHP
C# new ReadOnlyCollection<T>(enumerable)
To (mutable) set
Language Equivalent
toSet
Equivalent
toMutableSet
Javascript Object.freeze(new Set(iterable,),)
Object.freeze(new WeakSet(iterable,),)
new Set(iterable,)
new WeakSet(iterable,)
Java new ConcurrentSkipListSet<>(collection)
new CopyOnWriteArraySet<>(collection)
new HashSet<>(collection)
new LinkedHashSet<>(collection)
new TreeSet<>(collection)
Kotlin
PHP
C# new ReadOnlyCollection<T>(enumerable)

To (mutable) map/dictionary
Language Equivalent
toMap
Equivalent
toMutableMap
Javascript Object.freeze(new Map(array.map((it, index,) => [it, index,],),),) new Map(array.map((it, index,) => [it, index,],),)
Java list.stream().collect(Collectors.toUnmodifiableMap(list::indexOf, it -> it)) list.stream().collect(Collectors.toMap(list::indexOf, it -> it))
Kotlin iterable.mapIndexed { index, i -> index to i }.toMap() iterable.mapIndexed { index, i -> index to i }.toMap().toMutableMap()
PHP
C# new ReadOnlyCollection<T>(enumerable)

joinToString()
Language Equivalent
Javascript `[${array.join(", ",)}]`
Java array.join(", ")
Kotlin Array.joinToString()
Iterable.joinToString()
PHP
C# '[' + string.Join(", ", array, 0, size - 1) + ']'
'[' + Join(", ", enumerable) + ']'
joinToString(separator)
Language Equivalent
Javascript `[${Array.join(separator)}]`
Java String.join(separator, iterable)
Kotlin Array.joinToString(separator)
Iterable.joinToString(separator)
PHP
C# '[' + string.Join(separator, array, 0, size - 1) + ']'
'[' + Join(separator, enumerable) + ']'`
joinToString(separator, prefix)
Language Equivalent
Javascript `${prefix}${Array.join(separator)}]`
Java
Kotlin Array.joinToString(separator, prefix)
Iterable.joinToString(separator, prefix)
PHP
C# prefix + string.Join(separator, array, 0, size - 1) + ']'
prefix + Join(separator, enumerable) + ']'`
joinToString(separator, prefix, postfix)
Language Equivalent
Javascript `${prefix}${Array.join(separator)}${postfix}`
Java
Kotlin Array.joinToString(separator, prefix, postfix)
Iterable.joinToString(separator, prefix, postfix)
PHP
C# prefix + string.Join(separator, array, 0, size - 1) + postfix
prefix + Join(separator, enumerable) + postfix`
joinToString(separator, prefix, postfix, limit, truncated)
Language Equivalent
Javascript
Java
Kotlin Array.joinToString(separator, prefix, postfix, limit, truncated)
Iterable.joinToString(separator, prefix, postfix, limit, truncated)
PHP
C#
joinToString(separator, prefix, postfix, limit, truncated, transform)
Language Equivalent
Javascript
Java
Kotlin Array.joinToString(separator, prefix, postfix, limit, truncated, transform)
Iterable.joinToString(separator, prefix, postfix, limit, truncated, transform)
PHP
C#

Version history

JS/TS Date Quick note
1.10.0 October 8th, 2024 New methods take, takeWhile, takeLast, takeLastWhile, drop, dropWhile, dropLast and dropLastWhile,
New aliases some and every,
Deprecation of the argument limit where there is a fromIndex and toIndex,
Addition of Array to be handled in the methods
1.9.3 August 15th, 2024 Fix on the missing export for filterNotIndexed
1.9.2 July 23rd, 2024 Fix on an array of 2 on MinimalistCollectionHolder,
Fix on the CollectionIterator to handle properly when it has 2 values
1.9.1 July 21st, 2024 Spread of the deprecation across the inheritor of CollectionHolder
1.9.0 July 21st, 2024 Addition of hasDuplicate, joinToString for the CollectionHolder,
Change on the CollectionIterator to use NullableNumber instead of number for the indexes,
Addition of implementation for the ValueHolder,
Deprecation of variadic parameters for has, hasAll & hasOne,
The abstract implementations no longer have fields held,
lazy (1.4.01.5.0)
1.8.0 March 31st, 2024 Addition of type to the dependency
1.7.1 February 19th, 2024 Change from == to === on the GenericCollectionIterator
1.7.0 February 19th, 2024 New implementation of MinimalistCollectionHolder,
New method has,
New aliases contains, includes, reversed, toReversed,
New names to the CollectionHandlers,
lazy (1.2.11.3.0)
1.6.1 December 23rd, 2023 Addition of the missing amd (asynchronous module definition) package
1.6.0 December 4th, 2023 New implementation based on the amount of arguments received in a callback,
New method variant of toString,
Deprecation (for removal) of the newInstance
1.5.0 September 28th, 2023 The RangeError and ReferenceError has been changed to custom exceptions,
lazy (1.1.01.2.0)
1.4.0 September 8th, 2023 Addition of a limit on every methods where it has fromIndex and toIndex,
Change on the toIndex to be inclusive instead of exclusive,
New methods mapNotNull, mapNotNullIndexed and slice,
New aliases elementAt, elementAtOrNull and elementAtOrElse
1.3.0 August 14th, 2023 Small changes on some utility methods,
Addition of nextValue and previousValue to the CollectionIterator
1.2.0 July 27th, 2023 Fix on a "for‥of" not working properly
1.1.0 July 23rd, 2023 Addition of a new CollectionIterator
1.0.4 July 2nd, 2023 There were recursive import for the EmptyCollectionHolder from the CollectionConstants
1.0.3 July 1st, 2023 For some reason, the file CollectionHolder had a Symbol not declared
1.0.2 July 1st, 2023 Small fix on the package.json
1.0.1 July 1st, 2023 An update based on the new lazy version
1.0.0 July 1st, 2023 The first version,
It was originally on the enumeration project

About

A simple collection holder (eager or lazy) made to have the functionality of other languages

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published