HashTable help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NagarajanS
    New Member
    • Dec 2006
    • 39

    #1

    HashTable help

    Hi,
    i need one help in HashTable. see the below example
    [CODE=java]/*
    * HashTableTest.j ava
    *
    * Created on October 20, 2007, 12:48 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package kcbchatapplicat ion;

    import java.util.Hasht able;
    import java.util.Itera tor;

    /**
    *
    * @author nagaraj
    */
    public class HashTableTest {

    /** Creates a new instance of HashTableTest */
    private static Hashtable table;
    public HashTableTest() {
    table=new Hashtable();
    }
    private void AddTwo()
    {
    table.put("firs t","First");
    table.put("seco nd","Second") ;
    }
    private void AddThree()
    {
    table.put("thre e","Three");
    }

    public static void main(String a[])
    {
    HashTableTest test=new HashTableTest() ;
    test.AddTwo();
    // Enumeration en=table.elemen ts();

    for (Iterator it=table.keySet ().iterator(); it.hasNext(); ) {
    Object key = it.next();
    Object value = table.get(key);
    System.out.prin tln("Values:" +value.toString ());
    }
    test.AddThree() ;
    for (Iterator it1=table.keySe t().iterator(); it1.hasNext(); ) {
    Object key = it1.next();
    Object value = table.get(key);
    System.out.prin tln("Values:" +value.toString ());
    }
    /* while(en.hasMor eElements())
    {
    System.out.prin tln("Values:" +en.nextElement ().toString());
    }
    test.AddThree() ;
    Enumeration en1=table.eleme nts();
    while(en1.hasMo reElements())
    {
    System.out.prin tln("Values:" +en1.nextElemen t().toString()) ;
    }*/
    }

    public Hashtable getTable() {
    return table;
    }
    }[/CODE]

    while running this example i get the out put like this,
    Values:Second
    Values:First
    Values:Second
    Values:Three
    Values:First
    But my need is,this will be look as following,
    Values:Second
    Values:First
    Values:Second
    Values:First
    Values:Three

    How to do that?
    Regards,
    Nags
    Last edited by Ganon11; Oct 22 '07, 01:12 PM. Reason: Please use the [CODE] tags provided.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by NagarajanS
    How to do that?
    Regards,
    Nags
    Have a look at the LinkedHashMap class instead; a HashTable can't do what
    you want it to do.

    kind regards,

    Jos

    Comment

    • NagarajanS
      New Member
      • Dec 2006
      • 39

      #3
      hi,
      Can any body give a a small example in linked hashtable?
      Regards,
      Nags

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by NagarajanS
        hi,
        Can any body give a a small example in linked hashtable?
        Regards,
        Nags
        Simply replace the HashTable references in your code by LinkedHashMap
        references. Both classes are members of the Collections framework.

        kind regards,

        Jos

        Comment

        Working...