js-collections-utils
v0.0.1
Published
JavaScript utility collections equivalent to java.utils - includes LinkedList, PriorityQueue, and related data structures
Maintainers
Readme
This is js-util equivalent of java.utils
LinkedList Methods
- iterator(): Returns an iterator over the elements in this collection.
- toArray(): Returns an array containing all of the elements in this collection.
- addAll(collection): Adds all of the elements in the specified collection to this collection.
- removeAll(collection): Removes all of this collection's elements that are also contained in the specified collection.
- retainAll(collection): Retains only the elements in this collection that are contained in the specified collection.
- containsAll(collection): Returns true if this collection contains all of the elements in the specified collection.
- subList(fromIndex, toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
- add(element): Appends the specified element to the end of the list.
- addAtIndex(index, element): Inserts the specified element at the specified position in the list.
- removeAtIndex(index): Removes the element at the specified position in the list.
- remove(element): Removes the first occurrence of the specified element from this list.
- get(index): Returns the element at the specified position in the list.
- set(index, element): Replaces the element at the specified position in the list with the specified element.
- size(): Returns the number of elements in the list.
- clear(): Removes all elements from the list.
- isEmpty(): Returns true if the list contains no elements.
- contains(element): Returns true if the list contains the specified element.
- sort(comparator): Sorts this list using the supplied comparator function.
- indexOf(element): Returns the index of the first occurrence of the specified element.
- lastIndexOf(element): Returns the index of the last occurrence of the specified element.
Deque Methods
- addFirst(element): Inserts the specified element at the front of this deque.
- addLast(element): Inserts the specified element at the end of this deque.
- removeFirst(): Retrieves and removes the first element of this deque.
- removeLast(): Retrieves and removes the last element of this deque.
- getFirst(): Retrieves, but does not remove, the first element of this deque.
- getLast(): Retrieves, but does not remove, the last element of this deque.
- peekFirst(): Retrieves, but does not remove, the first element of this deque, or returns null if empty.
- peekLast(): Retrieves, but does not remove, the last element of this deque, or returns null if empty.
- offerFirst(element): Inserts the specified element at the front (returns false if capacity-restricted).
- offerLast(element): Inserts the specified element at the end (returns false if capacity-restricted).
- pollFirst(): Retrieves and removes the first element, or returns null if empty.
- pollLast(): Retrieves and removes the last element, or returns null if empty.
- push(element): Pushes an element onto the stack (equivalent to addFirst).
- pop(): Pops an element from the stack (equivalent to removeFirst).
Queue Methods
- offer(element): Inserts the specified element into this queue (returns false if capacity-restricted).
- poll(): Retrieves and removes the head of this queue, or returns null if empty.
- peek(): Retrieves, but does not remove, the head of this queue, or returns null if empty.
- element(): Retrieves, but does not remove, the head of this queue (throws if empty).
PriorityQueue Methods
- add(element): Inserts the specified element into the priority queue.
- offer(element): Inserts the specified element into the priority queue (returns false if full).
- remove(element): Removes a single instance of the specified element.
- poll(): Retrieves and removes the head of the queue, or returns null if the queue is empty.
- peek(): Retrieves, but does not remove, the head of the queue, or returns null if the queue is empty.
- element(): Retrieves, but does not remove, the head of the queue (throws if empty).
- size(): Returns the number of elements in the queue.
- clear(): Removes all elements from the queue.
- isEmpty(): Returns true if the queue contains no elements.
- contains(element): Returns true if the queue contains the specified element.
- toArray(): Returns a sorted array of all elements.
- removeIf(filter): Removes all elements that satisfy the given predicate.
- addAll(collection): Adds all elements from another collection.
Sample Code
LinkedList Example
let linkedList = new LinkedList<string>();
linkedList.add('A');
linkedList.add('B');
linkedList.addAtIndex(1, 'C');
console.log(linkedList.get(1)); // Output: C
linkedList.set(1, 'D');
console.log(linkedList.get(1)); // Output: D
console.log(linkedList.size()); // Output: 3
linkedList.removeAtIndex(1);
console.log(linkedList.contains('D')); // Output: false
linkedList.clear();
console.log(linkedList.isEmpty()); // Output: truePriorityQueue Example
let priorityQueue = new PriorityQueue<string>();
priorityQueue.add('A');
priorityQueue.offer('B');
console.log(priorityQueue.peek()); // Output: A
console.log(priorityQueue.size()); // Output: 2
console.log(priorityQueue.poll()); // Output: A
console.log(priorityQueue.contains('B')); // Output: true
priorityQueue.clear();
console.log(priorityQueue.isEmpty()); // Output: trueLinkedList as Queue Example
let queue = new LinkedList<string>();
queue.offer('A');
queue.offer('B');
console.log(queue.peek()); // Output: A
console.log(queue.size()); // Output: 2
console.log(queue.poll()); // Output: A
console.log(queue.isEmpty()); // Output: falseStack Example
let stack = new LinkedList<string>();
stack.push('A');
stack.push('B');
console.log(stack.peekFirst()); // Output: B
console.log(stack.size()); // Output: 2
console.log(stack.pop()); // Output: B
console.log(stack.isEmpty()); // Output: falseDeque Example
let deque = new LinkedList<string>();
deque.addFirst('A');
deque.addLast('B');
console.log(deque.peekFirst()); // Output: A
console.log(deque.peekLast()); // Output: B
console.log(deque.size()); // Output: 2
console.log(deque.removeFirst()); // Output: A
console.log(deque.isEmpty()); // Output: falseLinkedList Iterator Example
let linkedList = new LinkedList<string>();
linkedList.add('A');
linkedList.add('B');
linkedList.add('C');
let iterator = linkedList.iterator();
while (iterator.hasNext()) {
console.log(iterator.next());
}
// Output: A B CFor-of Loop Example
let linkedList = new LinkedList<string>();
linkedList.add('A');
linkedList.add('B');
linkedList.add('C');
for (const item of linkedList) {
console.log(item);
}
// Output: A B C