java - How to iterate HashMap without entrySet -
in original java source, must use entryset iterate hashmap, instance:
(map.entry<string, string> e : new hash<string, string>().entryset()) { }
i expect more simple way iterate hashmap following:
package ro.ex; import java.util.hashmap; import java.util.map; /** * created roroco on 11/17/14. */ class hash<k, v> extends hashmap<k, v> { // how implement? } public class ex2 { public static void main(string[] args) { // expect (hash.entry<string, string > e : new hash<string, string>()) { } } }
my question is how implement it?
implement iterable<map.entry<k, v>>
:
class hash<k, v> extends hashmap<k, v> implements iterable<map.entry<k, v>> { @override public iterator<entry<k, v>> iterator() { return entryset().iterator(); } }
this works because implementing iterable
allows type used in for-each loop.
Comments
Post a Comment