Skip to content

jazelly/collections-jdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome

collections-jdk is a project inspired by JDK Collections.

Features

  • Heap (Priority queue)
  • TreeMap (Sorted map by key implemented in Red-black Tree)
  • TreeSet (Sorted set)
  • BTreeMap (Sorted map in B-tree)

Example Usage

npm install collections-jdk
import { PriorityQueue } from 'collections-jdk';

const pq0 = new PriorityQueue(); // not ok - didn't specify element type
const pq2 = new PriorityQueue({from: [3, 4, 5]}) // not ok - didn't specify element type
const pq3 = new PriorityQueue<number>(); // ok - number is comparable
const pq4 = new PriorityQueue<{ id: number }>() // not ok - object is not comparable, need to specify comparator
const pq5 = new PriorityQueue<{ id: number }>({ comparator: (a, b) => {
  return a.id - b.id;
}}) // ok
const pq6 = new PriorityQueue({ from: [1, 2, 3] as number[] }) // ok
const pq6 = new PriorityQueue({ from: [{ id: 1 }] as Record<string, number>[] }) // not ok - object is not comparable


pq5.add({ id: 3});
pq5.peek(); // { id: 3 }
pq5.add({ id: 4});
pq5.peek(); // { id: 4 }
pq5.add({ id: 5});
pq5.peek(); // { id: 5 }
pq5.pop();
pq5.pop();
pq5.peek(); // { id: 3 }

Why choose this repo

  1. collections-jdk is fully typed, and it's recommended to use TypeScript in VS Code to leverage the intellisense and validation. For TS users, collections-jdk guarantees 100% fast-fail for wrong data types
  2. collections-jdk strictly follows the Java util library, and there are some extra supports on top of that

FAQ

Q: What runtime does collections-ts support?
A: We aim to support all common runtime of js, node.js, deno, bun, broswer etc.

Q: Can JS users use this?
A: Yes! collections-jdk aim to support JS users, but it's highly recommended to use this package with TS files

All kinds of contribution are welcome

Feel free to raise pull requests and issues