0.1.4 • Published 5 years ago
collection4js v0.1.4
collection4js
collection tools,HashMap/ArrayList etc.
Description
This project provides a collection library that runs on NodeJs and browsers.
HashMapAllow string | number type keys, any type of value.ArrayListAllow any type of value.
Method chaining
All methods that do not return content will return instances to support chain calls
HashMap
constructor overloads
new HashMap()Create an empty HashMapnew HashMap(map:HashMap)Create a HashMap using key value pairs
methods
get(key: K): VReturns the value stored by the keyput(key: K, value: V):this;Insert key-value pairskeys(): Array<K>Returns an array of all keyscontainsKey(key: K):booleanDoes it contain keysvalues(): Array<V>Returns an array of all valuesforEach(callbackfn: (key: K, value: V) => boolean | any, context?: any): voidIterate over these key-value pairs and call functions for each pair. @param callbackfn The callback of the loop. Return false in the function terminates the entire loop. Any value of return that is not false terminates a single loop @param context Context within callback functionremove(key: K): voidDelete key-value pairs by keyclear(): voidDelete all key-value pairssize(): numberThe size of key-value pairsclone(): Map<K, V>The clone becomes a new set of key-value pairsisEmpty(): booleanIs it a set of null key-value pairs
Examples
If you use it in Node, you first need to import this class
import { HashMap } from 'collection4js';
var Collection4js = require('collection4js');
var hashMap = new Collection4js.HashMap();
//More maps or list to be updated laterSimple use cases
map.put("key1", "value");Map size
map.put("key1", "value");
map.put("key2", "value");
map.size; // -> 2Delete key-value pairs
map.put("key1", "value");
map.remove("key1");
map.get("key1"); // --> nullSame key coverage
map.put("key1", "one");
map.put("key1", "two");
map.get("key1"); // --> "two"Iterating
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.forEach(function(key, value) {
console.log(key + " : " + value);
});Method chaining
map
.put(1, "1")
.put(2, "2")
.put(3, "3")
.forEach(function(value, key) {
console.log(key + " : " + value);
});LinkedHashMap
等待后续更新
ArrayList
constructor overloads
new ArrayList()Create an empty ArrayListnew ArrayList(list?: List<T> | Array<T>)Create an ArrayList using an array or an existing collection
methods
size(): numberCollection sizeadd(value: T): thisInsert elementsaddAll(values: T[] | List<T>): thisInsert multiple elementsget(index: number): TGet elementremoveIndex(index: number): TRemove the specified subscript elementremoveObject(obj: T): TRemove the specified elementisEmpty(): booleanIs it emptyfilter(callbackfn: (value: T) => boolean | any, context?: any): voidFilter these elements and call functions for each element. @param callbackfn The callback is repeated one by one, and return true in the function is added to the filter result set @param context Context within callback functionforEach(callbackfn: (value: T) => boolean | any, context?: any): voidIterate over these element and call functions for each. @param callbackfn The callback of the loop. Return false in the function terminates the entire loop. Any value of return that is not false terminates a single loop @param context Context within callback functionclear(): voidclearcontains(obj: T): booleanDoes it contain elementsindexOf(obj: T): numberGet element SubscriptstoArray(): Array<T>Converting to arraysort(sortBy: (a: T, b: T) => number): thiselements sort @param sortBy If A is less than b, A should appear before B in the sorted set, then a value less than 0 is returned. If a equals b, return 0. If A is greater than b, a value greater than 0 is returned.
Examples
If you use it in Node, you first need to import this class
import { ArrayList } from 'collection4js';
var Collection4js = require('collection4js');
var arrayList = new Collection4js.ArrayList();
var arrayList2 = new ArrayList();
//More lists to be updated laterSimple use cases
list.add('value1')
console.log(list.get(0))List size
list.add("value1");
list.add("value2");
console.log(list.size())Delete Element
list.add("value1");
list.removeIndex(0);
//list.removeIndex(0) or list.removeObject('value1')
console.log(list.get(0))sort elements
list.add({order: 3, value: 'value3'})
.add({order: 2, value: 'value2'})
.add({order: 1, value: 'value1'})
.sort(function(a,b) {
return a.order - b.order
}).forEach(function (value) {
console.log(value.value)
});Iterating
list.add("value1");
list.add("value2");
list.add("value3");
list.forEach(function (value) {
console.log(value);
})Method chaining
list.add("value1")
.add("value2")
.add("value3")
.forEach(function (value) {
console.log(value);
});