Hashtables and similars

See Vectors and similars

Hashtable does not allow duplicates, when same key, the last inserted only is retained

 

Looping
    
Method 1, Hashset iteration
Iterator it = inH.iterator();
while(it.hasNext()) {
  File li = it.next();
  System.out.println(li);
}

Method 2, Hashtable iteration
int mapsize = clientPings.size();
Iterator<Map.Entry<Key, Value>> kv = clientPings.entrySet().iterator();
for(int i = 0; i < mapsize; i++) {
  Map.Entry<Key, Value> entry = kv.next();
  Key key = entry.getKey();
  Value value = entry.getValue();
  ...
}

Method 3
Object[] keyValuePairs2 = aMap.entrySet().toArray();
for (int i = 0; i < mapsize; i++)
{
  Map.Entry entry = (Map.Entry) keyValuePairs2[i];
  Object key = entry.getKey();
  Object value = entry.getValue();
  ...
}

 

Sorting an hashtable
public class SortHashtable {

  public static void main(String[] args) {
    // Create and populate hashtable
    Hashtable ht = new Hashtable();
    ht.put("ABC", "abc");
    ht.put("XYZ", "xyz");
    ht.put("MNO", "mno");
    
    // Sort hashtable.
    Vector v = new Vector(ht.keySet());
    Vector v = new Vector(ht.values());
    Collections.sort(v);
    // Display (sorted) hashtable.
    for (Enumeration e = v.elements(); e.hasMoreElements();) {
      String key = (String)e.nextElement();
      String val = (String)ht.get(key);
      System.out.println("Key: " + key + "     Val: " + val);
    }
  }
}