java - How to write HashMap iterator? -
when use iterator = densebagmap.keyset().iterator(), return key once though if contains more once. want able return element let's if 'a' occurs 3 times. next method should able return "a" 3 times not sure how can that. sort of slow in helpful if can show me iterator actual code. hasnext() , next() method required iterator.
public class densebag<t> extends abstractcollection<t> { private map<t, integer> densebagmap; private int size; // total number of elements in bag /** * initialize new, empty densebag */ public densebag() { densebagmap = new hashmap<t, integer>(); }; public boolean equals(object o) { if (o == this) { return true; } if (!(o instanceof densebag)) { return false; } densebag<t> dense = (densebag<t>) o; return size == dense.size; } public int hashcode() { return this.densebagmap.hashcode(); } //i not sure how write iterator method this. public iterator<t> iterator() { return new iterator<t>() { public boolean hasnext(){ } public t next(){ }
a keyset is, definition, set , therefore contains only 1 entry each key. if wish store entries added under each key should use apache multimap.
Comments
Post a Comment