Set data structure of Java in javascript/jQuery

后端 未结 5 1667
臣服心动
臣服心动 2021-02-02 10:52

Is there any way to create Set data structure(Unique Collections) like java in javascript?

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 11:31

    In modern browsers, I would use a Map using only the keys' methods. For example:

    • New set: let mySet = new Map();
    • Add element: mySet.set(element, 1)
    • Remove element: mySet.delete(element)
    • Contains element: mySet.has(element)

    You can make your own wrapper if you don't want to see the Map implementation. This approach is how Java HashSet is implemented, it uses a HashMap and only uses the key methods.

提交回复
热议问题