Loading repository data…
Loading repository data…
Karelaking / repository
ts-collections is a robust, Java-inspired collections framework for TypeScript, designed to provide developers with a powerful, type-safe, and feature-rich library for managing data structures. It is built with modern TypeScript features, ensuring high performance, scalability, and adherence to best practices.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
ts-collections is a Java-inspired collections framework and library that brings enterprise-grade data structures to TypeScript. Built with modern TypeScript features, it provides automatic type safety, familiar APIs for Java developers, and zero-configuration setup.
import { ArrayList, HashMap, HashSet } from "ts-collections";
// Automatic type safety - no configuration needed!
const list = new ArrayList<number>();
list.add(42); // ✅ Works
list.add("text" as any); // ❌ Runtime error (caught automatically)
// Familiar Java-style APIs
const map = new HashMap<string, User>();
map.put("john", user);
const value = map.get("john");
// Complete type inference
const set = new HashSet<string>();
set.add("unique");
Runtime type checking enabled by default - no configuration needed. Works just like Java's type-safe collections.
Type safety works out of the box. No need to learn validation libraries or configure schemas.
Familiar Collections interface for Java developers. If you know Java Collections, you already know ts-collections.
Easy to create custom implementations through abstract base classes and clear interfaces.
All operations documented with time/space complexity (O-notation).
332/332 tests passing. Comprehensive test coverage for all implementations.
You'll feel right at home! Type safety works exactly like Java:
// Java
List<Integer> list = new ArrayList<>();
list.add(1);
list.add("text"); // ❌ Compile error
// TypeScript with ts-collections
const list = new ArrayList<number>();
list.add(1);
list.add("text" as any); // ❌ Runtime error (automatic!)
| Interface | Description | Key Methods |
|---|---|---|
| Collection<E> | Base interface for all collections | add(), remove(), size(), contains() |
| List<E> | Ordered, indexed collection | get(), set(), indexOf(), subList() |
| Set<E> | Unique elements, no duplicates | Inherits Collection with uniqueness |
| Map<K,V> | Key-value associations | put(), get(), containsKey(), entries() |
| Queue<E> | FIFO processing | offer(), poll(), peek() |
| Type | Implementation | Characteristics |
|---|---|---|
| List | ArrayList<E> | Dynamic array, fast random access O(1) |
| List | LinkedList<E> | Doubly-linked, fast insertion/deletion |
| Set | HashSet<E> | Hash table, fast lookup O(1) average |
| Set | TreeSet<E> | Sorted set, comparator-based ordering |
| Map | HashMap<K,V> | Hash table, fast key lookup O(1) average |
| Map | TreeMap<K,V> | Sorted map, comparator-based ordering |
| Queue | LinkedQueue<E> | Linked list, FIFO operations O(1) |
| Queue | PriorityQueue<E> | Heap-based priority ordering |
| Deque | LinkedDeque<E> | Double-ended queue, add/remove at both ends |
This table helps developers quickly map ts-collections data structures to familiar native JavaScript alternatives.
| ts-collections | Native JavaScript Equivalent |
|---|---|
ArrayList | Array |
HashMap | Map |
HashSet | Set |
LinkedQueue | Array (queue pattern) |
PriorityQueue | No direct equivalent |
TreeMap | No direct equivalent |
TreeSet | No direct equivalent |
graph TD
A[Collection Interface] --> B[List]
A --> C[Set]
A --> D[Queue]
E[Map Interface] --> F[HashMap]
B --> G[ArrayList]
B --> H[LinkedList]
C --> I[HashSet]
D --> J[LinkedQueue]
npm i @karelaking/ts-collections
pnpm i @karelaking/ts-collections
# Using pnpm
pnpm add ts-collections
# Using yarn
yarn add ts-collections
import { ArrayList } from "ts-collections";
const list = new ArrayList<number>();
list.add(10);
list.add(20);
list.add(30);
console.log(list.get(0)); // 10
console.log(list.size()); // 3
list.removeAt(1); // [10, 30]
import { HashSet } from "ts-collections";
const set = new HashSet<string>();
set.add("apple");
set.add("banana");
set.add("apple"); // Duplicate ignored
console.log(set.size()); // 2
console.log(set.contains("apple")); // true
import { HashMap } from "ts-collections";
const map = new HashMap<string, number>();
map.put("Alice", 25);
map.put("Bob", 30);
console.log(map.get("Alice")); // 25
console.log(map.containsKey("Bob")); // true
import { LinkedQueue } from "ts-collections";
const queue = new LinkedQueue<string>();
queue.offer("Task 1");
queue.offer("Task 2");
console.log(queue.poll()); // "Task 1"
console.log(queue.peek()); // "Task 2"
import { ArrayList } from "ts-collections";
const list = new ArrayList<number>();
list.add(1);
list.add(2);
list.add(3);
const iterator = list.iterator();
while (iterator.hasNext()) {
console.log(iterator.next());
}
import { PriorityQueue } from "ts-collections";
const priorities = new PriorityQueue<number>();
priorities.offer(5);
priorities.offer(1);
priorities.offer(3);
console.log(priorities.poll()); // 1
console.log(priorities.peek()); // 3
import { TreeMap } from "ts-collections";
const sortedMap = new TreeMap<string, number>();
sortedMap.put("c", 3);
sortedMap.put("a", 1);
sortedMap.put("b", 2);
console.log(sortedMap.keys()); // ["a", "b", "c"]
import { TreeSet } from "ts-collections";
const sortedSet = new TreeSet<number>();
sortedSet.add(3);
sortedSet.add(1);
sortedSet.add(2);
console.log(sortedSet.toArray()); // [1, 2, 3]
import { LinkedDeque } from "ts-collections";
const deque = new LinkedDeque<string>();
deque.addFirst("middle");
deque.addFirst("front");
deque.addLast("back");
console.log(deque.pollFirst()); // "front"
console.log(deque.pollLast()); // "back"
ts-collections provides a broader set of data structures and stronger collection APIs than native arrays, maps, and sets. It adds type-safe behavior, consistent method names, and specialized implementations like priority queues and sorted trees.
Runtime validation can add a small cost on inserts, but it helps catch invalid values early and keeps collections reliable. The library is designed so this overhead is minimal for most typical use cases.
Yes — runtime validation can be customized with a Zod schema or a custom validator, and it can be disabled by setting strict: false. This makes it easy to choose either the safety of runtime checks or the speed of unchecked collections.
Use ArrayList for ordered lists and frequent iteration, HashMap for key-value lookups, and HashSet for unique values. Choose LinkedQueue for FIFO queues, PriorityQueue for priority-based processing, and TreeMap/TreeSet when you need sorted data and ordered traversal.
Yes, the library follows Java-style collection interfaces and naming patterns while remaining idiomatic to TypeScript. It combines familiar Java concepts with TypeScript runtime safety and native JavaScript interop.
import { ArrayList } from "ts-collections";
interface Product {
id: string;
name: string;
price: number;
}
class ShoppingCart {
private items = new ArrayList<Product>();
addItem(product: Product): void {
this.items.add(product);
}
getTotalPrice(): number {
return this.items.toArray().reduce((sum, item) => sum + item.price, 0);
}
}
const cart = new ShoppingCart();
cart.addItem({ id: "1", name: "Laptop", price: 999 });
console.log(cart.getTotalPrice()); // 999
import { HashMap } from "ts-collections";
interface Session {
userId: string;
loginTime: Date;
}
class SessionManager {
private sessions = new HashMap<string, Session>();
createSession(userId: string): string {
const sessionId = this.generateId();
this.sessions.put(sessionId, { userId, loginTime: new Date() });
return sessionId;
}
getSession(sessionId: string): Session | undefined {
return this.sessions.get(sessionId);
}
}
import { LinkedQueue } from "ts-collections";
const taskQueue = new LinkedQueue<string>();
// Add tasks
taskQueue.offer("Process payment");
taskQueue.offer("Send email");
taskQueue.offer("Update inventory");
// Process tasks
while (taskQueue.size() > 0) {
const task = taskQueue.poll();
console.log(`Processing: ${task}`);
}
import { HashSet } from "ts-collections";