Iterators and Hashtable in Java Baeldung

How can I make the Iterator

point where I want to Hashtable

, other than the start?

Suppose I have

Hashtable P = new Hashtable();
P.put("P10", new Integer (10) );
P.put("P11", new Integer (11) );
P.put("P13", new Integer (13) );
P.put("P17", new Integer (17) );
P.put("P15", new Integer (15) );
Set PSet = P.entrySet();
Iterator ptP = Pset.iterator();

      

How can I make a Iterator

point P17

without the need for repetition. I would like to write something like C ++, i.e. Something like ptP = P17

to have a pointer or Iterator

point to that element first.

+3


source to share


1 answer


You cannot do this with HashTable or HashMap.

If you can use TreeMap instead of HashTable, you can use:



Iterator iter = tailMap("P17",true).entrySet().iterator();

      

This iterator will iterate starting at the entry "P17".

+4


source







All Articles