Question:
Help with iterators hash tables in java?
Tom
2010-04-26 17:05:24 UTC
I'm very confused on what iterators and hash tables do in java. Can anyone explain it simply? What does it mean when a method is returning an iterator? How do I nest a hash table?
Three answers:
vincentgl
2010-04-27 07:57:15 UTC
When you have a collections object and you want to navigate through it and visit each of the elements stored in the collection, you either have to know something about the structure of the collection, or you can call the iterator() method and get back some object that implements the Iterator interface. Doing the latter is more convenient, in that you need not know anything about the actual collection implementation. I can work with it knowing that it is just "some sort of " Map, or Set, or List. For example, I don't need to know that the collection someone passes me is a TreeSet and is backed by a Red-Black Tree. Instead, I can specify that I want a Set and they can pass me a HashSet or a TreeSet. So how do I inspect all the contents if I don't know which implementation it is? Easy! I ask it for an Iterator! The Iterator implementation knows how to navigate that particular implementation.

(Note: for Map implementations, you retrieve the Set of keys, then iterate the keys, using the keys to retrieve the values.)



A hash table (either the java.util.Hashtable or java.util.HashMap) in java is an associative map collection. What that means, is that value objects are stored based on a key object, i.e. the value is associated with a key. The keys are hashed and that hash value is used to index into a lookup table for finding the value. You can nest them if you really think it makes sense. Just be sure that whatever you use as the key object type has properly implemented hashCode() and equals() methods. So yes, you could store HashMap instances as values inside a Hashtable.
Bo Zimmerman
2010-04-27 17:56:56 UTC
A Hashtable is a type of container, meaning its an object that contains other objects. In the case of a Hashtable, the container allows you to map (or PAIR) two objects together into a single ENTRY. One of the objects in the entry is the KEY and the other is the VALUE. A Hashtable then contains as many entries as you have keys. Hashtables are great because they guarentee fast lookup of keys and the value they are associated with. The lookup time is what we call in computer science "constant", meaning that adding new entries to your Hashtable won't make it take longer to look up the keys and their values.



Iterators let you step through each and every object inside a collection. In Java, all the containers that are Set-types contain an iterator method. The iterator object can be checked to see if you've already seen all the objects iterator.hasNext() and it can move on to the next object in the container iterator.next(). You can also remove the current object from the container using an iterator.remove() method.
portell
2016-12-04 08:37:42 UTC
you will earnings so plenty extra function employing Generics HashMap hashMap = new HashMap(); then, you get the Itenerator for unfastened for( vehicle v : hashMap ) or for( vehicle v : hashMap ) if( v.producer.equalsIgnoreCase("Tata") ) HashMap enforces unique gadgets (no duplicates ); HashTree alphabetizes (ordering) the set.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...