HashMap.js 810 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import ArrayList from './ArrayList'
  2. import MapInterface from './Map'
  3. import HashSet from './HashSet'
  4. /**
  5. * @see http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
  6. */
  7. export default class HashMap extends MapInterface {
  8. constructor() {
  9. super()
  10. this.map = new Map()
  11. }
  12. get(key) {
  13. return this.map.get(key) || null
  14. }
  15. put(key, value) {
  16. this.map.set(key, value)
  17. return value
  18. }
  19. values() {
  20. const arrayList = new ArrayList()
  21. const it = this.map.values()
  22. let o = it.next()
  23. while (!o.done) {
  24. arrayList.add(o.value)
  25. o = it.next()
  26. }
  27. return arrayList
  28. }
  29. entrySet() {
  30. const hashSet = new HashSet()
  31. this.map.entries().forEach(entry => hashSet.add(entry))
  32. return hashSet
  33. }
  34. size() {
  35. return this.map.size()
  36. }
  37. }